class Panel::PersonalExperience::Plugin::ExperiencesController < OrbitBackendController
  include AdminHelper
  include OrbitControllerLib::DivisionForDisable
  
  before_filter :authenticate_user!
  # before_filter :for_app_manager,:except => [:index,:show] 
  before_filter :only => [ :new,:edit,:update] do |controller|
    controller.get_categorys('ExperienceCategory')
  end

  def index

  get_categorys("ExperienceCategory",params[:experience_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
  
  @experience_categorys =  get_categories_for_index("ExperienceCategory")
  @experience_category_ids = @experience_categorys.collect{|t| t.id.to_s} + [nil]

  @experiences = (params[:sort] || @filter) ? get_sorted_and_filtered("experience",:create_user_id => current_user.id) : get_viewable("experience", :create_user_id => current_user.id)

  @tags = get_tags

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

  def experience_setting
  
    @experience_types = ExperienceCategory.all
	
    @tags = get_tags
	
    @set_experience_type = ExperienceCategory.new(:display => 'List')
	@experience_type_url = panel_personal_experience_plugin_experiences_path
	
	
  end

  def experience_category_quick_add    
    @set_experience_type = ExperienceCategory.new(:display => 'List')
    @experience_type_url = panel_personal_experience_plugin_experiences_path
    @set_experience_type.id = params[:id]
	
    respond_to do |format|
      format.js
    end
	
  end

  def experience_category_quick_edit
  
    @set_experience_type = ExperienceCategory.find(params[:experience_id])
    @experience_type_url = panel_personal_experience_plugin_experience_path(@set_experience_type)
	
    respond_to do |format|
      format.js
    end
  end

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

  # GET /experiences/new
  # GET /experiences/new.xml
  def new
  
    @experience = Experience.new
    @experience_categorys = ExperienceCategory.all
	
    @tags = get_tags

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

  # GET /experiences/1/edit
  def edit
    @experience = Experience.find(params[:id])
	
    @experience_types = ExperienceCategory.all
	
    @tags = get_tags
  end

  # POST /experiences
  # POST /experiences.xml
  def create
  
  if params[:experience_category]
		
		@experience_category = ExperienceCategory.new(params[:experience_category])

		respond_to do |format|
		  if @experience_category.save
			format.js { render 'create_experience_setting' }
		  end
		end
	
  else
	
    @experience_types = ExperienceCategory.all
	@tags = get_tags
  
    @experience = Experience.new(params[:experience])
	
    @experience.create_user_id = current_user.id
    @experience.update_user_id = current_user.id

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

  # PUT /experiences/1
  # PUT /experiences/1.xml
  def update
  
    if params[:experience_category]
  	
  		@experience_category = ExperienceCategory.find(params[:id])

  		respond_to do |format|
            
  		  if @experience_category.update_attributes(params[:experience_category])
  			# format.html { redirect_to(panel_announcement_plugin_bulletins_url) }
  			format.js { render 'update_experience_setting' }
  		  end
  		end
		
  	else
	
    @experience = Experience.find(params[:id])
	
    @experience.update_user_id = current_user.id
	
	params[:experience][:tag_ids] ||=[]

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

  # DELETE /experiences/1
  # DELETE /experiences/1.xml
  def destroy
    @experience = Experience.find(params[:id])
    @experience.destroy

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

  def delete
    if params[:ids]
      experiences = Experience.any_in(:_id => params[:ids]).destroy_all
    end
    redirect_to panel_personal_experience_plugin_experiences_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)
  #   @experience_categorys = []
  #   if(is_manager? || is_admin?)
  #     @experience_categorys = (id ? ExperienceCategory.admin_manager_all.find(id).to_a : ExperienceCategory.admin_manager_all))
  #   elsif is_sub_manager?
  #     @experience_categorys = ExperienceCategory.all.authed_for_user(current_user,'edit')
  #   end
  #   if @experience_categorys.empty? && params[:action] != "index"
  #       flash[:alert] = t("announcement.error.no_avilb_cate_for_posting")
  #       redirect_to :action => :index
  #   end
  # end
  
end
