class Panel::PersonalProject::BackEnd::ProjectsController < 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('ProjectCategory')
  end

  def index
    get_plugins

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

    @projects = get_sorted_and_filtered("project", :project_category_id.in => @project_category_ids)
    respond_to do |format|
      format.html # index.html.erb
      format.js { }
      format.xml  { render :xml => @projects }
    end
  end

  def project_setting
  
    get_plugins
  
    @project_categorys = ProjectCategory.all
	
    @tags = get_tags
	
    @set_project_type = ProjectCategory.new(:display => 'List')
  	@project_type_url = panel_personal_project_back_end_projects_path
	
	
  end

  def project_category_quick_add    
    @set_project_type = ProjectCategory.new(:display => 'List')
    @project_type_url = panel_personal_project_back_end_projects_path
    @set_project_type.id = params[:id]
	
    respond_to do |format|
      format.js
    end
	
  end

  def project_category_quick_edit
  
    @set_project_type = ProjectCategory.find(params[:project_id])
    @project_type_url = panel_personal_project_back_end_project_path(@set_project_type)
	
    respond_to do |format|
      format.js
    end
  end

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

  # GET /projects/new
  # GET /projects/new.xml
  def new
  
    get_plugins
  
    @project = Project.new
    @project_categorys = ProjectCategory.all
	
    @tags = get_tags

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

  # GET /projects/1/edit
  def edit
  
    get_plugins
	
    @project = Project.find(params[:id])
	
    @project_types = ProjectCategory.all
	
    @tags = get_tags
  end

  # POST /projects
  # POST /projects.xml
  def create
  
    if params[:project_category]
  		
  		@project_category = ProjectCategory.new(params[:project_category])

  		respond_to do |format|
  		  if @project_category.save
  			format.js { render 'create_project_setting' }
  		  end
  		end
  	
    else
  	
      @project_types = ProjectCategory.all
  	  @tags = get_tags
    
      @project = Project.new(params[:project])
  	
  	  if params[:project][:user_id]
        @project.create_user_id = params[:project][:user_id]
        @project.update_user_id = params[:project][:user_id]
      else
  	    @project.create_user_id = current_user.id
        @project.update_user_id = current_user.id
      end

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

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

  # PUT /projects/1
  # PUT /projects/1.xml
  def update
  
    if params[:project_category]
  	
  		@project_category = ProjectCategory.find(params[:id])

  		respond_to do |format|
            
  		  if @project_category.update_attributes(params[:project_category])
  			# format.html { redirect_to(panel_announcement_back_end_bulletins_url) }
  			format.js { render 'update_project_setting' }
  		  end
  		end
		
  	else
	
    @project = Project.find(params[:id])
	
    @project.update_user_id = current_user.id
	
  	params[:project][:tag_ids] ||=[]

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

  # DELETE /projects/1
  # DELETE /projects/1.xml
  def destroy
    @project = Project.find(params[:id])
    @project.destroy

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

  def delete
    if params[:ids]
      projects = Project.any_in(:_id => params[:ids]).destroy_all
    end
    redirect_to panel_personal_project_back_end_projects_url(:direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options])
  end

  def data_share

    if params[:ids]
    
      @projects = Project.any_in(:_id => params[:ids])
      
      @projects.each do |project|

        project.is_hidden = params[:disable]

        project.save
      end

    end

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