class Panel::Archive::BackEnd::ArchiveFilesController < OrbitBackendController
  # before_filter :for_app_manager,:except => [:index,:show] 
  # include OrbitControllerLib::DivisionForDisable

  open_for_sub_manager :except => [:index]

  before_filter :only => [ :new, :create, :edit, :update ] do |controller|
    @categories = get_categories_for_form
  end

  def index
    @tags = get_tags
    @categories = get_categories_for_index
    @statuses = get_statuses
    category_ids = @categories.collect{|t| t.id}

    @archive_files = get_sorted_and_filtered("archive_file", :category_id.in => category_ids)
    respond_to do |format|
      format.html # index.html.erb
      format.js { }
      format.xml  { render :xml => @archive_files }
    end
  end

  # def index

  # get_categorys("ArchiveFileCategory",params[:archive_file_category_id])
  # @filter = params[:filter]
  # new_filter = params[:new_filter]

  # if @filter && params[:clear]
  #   @filter.delete(params[:type])
  # elsif @filter && new_filter
  #   if @filter.has_key?(new_filter[:type]) && @filter[new_filter[:type]].include?(new_filter[:id].to_s)
  #     @filter[new_filter[:type]].delete(new_filter[:id].to_s)
  #   elsif @filter.has_key?(new_filter[:type])
  #     @filter[new_filter[:type]] << new_filter[:id].to_s
  #   else
  #     @filter.merge!({new_filter[:type] => [new_filter[:id].to_s]})
  #   end
  # elsif new_filter
  #   @filter = {new_filter[:type] => [new_filter[:id].to_s]}
  # end
  # @archive_filecategories =  get_categories_for_index("ArchiveFileCategory")
  # @archive_file_category_ids = @archive_file_categories.collect{|t| t.id.to_s} + [nil]

  # @archive_files = (params[:sort] || @filter) ? get_sorted_and_filtered("archive_file",:archive_file_category_id.in => @archive_file_category_ids) : get_viewable("archive_file",:archive_file_category_id.in => @archive_file_category_ids)

  # @tags = get_tags

  #   respond_to do |format|
  #     format.html # index.html.erb
  #     format.xml  { render :xml => @archive_files }
  #     format.js
  #   end
  # end

  # GET /archive_files/1
  # GET /archive_files/1.xml
  def show
    @archive_file = ArchiveFile.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @archive_file }
    end
  end

  # GET /archive_files/new
  # GET /archive_files/new.xml
  def new
    if(session[:in_validate_object].blank?)
      @archive_file = ArchiveFile.new
    else
      @archive_file = session[:in_validate_object]
      session[:in_validate_object] = {}
    end
	
	  @tags = get_tags

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @archive_file }
    end
  end

  # GET /archive_files/1/edit
  def edit
    @archive_file = ArchiveFile.find(params[:id])
    if !current_user.admin?
      redirect_to :action => :index
    else
      @tags = get_tags
    end
  end

  # POST /archive_files
  # POST /archive_files.xml
  def create
    @archive_file = ArchiveFile.new(params[:archive_file])
    @archive_file.create_user_id = current_user.id
    @archive_file.update_user_id = current_user.id

    respond_to do |format|
      if @archive_file.save
        format.html { redirect_to(panel_archive_back_end_archive_files_url) }
        format.xml  { render :xml => @archive_file, :status => :created, :location => @archive_file }
      else
        @tags = get_tags
        format.html { render :action => "new" }
        format.xml  { render :xml => @archive_file.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /archive_files/1
  # PUT /archive_files/1.xml
  def update
    @archive_file = ArchiveFile.find(params[:id])
	
    @archive_file.update_user_id = current_user.id
	
	params[:archive_file][:tag_ids] ||=[]

    respond_to do |format|
      if @archive_file.update_attributes(params[:archive_file])
        format.html { redirect_to(panel_archive_back_end_archive_files_url(:page => params[:page])) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @archive_file.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /archive_files/1
  # DELETE /archive_files/1.xml
  def destroy
    @archive_file = ArchiveFile.find(params[:id])
    @archive_file.destroy

    respond_to do |format|
      format.html { redirect_to(panel_archive_back_end_archive_files_url) }
      # format.xml  { head :ok }
      format.js
    end
  end

  def delete
    if params[:ids]
      archive_files = ArchiveFile.any_in(:_id => params[:ids]).destroy_all
    end
    redirect_to panel_archive_back_end_archive_files_url(:direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options])
  end
  
  protected
  
  # def get_categorys(id = nil)
  #   @archive_file_categorys = (id ? ArchiveFileCategory.find(id).to_a : ArchiveFileCategory.excludes('disabled' => true))
  # end
  
end
