class Panel::PersonalDiploma::BackEnd::DiplomasController < OrbitBackendController
  include AdminHelper
  include OrbitControllerLib::DivisionForDisable
  
  before_filter :authenticate_user!

  def index
    get_plugins

    # @tags = get_tags
    # @categories = get_categories_for_index
    # @statuses = get_statuses
    # category_ids = @categories.collect{|t| t.id}

    @diplomas = get_sorted_and_filtered("diploma")
    respond_to do |format|
      format.html # index.html.erb
      format.js { }
      format.xml  { render :xml => @diplomas }
    end
  end

  def diploma_setting
  
    get_plugins
  	
    @tags = get_tags
	
  end

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

  # GET /diplomas/new
  # GET /diplomas/new.xml
  def new
  
    get_plugins
  
    @diploma = Diploma.new 
	
    @tags = get_tags

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

  # GET /diplomas/1/edit
  def edit
  
    get_plugins
	
    @diploma = Diploma.find(params[:id]) 
	
    @tags = get_tags
  end

  # POST /diplomas
  # POST /diplomas.xml
  def create
  
  	@tags = get_tags
  
    @diploma = Diploma.new(params[:diploma])
	
  	if params[:diploma][:user_id]
      @diploma.create_user_id = params[:diploma][:user_id]
      @diploma.update_user_id = params[:diploma][:user_id]
    else
  	  @diploma.create_user_id = current_user.id
      @diploma.update_user_id = current_user.id
    end

    respond_to do |format|
      if @diploma.save
	  
    		if params[:diploma][:user_id]
          format.html { redirect_to(admin_users_new_interface_url(:id=>params[:diploma][:user_id],:show_plugin_profile=>'Diploma')) }
        else
    		  format.html { redirect_to(panel_personal_diploma_back_end_diplomas_url) }
        end
		
        format.xml  { render :xml => @diploma, :status => :created, :location => @diploma }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @diploma.errors, :status => :unprocessable_entity }
      end
    end

  end
  
  # PUT /diplomas/1
  # PUT /diplomas/1.xml
  def update
  
    @diploma = Diploma.find(params[:id])
	
    @diploma.update_user_id = current_user.id
	
  	params[:diploma][:tag_ids] ||=[]

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

  end
  
  # DELETE /diplomas/1
  # DELETE /diplomas/1.xml
  def destroy
    @diploma = Diploma.find(params[:id])
    @diploma.destroy

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

  def delete
    if params[:ids]
      diplomas = Diploma.any_in(:_id => params[:ids]).destroy_all
    end
    redirect_to panel_personal_diploma_back_end_diplomas_url(:direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options])
  end

  def data_share

    if params[:ids]
    
      @diplomas = Diploma.any_in(:_id => params[:ids])
      
      @diplomas.each do |diploma|

        diploma.is_hidden = params[:disable]

        diploma.save
      end

    end

    respond_to do |format|
    
      format.html { redirect_to(admin_users_new_interface_url(:id=>params[:user_id],:show_plugin_profile=>"Diploma")) }
       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)
  #   @diploma_categorys = []
  #   if(is_manager? || is_admin?)
  #     @diploma_categorys = (id ? DiplomaCategory.admin_manager_all.find(id).to_a : DiplomaCategory.admin_manager_all))
  #   elsif is_sub_manager?
  #     @diploma_categorys = DiplomaCategory.all.authed_for_user(current_user,'edit')
  #   end
  #   if @diploma_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
