class Panel::Archive::Widget::ArchiveFilesController < OrbitWidgetController
  include AdminHelper

  def initialize
    super
    @app_title = 'archive_files'
  end

  def index

  	@part = PagePart.find(params[:part_id])

	  if @part.widget_data_count
		  @page_num = @part.widget_data_count
	  else
		  @page_num = 4
	  end

	  if @part.widget_field
	  	@widget_fields = @part.widget_field
	  else
  		@widget_fields = []
	  end

    @widget_style = @part.widget_style
    @widget_style = !@part.widget_style.blank? ? @part.widget_style : "1"

	  @category_id = @part.category

    date_now = Time.now

    # if !params[:category_id].blank?
    #   @archive_files = ArchiveFile.default_sort(params[:sort]).all.can_display.any_in(:category_id => params[:category_id]).merge(ArchiveFileCategory.excludes('disable' => true)).desc( :is_top ).page( params[:page_main] ).per(@page_num)
    #   @archive_file_categorys = ArchiveFileCategory.any_in(:_id => params[:category_id]).excludes('disable' => true)
    # elsif !params[:tag_id].blank?
    #   @archive_files = ArchiveFile.default_sort(params[:sort]).all.can_display.any_in(:tagged_ids => params[:tag_id]).merge(ArchiveFileCategory.excludes('disable' => true)).desc( :is_top ).page( params[:page_main] ).per(@page_num)
    #   get_categorys
    # else
    #   @archive_files = ArchiveFile.default_sort(params[:sort]).all.can_display.merge(ArchiveFileCategory.excludes('disable' => true)).desc( :is_top).page( params[:page_main] ).per(@page_num)
    #   get_categorys
    # end

    # if !params[:category_id].blank? && !params[:tag_id].blank?
    #   @archive_files = ArchiveFile.default_sort(params[:sort]).can_display.where(:category_id.in => params[:category_id], :tagged_ids.in => params[:tag_id]).desc( :is_top ).page( params[:page_main] ).per(@page_num)
    #   @current_category = ArchiveFileCategory.from_id(params[:category_id]) rescue nil
    # elsif !params[:category_id].blank?
    #   @archive_files = ArchiveFile.default_sort(params[:sort]).can_display.where(:category_id.in => 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?
    #   @archive_files = ArchiveFile.default_sort(params[:sort]).can_display.where(:tagged_ids.in => params[:tag_id]).desc( :is_top ).page( params[:page_main] ).per(@page_num)
    # else
    #   @archive_files = ArchiveFile.default_sort(params[:sort]).can_display.desc( :is_top ).page( params[:page_main] ).per(@page_num)
    # end



    if !params[:category_id].blank?
      @archive_file_categorys = @module_app.categories.enabled.any_in(:_id => params[:category_id])
      @archive_file_category_ids = @module_app.categories.enabled.any_in(:_id => params[:category_id]).collect{|a|a.id}
    else
      @archive_file_categorys = @module_app.categories.enabled
      @archive_file_category_ids = @module_app.categories.enabled.collect{|a|a.id}
    end

    if @widget_style == "1"

      if !params[:category_id].blank? && !params[:tag_id].blank?
        @archive_files = ArchiveFile.can_display.where(:category_id.in => @archive_file_category_ids, :tagged_ids.in => params[:tag_id], :title.nin=>[""]).desc( :is_top ).default_sort(params[:sort]).page( params[:page_main] ).per(@page_num)
      elsif !params[:category_id].blank?
        @archive_files = ArchiveFile.can_display.where(:category_id.in => @archive_file_category_ids, :title.nin=>[""]).desc( :is_top ).default_sort(params[:sort]).page( params[:page_main] ).per(@page_num)
      elsif !params[:tag_id].blank?
        @archive_files = ArchiveFile.can_display.where(:category_id.in => @archive_file_category_ids,:tagged_ids.in => params[:tag_id], :title.nin=>[""]).desc( :is_top ).default_sort(params[:sort]).page( params[:page_main] ).per(@page_num)
      else
        @archive_files = ArchiveFile.can_display.where(:category_id.in => @archive_file_category_ids, :title.nin=>[""]).desc( :is_top ).default_sort(params[:sort]).page( params[:page_main] ).per(@page_num)
      end

    elsif @widget_style == "2"

      @archive_file_datas = Array.new

      @archive_file_categorys.each do |afcg|

        if !params[:category_id].blank? && !params[:tag_id].blank?
          @archive_files = ArchiveFile.can_display.where(:category_id.in => [afcg.id], :tagged_ids.in => params[:tag_id], :title.nin=>[""]).desc( :is_top ).default_sort(params[:sort]).limit(@page_num)
        elsif !params[:category_id].blank?
          @archive_files = ArchiveFile.can_display.where(:category_id.in => [afcg.id], :title.nin=>[""]).desc( :is_top ).default_sort(params[:sort]).limit(@page_num)
        elsif !params[:tag_id].blank?
          @archive_files = ArchiveFile.can_display.where(:category_id => afcg.id, :tagged_ids.in => params[:tag_id], :title.nin=>[""]).desc( :is_top ).default_sort(params[:sort]).limit(@page_num)
        else
          @archive_files = ArchiveFile.can_display.where(:category_id => afcg.id, :title.nin=>[""]).desc( :is_top ).default_sort(params[:sort]).limit(@page_num)
        end

        archive_file_datas = Array.new

        @archive_files.each do |af|

          archive_file_multiple_datas = Array.new

          af.archive_file_multiples.asc(:sort_number).each do | afile |
            archive_file_multiple_datas << {
             "file_title" => (!afile.file_title.blank? ? afile.file_title : t('archive.download') ) ,
             "url" => afile.file.url,
             "icon" => af.get_file_icon(afile.file.url)
            } if afile.file.file and afile.choose_lang_display(I18n.locale.to_s)
          end

          archive_file_datas << { "title" => af.title, "is_hot" => (!af.is_hot.blank? ? '1' : ''), "archive_file_multiples" => archive_file_multiple_datas } if !af.archive_file_multiples.blank? and !archive_file_multiple_datas.blank?
        end
        
        @archive_file_datas << { "title" => afcg.title, "archive_files" => archive_file_datas } if !@archive_files.blank?

      end

    end   
    

    if params[:sort]
      @archive_files = get_sorted_and_filtered('archive_file', nil, @archive_files)
    end

  end

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

end
