class Panel::PersonalSeminar::BackEnd::WritingSeminarsController < 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 :only => [ :new,:edit,:update] do |controller|
    controller.get_categorys('WritingSeminarCategory')
  end

  def index

  get_categorys("SeminarPaperType",params[:seminar_paper_type_id])
  @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
  
  @paper_types =  SeminarPaperType.all
  @paper_type_ids = @paper_types.collect{|t| t.id.to_s} + [nil]
  

	# @writing_seminars = WritingSeminar.search(params[:category_id])
	#@writing_seminars = (params[:sort] || @filter) ? get_sorted_and_filtered_writing_seminars : WritingSeminar.all.page(params[:page]).per(10)

    @writing_seminars = (params[:sort] || @filter) ? get_sorted_and_filtered("writing_seminar",:seminar_paper_type_id.in => @paper_type_ids) : get_viewable("writing_seminar",:seminar_paper_type_id.in => @paper_type_ids)


  get_tags

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

  def writing_seminar_setting
  
    @author_types = SeminarAuthorType.all
    @paper_types = SeminarPaperType.all
	
    get_tags
	
    @set_author_type = SeminarAuthorType.new(:display => 'List')
	@author_type_url = panel_personal_seminar_back_end_writing_seminars_path
	
    @set_paper_type = SeminarPaperType.new(:display => 'List')
	@paper_type_url = panel_personal_seminar_back_end_writing_seminars_path
	
	
  end

  def paper_type_quick_add    
    @set_paper_type = SeminarPaperType.new(:display => 'List')
    @paper_type_url = panel_personal_seminar_back_end_writing_seminars_path
    @set_paper_type.id = params[:id]
	
    respond_to do |format|
      format.js
    end
	
  end

  def paper_type_quick_edit
  
    @set_paper_type = SeminarPaperType.find(params[:writing_seminar_id])
    @paper_type_url = panel_personal_seminar_back_end_writing_seminar_path(@set_paper_type)
	
    respond_to do |format|
      format.js
    end
  end

  def author_type_quick_add    
    @set_author_type = SeminarAuthorType.new(:display => 'List')
    @author_type_url = panel_personal_seminar_back_end_writing_seminars_path
    @set_author_type.id = params[:id]
	
    respond_to do |format|
      format.js
    end
	
  end

  def author_type_quick_edit
  
    @set_author_type = SeminarAuthorType.find(params[:writing_seminar_id])
    @author_type_url = panel_personal_seminar_back_end_writing_seminar_path(@set_author_type)
	
    respond_to do |format|
      format.js
    end
  end

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

  # GET /writing_seminars/new
  # GET /writing_seminars/new.xml
  def new
  
    @writing_seminar = WritingSeminar.new
    # @writing_seminar_files = WritingSeminarFile.all
    # @paper_types = SeminarLevelType.all
    @author_types = SeminarAuthorType.all
    @paper_types = SeminarPaperType.all
	
    get_tags

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

  # GET /writing_seminars/1/edit
  def edit
    @writing_seminar = WritingSeminar.find(params[:id])
	
    # @paper_types = SeminarLevelType.all
    @author_types = SeminarAuthorType.all
    @paper_types = SeminarPaperType.all
	
    get_tags
  end

  # POST /writing_seminars
  # POST /writing_seminars.xml
  def create
  
  if params[:seminar_paper_type]
		
		@seminar_paper_type = SeminarPaperType.new(params[:seminar_paper_type])

		respond_to do |format|
		  if @seminar_paper_type.save
			format.js { render 'create_writing_seminar_setting' }
		  end
		end
  
   elsif params[:seminar_author_type]
		
		@seminar_author_type = SeminarAuthorType.new(params[:seminar_author_type])

		respond_to do |format|
		  if @seminar_author_type.save
			format.js { render 'create_writing_seminar_setting' }
		  end
		end
	
  else
	
    # @paper_types = SeminarLevelType.all
    @author_types = SeminarAuthorType.all
    @paper_types = SeminarPaperType.all
	get_tags
  
    @writing_seminar = WritingSeminar.new(params[:writing_seminar])
	
	if params[:writing_seminar][:user_id]
      @writing_seminar.create_user_id = params[:writing_seminar][:user_id]
      @writing_seminar.update_user_id = params[:writing_seminar][:user_id]
    else
	  @writing_seminar.create_user_id = current_user.id
      @writing_seminar.update_user_id = current_user.id
    end

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

  end

  # PUT /writing_seminars/1
  # PUT /writing_seminars/1.xml
  def update
  
	if params[:seminar_paper_type]
  	
  		@seminar_paper_type = SeminarPaperType.find(params[:id])

  		respond_to do |format|
            
  		  if @seminar_paper_type.update_attributes(params[:seminar_paper_type])
  			# format.html { redirect_to(panel_announcement_back_end_bulletins_url) }
  			format.js { render 'update_writing_seminar_setting' }
  		  end
  		end
		
	elsif params[:seminar_author_type]
  	
  		@seminar_author_type = SeminarAuthorType.find(params[:id])

  		respond_to do |format|
            
  		  if @seminar_author_type.update_attributes(params[:seminar_author_type])
  			# format.html { redirect_to(panel_announcement_back_end_bulletins_url) }
  			format.js { render 'update_writing_seminar_setting' }
  		  end
  		end
		
  	else
	
    @writing_seminar = WritingSeminar.find(params[:id])
	
    @writing_seminar.update_user_id = current_user.id
	
	params[:writing_seminar][:tag_ids] ||=[]

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

  # DELETE /writing_seminars/1
  # DELETE /writing_seminars/1.xml
  def destroy
    @writing_seminar = WritingSeminar.find(params[:id])
    @writing_seminar.destroy

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

  def delete
    if params[:ids]
      writing_seminars = WritingSeminar.any_in(:_id => params[:ids]).delete_all
    end
    redirect_to panel_personal_seminar_back_end_writing_seminars_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_seminar_categorys = []
  #   if(is_manager? || is_admin?)
  #     @writing_seminar_categorys = (id ? WritingSeminarCategory.admin_manager_all.find(id).to_a : WritingSeminarCategory.admin_manager_all))
  #   elsif is_sub_manager?
  #     @writing_seminar_categorys = WritingSeminarCategory.all.authed_for_user(current_user,'edit')
  #   end
  #   if @writing_seminar_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_seminar'})
  	@tags = Tag.all(:conditions => {:module_app_id => module_app.id})
  end
  
end
