class Panel::Archive::BackEnd::ArchiveFileCategorysController < OrbitBackendController  
  include OrbitControllerLib::DivisionForDisable

  open_for_manager :except => [:index,:get_categorys_json,:get_archive_files_json]
  open_for_sub_manager :except => [:index,:get_categorys_json,:get_archive_files_json]
  
  def index
    @archive_file_categorys =  get_categories_for_index("ArchiveFileCategory")
    # @archive_file_categorys = ArchiveFileCategory.all
    @archive_file_category = ArchiveFileCategory.new(:display => 'List')
	
	  @url = panel_archive_back_end_archive_file_categorys_path
	
    respond_to do |format|
      format.html # index.html.erb
      format.js
    end
  end

  def get_categorys_json
    categorys = ArchiveFileCategory.all
    data = Array.new

    categorys.each do |c|
      data << {
        category: c.title,
        link: "#{url_for( :action => "index",
                          :controller => "panel/archive/front_end/archive_files",
                          :format => :rss,
                          :only_path => false,
                          :inner=>true,
                          :category_id => c )}"
      }
    end

    render :json => JSON.pretty_generate(data)
  end

  def get_archive_files_json
    archive_file = ArchiveFileCategory.find(params[:archive_file_category_id]).archive_files
    p archive_file
    data = Array.new

    archive_file.each do |b|

      data << {
        title: b.title,
        link: "http://#{request.host_with_port}#{panel_archive_front_end_archive_file_path(b, :category_id => b.archive_file_category.id)}",
        tag: b.sorted_tags.to_a,
      }
    end

    render :json => JSON.pretty_generate(data)
  end

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

    respond_to do |format|
      format.html # show.html.erb
      format.js
    end
  end

  # GET /archive_files/new
  # GET /archive_files/new.xml
  def new
    @archive_file_category = ArchiveFileCategory.new(:display => 'List')

    respond_to do |format|
      format.html # new.html.erb
      format.js
    end
  end
  
  # GET /archive_files/1/edit
  def edit
    @archive_file_category = ArchiveFileCategory.find(params[:id])
	
	@url = panel_archive_back_end_archive_file_category_path(@archive_file_category)

    respond_to do |format|
      format.html
      format.js
    end
  end

  # POST /archive_files
  # POST /archive_files.xml
  def create
    @archive_file_category = ArchiveFileCategory.new(params[:archive_file_category])

    respond_to do |format|
      if @archive_file_category.save
        format.html { redirect_to(panel_archive_back_end_archive_file_categorys_url, :notice => t('archive_file_category.create_archive_file_category_success')) }
        format.js
      else
        format.html { render :action => "new" }
        format.js { render action: "new" }
      end
    end
  end

  # PUT /archive_files/1
  # PUT /archive_files/1.xml
  def update
    @archive_file_category = ArchiveFileCategory.find(params[:id])
	
	@url = panel_archive_back_end_archive_file_category_path(@archive_file_category)

    respond_to do |format|
      if @archive_file_category.update_attributes(params[:archive_file_category])
        format.html { redirect_to(panel_archive_back_end_archive_file_categorys_url, :notice => t('archive_file_category.update_archive_file_category_success')) }
        # format.xml  { head :ok }
        format.js
      else
        format.html { render :action => "edit" }
        format.js { render :action => "edit" }
      end
    end
  end

  # DELETE /archive_files/1
  # DELETE /archive_files/1.xml
  def destroy
    @archive_file_category = ArchiveFileCategory.find(params[:id])
    @archive_file_category.disable = @archive_file_category.disable ? false : true

    if @archive_file_category.save! 
      respond_to do |format|
        flash[:notice] = t("update.success_")
        # flash[:error] +=  @archive_file_category.disable ?  t(:enable) : t(disable)
        format.html { redirect_to(panel_archive_back_end_archive_file_categorys_url) }
        # format.xml  { head :ok }
        format.js 
      end
    else
      flash[:error] = t("update.fail")
      format.html { render :action => "index" }
    end
  end
end
