class Panel::PersonalHonor::Plugin::HonorsController < 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('HonorCategory')
  end

  def index

  get_categorys("HonorCategory",params[:honor_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
  
  @honor_categorys =  get_categories_for_index("HonorCategory")
  @honor_category_ids = @honor_categorys.collect{|t| t.id.to_s} + [nil]
  
  @honors = (params[:sort] || @filter) ? get_sorted_and_filtered("honor",:create_user_id => current_user.id) : get_viewable("honor", :create_user_id => current_user.id)

  get_tags

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

  def honor_setting
  
    @honor_types = HonorCategory.all
	
    get_tags
	
    @set_honor_type = HonorCategory.new(:display => 'List')
	@honor_type_url = panel_personal_honor_plugin_honors_path
	
	
  end

  def honor_category_quick_add    
    @set_honor_type = HonorCategory.new(:display => 'List')
    @honor_type_url = panel_personal_honor_plugin_honors_path
    @set_honor_type.id = params[:id]
	
    respond_to do |format|
      format.js
    end
	
  end

  def honor_category_quick_edit
  
    @set_honor_type = HonorCategory.find(params[:honor_id])
    @honor_type_url = panel_personal_honor_plugin_honor_path(@set_honor_type)
	
    respond_to do |format|
      format.js
    end
  end

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

  # GET /honors/new
  # GET /honors/new.xml
  def new
  
    @honor = Honor.new
    @honor_categorys = HonorCategory.all
	
    get_tags

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

  # GET /honors/1/edit
  def edit
    @honor = Honor.find(params[:id])
	
    @honor_types = HonorCategory.all
	
    get_tags
  end

  # POST /honors
  # POST /honors.xml
  def create
  
  if params[:honor_category]
		
		@honor_category = HonorCategory.new(params[:honor_category])

		respond_to do |format|
		  if @honor_category.save
			format.js { render 'create_honor_setting' }
		  end
		end
	
  else
	
    @honor_types = HonorCategory.all
	get_tags
  
    @honor = Honor.new(params[:honor])
	
    @honor.create_user_id = current_user.id
    @honor.update_user_id = current_user.id

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

  # PUT /honors/1
  # PUT /honors/1.xml
  def update
  
    if params[:honor_category]
  	
  		@honor_category = HonorCategory.find(params[:id])

  		respond_to do |format|
            
  		  if @honor_category.update_attributes(params[:honor_category])
  			# format.html { redirect_to(panel_announcement_plugin_bulletins_url) }
  			format.js { render 'update_honor_setting' }
  		  end
  		end
		
  	else
	
    @honor = Honor.find(params[:id])
	
    @honor.update_user_id = current_user.id
	
	params[:honor][:tag_ids] ||=[]

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

  # DELETE /honors/1
  # DELETE /honors/1.xml
  def destroy
    @honor = Honor.find(params[:id])
    @honor.destroy

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

  def delete
    if params[:ids]
      honors = Honor.any_in(:_id => params[:ids]).delete_all
    end
    redirect_to panel_personal_honor_plugin_honors_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)
  #   @honor_categorys = []
  #   if(is_manager? || is_admin?)
  #     @honor_categorys = (id ? HonorCategory.admin_manager_all.find(id).to_a : HonorCategory.admin_manager_all))
  #   elsif is_sub_manager?
  #     @honor_categorys = HonorCategory.all.authed_for_user(current_user,'edit')
  #   end
  #   if @honor_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_honor'})
  	@tags = Tag.all(:conditions => {:module_app_id => module_app.id})
  end
  
end
