class Panel::PersonalJournal::BackEnd::WritingJournalsController < 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('WritingJournalCategory')
  end

  def index

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

	# @writing_journals = WritingJournal.search(params[:category_id])
	#@writing_journals = (params[:sort] || @filter) ? get_sorted_and_filtered_writing_journals : WritingJournal.all.page(params[:page]).per(10)
    @writing_journals = (params[:sort] || @filter) ? get_sorted_and_filtered("writing_journal",:journal_paper_type_id.in => @paper_type_ids) : get_viewable("writing_journal",:journal_paper_type_id.in => @paper_type_ids)

  get_tags

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

  def writing_journal_setting
  
    @plugins = ModuleApp.where(has_plugin: true).order_by(:title, :asc)
  
    @level_types = JournalLevelType.all
    @author_types = JournalAuthorType.all
    @paper_types = JournalPaperType.all
	
    get_tags
	
    # @writing_journal_categorys =  JournalLevelType.all
    @set_level_type = JournalLevelType.new(:display => 'List')
	@level_type_url = panel_personal_journal_back_end_writing_journals_path
	
    @set_author_type = JournalAuthorType.new(:display => 'List')
	@author_type_url = panel_personal_journal_back_end_writing_journals_path
	
    @set_paper_type = JournalPaperType.new(:display => 'List')
	@paper_type_url = panel_personal_journal_back_end_writing_journals_path
	
	
  end

  def level_type_quick_add    
    @set_level_type = JournalLevelType.new(:display => 'List')
    @level_type_url = panel_personal_journal_back_end_writing_journals_path
    @set_level_type.id = params[:id]
	
    respond_to do |format|
      format.js
    end
	
  end

  def level_type_quick_edit
  
    @set_level_type = JournalLevelType.find(params[:writing_journal_id])
    @level_type_url = panel_personal_journal_back_end_writing_journal_path(@set_level_type)
	
    respond_to do |format|
      format.js
    end
  end

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

  def author_type_quick_edit
  
    @set_author_type = JournalAuthorType.find(params[:writing_journal_id])
    @author_type_url = panel_personal_journal_back_end_writing_journal_path(@set_author_type)
	
    respond_to do |format|
      format.js
    end
  end

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

  def paper_type_quick_edit
  
    @set_paper_type = JournalPaperType.find(params[:writing_journal_id])
    @paper_type_url = panel_personal_journal_back_end_writing_journal_path(@set_paper_type)
	
    respond_to do |format|
      format.js
    end
  end

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

  # GET /writing_journals/new
  # GET /writing_journals/new.xml
  def new
  
    @writing_journal = WritingJournal.new
    # @writing_journal_files = WritingJournalFile.all
    @level_types = JournalLevelType.all
    @author_types = JournalAuthorType.all
    @paper_types = JournalPaperType.all
	
    get_tags

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

  # GET /writing_journals/1/edit
  def edit
    @writing_journal = WritingJournal.find(params[:id])
	
    @level_types = JournalLevelType.all
    @author_types = JournalAuthorType.all
    @paper_types = JournalPaperType.all
	
    get_tags
  end

  # POST /writing_journals
  # POST /writing_journals.xml
  def create
  
  if params[:journal_level_type]
		
		@journal_level_type = JournalLevelType.new(params[:journal_level_type])

		respond_to do |format|
		  if @journal_level_type.save
			format.js { render 'create_writing_journal_setting' }
		  end
		end
  
   elsif params[:journal_author_type]
		
		@journal_author_type = JournalAuthorType.new(params[:journal_author_type])

		respond_to do |format|
		  if @journal_author_type.save
			format.js { render 'create_writing_journal_setting' }
		  end
		end
	
  
   elsif params[:journal_paper_type]
		
		@journal_paper_type = JournalPaperType.new(params[:journal_paper_type])

		respond_to do |format|
		  if @journal_paper_type.save
			format.js { render 'create_writing_journal_setting' }
		  end
		end
	
  else
	
    @level_types = JournalLevelType.all
    @author_types = JournalAuthorType.all
    @paper_types = JournalPaperType.all
	get_tags
  
    @writing_journal = WritingJournal.new(params[:writing_journal])
	
	if params[:writing_journal][:user_id]
      @writing_journal.create_user_id = params[:writing_journal][:user_id]
      @writing_journal.update_user_id = params[:writing_journal][:user_id]
    else
	  @writing_journal.create_user_id = current_user.id
      @writing_journal.update_user_id = current_user.id
    end
	
    respond_to do |format|
      if @writing_journal.save
	  
		if params[:writing_journal][:user_id]
          format.html { redirect_to(admin_users_new_interface_url(:id=>params[:writing_journal][:user_id],:show_plugin_profile=>Journal)) }
        else
		  format.html { redirect_to(panel_personal_journal_back_end_writing_journals_url) }
        end
		
		format.xml  { render :xml => @writing_journal, :status => :created, :location => @writing_journal }
		
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @writing_journal.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  end

  # PUT /writing_journals/1
  # PUT /writing_journals/1.xml
  def update
  
	if params[:journal_level_type]
  	
  		@journal_level_type = JournalLevelType.find(params[:id])

  		respond_to do |format|
            
  		  if @journal_level_type.update_attributes(params[:journal_level_type])
  			# format.html { redirect_to(panel_announcement_back_end_bulletins_url) }
  			format.js { render 'update_writing_journal_setting' }
  		  end
  		end
		
	elsif params[:journal_author_type]
  	
  		@journal_author_type = JournalAuthorType.find(params[:id])

  		respond_to do |format|
            
  		  if @journal_author_type.update_attributes(params[:journal_author_type])
  			# format.html { redirect_to(panel_announcement_back_end_bulletins_url) }
  			format.js { render 'update_writing_journal_setting' }
  		  end
  		end
		
	elsif params[:journal_paper_type]
  	
  		@journal_paper_type = JournalPaperType.find(params[:id])

  		respond_to do |format|
            
  		  if @journal_paper_type.update_attributes(params[:journal_paper_type])
  			# format.html { redirect_to(panel_announcement_back_end_bulletins_url) }
  			format.js { render 'update_writing_journal_setting' }
  		  end
  		end
		
  	else
	
	
      @writing_journal = WritingJournal.find(params[:id])
	
      @writing_journal.update_user_id = current_user.id
	
	  params[:writing_journal][:tag_ids] ||=[]

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

  # DELETE /writing_journals/1
  # DELETE /writing_journals/1.xml
  def destroy
    @writing_journal = WritingJournal.find(params[:id])
    @writing_journal.destroy

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

  def delete
    if params[:ids]
      writing_journals = WritingJournal.any_in(:_id => params[:ids]).delete_all
    end
    redirect_to panel_personal_journal_back_end_writing_journals_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_journal_categorys = []
  #   if(is_manager? || is_admin?)
  #     @writing_journal_categorys = (id ? WritingJournalCategory.admin_manager_all.find(id).to_a : WritingJournalCategory.admin_manager_all))
  #   elsif is_sub_manager?
  #     @writing_journal_categorys = WritingJournalCategory.all.authed_for_user(current_user,'edit')
  #   end
  #   if @writing_journal_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_journal'})
  	@tags = Tag.all(:conditions => {:module_app_id => module_app.id})
  end
  
end
