class Panel::PersonalResearch::BackEnd::ResearchsController < 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}

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

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

  # GET /researchs/new
  # GET /researchs/new.xml
  def new
  
    get_plugins
  
    @research = Research.new
	
    @tags = get_tags

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

  # GET /researchs/1/edit
  def edit
  
    get_plugins
	
    @research = Research.find(params[:id])
	
    @tags = get_tags
  end

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

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

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

  # PUT /researchs/1
  # PUT /researchs/1.xml
  def update	
	
      @research = Research.find(params[:id])
	
      @research.update_user_id = current_user.id
	
  	  params[:research][:tag_ids] ||=[]

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

  # DELETE /researchs/1
  # DELETE /researchs/1.xml
  def destroy
    @research = Research.find(params[:id])
    @research.destroy

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

  def delete
    if params[:ids]
      researchs = Research.any_in(:_id => params[:ids]).destroy_all
    end
    redirect_to panel_personal_research_back_end_researchs_url(:direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options])
  end

  def data_share

    if params[:ids]
    
      @researchs = Research.any_in(:_id => params[:ids])
      
      @researchs.each do |research|

        research.is_hidden = params[:disable]

        research.save
      end

    end

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