class Panel::PersonalHonor::BackEnd::HonorsController < OrbitBackendController
  include AdminHelper
  include OrbitControllerLib::DivisionForDisable
  
  before_filter :authenticate_user!
 
  before_filter :only => [ :new,:edit,:update] do |controller|
    controller.get_categorys('HonorCategory')
  end

  def index
    get_plugins

    # @tags = get_tags
    # @categories = get_categories_for_index
    # @statuses = get_statuses
    # category_ids = @categories.collect{|t| t.id}
    @honor_category_ids = HonorCategory.all.collect{|t| t.id.to_s} + [nil]

    @honors = get_sorted_and_filtered("honor", :honor_category_id.in => @honor_category_ids)
    respond_to do |format|
      format.html # index.html.erb
      format.js { }
      format.xml  { render :xml => @honors }
    end
  end

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

  def honor_category_quick_add    
    @set_honor_type = HonorCategory.new(:display => 'List')
    @honor_type_url = panel_personal_honor_back_end_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_back_end_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
  
    get_plugins
  
    @honor = Honor.new
    @honor_categorys = HonorCategory.all
	
    @tags = get_tags

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

  # GET /honors/1/edit
  def edit
  
    get_plugins
  
    @honor = Honor.find(params[:id])
	
    @honor_types = HonorCategory.all
	
    @tags = 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
	  @tags = get_tags
  
    @honor = Honor.new(params[:honor])
	
    @honor.create_user_id = current_user.id
    @honor.update_user_id = current_user.id
	
	  if params[:honor][:user_id]
      @honor.create_user_id = params[:honor][:user_id]
      @honor.update_user_id = params[:honor][:user_id]
    else
	    @honor.create_user_id = current_user.id
      @honor.update_user_id = current_user.id
    end

    respond_to do |format|
      if @honor.save
	  
		    if params[:honor][:user_id]
          format.html { redirect_to(admin_users_new_interface_url(:id=>params[:honor][:user_id],:show_plugin_profile=>"Honor")) }
        else
		      format.html { redirect_to(panel_personal_honor_back_end_honors_url) }
        end

        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_back_end_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_back_end_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_back_end_honors_url) }
      # format.xml  { head :ok }
      format.js
    end
  end

  def delete
    if params[:ids]
      honors = Honor.any_in(:_id => params[:ids]).destroy_all
    end
    redirect_to panel_personal_honor_back_end_honors_url(:direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options])
  end

  def data_share

    if params[:ids]
    
      @honors = Honor.any_in(:_id => params[:ids])
      
      @honors.each do |honor|

        honor.is_hidden = params[:disable]

        honor.save
      end

    end

    respond_to do |format|
    
      format.html { redirect_to(admin_users_new_interface_url(:id=>params[:user_id],:show_plugin_profile=>"Honor")) }
       format.json { render json: {"success"=>true}.to_json}
    end

  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_plugins  
    @plugins = OrbitApp::Plugin::Registration.all
  end
  
end
