class Panel::Faq::BackEnd::QasController < OrbitBackendController
 
  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
    @statuses = get_statuses
    @categories = get_categories_for_index

    category_ids = @categories.collect{|t| t.id}
    @qas = get_sorted_and_filtered("qa", :category_id.in => category_ids)

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

  # GET /qas/1
  # GET /qas/1.xml
  def show
    @qa = Qa.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @qa }
    end
  end

  # GET /qas/new
  # GET /qas/new.xml
  def new
    @qa = Qa.new
    @tags = get_tags
    @statuses = get_statuses
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @qa }
    end
  end

  # GET /qas/1/edit
  def edit
    @qa = Qa.find(params[:id])
     @statuses = get_statuses
	   @tags = get_tags
  end

  # POST /qas
  # POST /qas.xml
  def create
    @qa = Qa.new(params[:qa])
	@tags = get_tags
    @qa.create_user_id = current_user.id
    @qa.update_user_id = current_user.id

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

  # PUT /qas/1
  # PUT /qas/1.xml
  def update
    @qa = Qa.find(params[:id])
    @qa.update_user_id = current_user.id

    respond_to do |format|
      if @qa.update_attributes(params[:qa])

        format.html { redirect_to(panel_faq_back_end_qas_path) }
		    format.js  { render 'toggle_enable' }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @qa.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /qas/1
  # DELETE /qas/1.xml
  def destroy
    @qa = Qa.find(params[:id])
    @qa.destroy

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

  def delete
    if params[:ids]
      qas = Qa.any_in(:_id => params[:ids]).destroy_all
    end
    redirect_to panel_faq_back_end_qas_url(:direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options])
  end
  
end
