class Panel::PersonalLab::BackEnd::LabsController < OrbitBackendController
  include AdminHelper
  include OrbitControllerLib::DivisionForDisable
  
  before_filter :authenticate_user!
  before_filter :force_order_for_visitor,:only=>[:index,:show]
  before_filter :force_order_for_user,:except => [:index,:show]
  # before_filter :for_app_manager,:except => [:index,:show] 
  # before_filter :only => [ :new,:edit,:update] do |controller|
    # controller.get_categorys('LabCategory')
  # end

  def index

  # get_categorys("LabCategory",params[:lab_category_ids])
  @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
  
  # @lab_categorys =  get_categories_for_index("LabCategory")
  # @lab_category_ids = @lab_categorys.collect{|t| t.id.to_s} + [nil]

  
  @labs = (params[:sort] || @filter) ? get_sorted_and_filtered("lab") : get_viewable("lab")

  get_tags

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

  def lab_setting
	
    get_tags	
	
  end

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

  # GET /labs/new
  # GET /labs/new.xml
  def new
  
    @lab = Lab.new
		
    get_tags

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

  # GET /labs/1/edit
  def edit
    @lab = Lab.find(params[:id])
	
    get_tags
  end

  # POST /labs
  # POST /labs.xml
  def create
  
	get_tags
  
    @lab = Lab.new(params[:lab])

	if params[:lab][:user_id]
      @lab.create_user_id = params[:lab][:user_id]
      @lab.update_user_id = params[:lab][:user_id]
    else
	  @lab.create_user_id = current_user.id
      @lab.update_user_id = current_user.id
    end

    respond_to do |format|
      if @lab.save
		if params[:lab][:user_id]
          format.html { redirect_to(admin_users_new_interface_url(:id=>params[:lab][:user_id],:show_plugin_profile=>'Lab')) }
        else
		  format.html { redirect_to(panel_personal_lab_back_end_labs_url) }
        end

        format.xml  { render :xml => @lab, :status => :created, :location => @lab }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @lab.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /labs/1
  # PUT /labs/1.xml
  def update
  
    @lab = Lab.find(params[:id])
	
    @lab.update_user_id = current_user.id
	
	params[:lab][:tag_ids] ||=[]

    respond_to do |format|
      if @lab.update_attributes(params[:lab])
        format.html { redirect_to(panel_personal_lab_back_end_labs_url) }
		# format.js  { render 'toggle_enable' }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @lab.errors, :status => :unprocessable_entity }
      end
    end
  end
  

  # DELETE /labs/1
  # DELETE /labs/1.xml
  def destroy
    @lab = Lab.find(params[:id])
    @lab.destroy

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

  def delete
    if params[:ids]
      labs = Lab.any_in(:_id => params[:ids]).delete_all
    end
    redirect_to panel_personal_lab_back_end_labs_url(:direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options])
  end
  
  protected
 

  # def get_index_categories(id = nil)
  #   @bulletin_categorys = []
  #   if(is_manager? || is_admin?)
  #     @bulletin_categorys = (id ? BulletinCategory.admin_manager_all.find(id).to_a : BulletinCategory.admin_manager_all)
  #   elsif is_sub_manager?
  #     @bulletin_categorys = BulletinCategory.all
  #   end
  #   @bulletin_categorys
  # end


  # def get_categorys(id = nil)
  #   @lab_categorys = []
  #   if(is_manager? || is_admin?)
  #     @lab_categorys = (id ? LabCategory.admin_manager_all.find(id).to_a : LabCategory.admin_manager_all))
  #   elsif is_sub_manager?
  #     @lab_categorys = LabCategory.all.authed_for_user(current_user,'edit')
  #   end
  #   if @lab_categorys.empty? && params[:action] != "index"
  #       flash[:alert] = t("announcement.error.no_avilb_cate_for_posting")
  #       redirect_to :action => :index
  #   end
  # end
  
  def get_tags
  	module_app = ModuleApp.first(:conditions => {:key => 'personal_lab'})
  	@tags = Tag.all(:conditions => {:module_app_id => module_app.id})
  end
  
end
