class Panel::PersonalPatent::BackEnd::WritingPatentsController < 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('WritingPatentCategory')
  end

  def index

  get_categorys("WritingPatentCategory",params[:writing_patent_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
  
  @writing_patent_categorys =  get_categories_for_index("WritingPatentCategory")
  @writing_patent_category_ids = @writing_patent_categorys.collect{|t| t.id.to_s} + [nil]

  
  @writing_patents = (params[:sort] || @filter) ? get_sorted_and_filtered("writing_patent",:writing_patent_category_id.in => @writing_patent_category_ids) : get_viewable("writing_patent",:writing_patent_category_id.in => @writing_patent_category_ids)

  get_tags

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

  def writing_patent_setting
  
    @patent_types = WritingPatentCategory.all
	
    get_tags
	
    @set_patent_type = WritingPatentCategory.new(:display => 'List')
	@patent_type_url = panel_personal_patent_back_end_writing_patents_path
	
	
  end

  def writing_patent_category_quick_add    
    @set_patent_type = WritingPatentCategory.new(:display => 'List')
    @patent_type_url = panel_personal_patent_back_end_writing_patents_path
    @set_patent_type.id = params[:id]
	
    respond_to do |format|
      format.js
    end
	
  end

  def writing_patent_category_quick_edit
  
    @set_patent_type = WritingPatentCategory.find(params[:writing_patent_id])
    @patent_type_url = panel_personal_patent_back_end_writing_patent_path(@set_patent_type)
	
    respond_to do |format|
      format.js
    end
  end

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

  # GET /writing_patents/new
  # GET /writing_patents/new.xml
  def new
  
    @writing_patent = WritingPatent.new
    @writing_patent_categorys = WritingPatentCategory.all
	
    get_tags

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

  # GET /writing_patents/1/edit
  def edit
    @writing_patent = WritingPatent.find(params[:id])
	
    @patent_types = WritingPatentCategory.all
	
    get_tags
  end

  # POST /writing_patents
  # POST /writing_patents.xml
  def create
  
  if params[:writing_patent_category]
		
		@writing_patent_category = WritingPatentCategory.new(params[:writing_patent_category])

		respond_to do |format|
		  if @writing_patent_category.save
			format.js { render 'create_writing_patent_setting' }
		  end
		end
	
  else
	
    @patent_types = WritingPatentCategory.all
	get_tags
  
    @writing_patent = WritingPatent.new(params[:writing_patent])
	
	if params[:writing_patent][:user_id]
      @writing_patent.create_user_id = params[:writing_patent][:user_id]
      @writing_patent.update_user_id = params[:writing_patent][:user_id]
    else
	  @writing_patent.create_user_id = current_user.id
      @writing_patent.update_user_id = current_user.id
    end
	
    respond_to do |format|
      if @writing_patent.save
	  
		if params[:writing_patent][:user_id]
          format.html { redirect_to(admin_users_new_interface_url(:id=>params[:writing_patent][:user_id],:show_plugin_profile=>'Patent')) }
        else
		  format.html { redirect_to(panel_personal_patent_back_end_writing_patents_url) }
        end

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

  # PUT /writing_patents/1
  # PUT /writing_patents/1.xml
  def update
  
    if params[:writing_patent_category]
  	
  		@writing_patent_category = WritingPatentCategory.find(params[:id])

  		respond_to do |format|
            
  		  if @writing_patent_category.update_attributes(params[:writing_patent_category])
  			# format.html { redirect_to(panel_announcement_back_end_bulletins_url) }
  			format.js { render 'update_writing_patent_setting' }
  		  end
  		end
		
  	else
	
    @writing_patent = WritingPatent.find(params[:id])
	
    @writing_patent.update_user_id = current_user.id
	
	params[:writing_patent][:tag_ids] ||=[]

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

  # DELETE /writing_patents/1
  # DELETE /writing_patents/1.xml
  def destroy
    @writing_patent = WritingPatent.find(params[:id])
    @writing_patent.destroy

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

  def delete
    if params[:ids]
      writing_patents = WritingPatent.any_in(:_id => params[:ids]).delete_all
    end
    redirect_to panel_personal_patent_back_end_writing_patents_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)
  #   @writing_patent_categorys = []
  #   if(is_manager? || is_admin?)
  #     @writing_patent_categorys = (id ? WritingPatentCategory.admin_manager_all.find(id).to_a : WritingPatentCategory.admin_manager_all))
  #   elsif is_sub_manager?
  #     @writing_patent_categorys = WritingPatentCategory.all.authed_for_user(current_user,'edit')
  #   end
  #   if @writing_patent_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_patent'})
  	@tags = Tag.all(:conditions => {:module_app_id => module_app.id})
  end
  
end
