class Panel::Archive::FrontEnd::ArchiveFilesController < OrbitWidgetController

  def initialize
    super
    @app_title = 'archive_files'
  end
  
  def index
    
	@item = Page.find(params[:page_id])
	
	@title = @item.title
  
  	if @item.frontend_data_count
		@page_num = @item.frontend_data_count
	else
		@page_num = 0
	end
  
	date_now = Time.now
	
	@archive_file_categorys = ArchiveFileCategory.all
	
	# @archive_files = ArchiveFile.where( :is_hidden => false ).desc(:is_top).page(params[:page]).per(@page_num)
	
    if !params[:category_id].blank?
      @archive_files = ArchiveFile.can_display.where(:archive_file_category_id => params[:category_id]).desc( :is_top ).page( params[:page_main] ).per(@page_num)
      @current_category = ArchiveFileCategory.from_id(params[:category_id]) rescue nil
    elsif !params[:tag_id].blank?
      @tag = ArchiveTag.find(params[:tag_id]) rescue nil
      @tag = ArchiveTag.where(key: params[:tag_id])[0] unless @tag
      @archive_files = @tag.archive_files.can_display.desc( :is_top ).page( params[:page_main] ).per(@page_num)
    else
      @archive_files = ArchiveFile.can_display.desc( :is_top ).page( params[:page_main] ).per(@page_num)
    end

	get_categorys
  end
  
  def show
    
	@item = Page.find(params[:page_id])
	
	@title = @item.title
	
    @archive_file = ArchiveFile.find(params[:id])
	
	get_categorys
	
  end
  
  protected
  
  def reload_archive_files
    
	@item = Page.find(params[:page_id])
	
	@title = @item.title
  
  	if @item.frontend_data_count
		@page_num = @item.frontend_data_count
	else
		@page_num = 0
	end
  
	date_now = Time.now
	
	@archive_file_categorys = ArchiveFileCategory.all
	
	# @archive_files = ArchiveFile.where( :is_hidden => false ).desc(:is_top).page(params[:page]).per(@page_num)
	
	
    if !params[:category_id].blank?
      @archive_files = ArchiveFile.can_display.where(:archive_file_category_id => params[:category_id]).desc( :is_top ).page( params[:page_main] ).per(@page_num)
      @current_category = ArchiveFileCategory.from_id(params[:category_id]) rescue nil
    elsif !params[:tag_id].blank?
      @tag = ArchiveTag.find(params[:tag_id]) rescue nil
      @tag = ArchiveTag.where(key: params[:tag_id])[0] unless @tag
      @archive_files = @tag.archive_files.can_display.desc( :is_top ).page( params[:page_main] ).per(@page_num)
    else
      @archive_files = ArchiveFile.can_display.desc( :is_top ).page( params[:page_main] ).per(@page_num)
    end

	get_categorys
	
  end
  
  def get_categorys
    @archive_file_categorys = ArchiveFileCategory.excludes('disabled' => true)
  end
  
end
