class Panel::PersonalBook::BackEnd::WritingBooksController < OrbitBackendController
  include AdminHelper
  include OrbitControllerLib::DivisionForDisable
  
  before_filter :authenticate_user!

  before_filter :only => [ :new,:edit,:update] do |controller|
    controller.get_categorys('WritingBookCategory')
  end

  def index
    get_plugins

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

    @writing_books = get_sorted_and_filtered("writing_book", :book_paper_type_id.in => @paper_type_ids)
    respond_to do |format|
      format.html # index.html.erb
      format.js { }
      format.xml  { render :xml => @writing_books }
    end
  end

  def writing_book_setting
  
    get_plugins
  
    @author_types = BookAuthorType.all
    @paper_types = BookPaperType.all
	
    @tags = get_tags
	
    @set_author_type = BookAuthorType.new(:display => 'List')
  	@author_type_url = panel_personal_book_back_end_writing_books_path
	
    @set_paper_type = BookPaperType.new(:display => 'List')
  	@paper_type_url = panel_personal_book_back_end_writing_books_path
	
	
  end

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

  def author_type_quick_edit
  
    @set_author_type = BookAuthorType.find(params[:writing_book_id])
    @author_type_url = panel_personal_book_back_end_writing_book_path(@set_author_type)
	
    respond_to do |format|
      format.js
    end
  end

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

  def paper_type_quick_edit
  
    @set_paper_type = BookPaperType.find(params[:writing_book_id])
    @paper_type_url = panel_personal_book_back_end_writing_book_path(@set_paper_type)
	
    respond_to do |format|
      format.js
    end
  end

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

  # GET /writing_books/new
  # GET /writing_books/new.xml
  def new
  
    get_plugins
  
    @writing_book = WritingBook.new
    # @writing_book_files = WritingBookFile.all
    # @level_types = BookLevelType.all
    @author_types = BookAuthorType.all
    @paper_types = BookPaperType.all
	
    @tags = get_tags

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

  # GET /writing_books/1/edit
  def edit
  
    get_plugins
	
    @writing_book = WritingBook.find(params[:id])
	
    # @level_types = BookLevelType.all
    @author_types = BookAuthorType.all
    @paper_types = BookPaperType.all
	
    @tags = get_tags
  end

  # POST /writing_books
  # POST /writing_books.xml
  def create
  
   if params[:book_author_type]
		
		@book_author_type = BookAuthorType.new(params[:book_author_type])

		respond_to do |format|
		  if @book_author_type.save
			format.js { render 'create_writing_book_setting' }
		  end
		end
	
  
   elsif params[:book_paper_type]
		
		@book_paper_type = BookPaperType.new(params[:book_paper_type])

		respond_to do |format|
		  if @book_paper_type.save
			format.js { render 'create_writing_book_setting' }
		  end
		end
	
  else
	
    # @level_types = BookLevelType.all
    @author_types = BookAuthorType.all
    @paper_types = BookPaperType.all
	  @tags = get_tags
  
    @writing_book = WritingBook.new(params[:writing_book])
	
	  if params[:writing_book][:user_id]
      @writing_book.create_user_id = params[:writing_book][:user_id]
      @writing_book.update_user_id = params[:writing_book][:user_id]
    else
	    @writing_book.create_user_id = current_user.id
      @writing_book.update_user_id = current_user.id
    end

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

    end

  end
  
  end

  # PUT /writing_books/1
  # PUT /writing_books/1.xml
  def update
  
    if params[:book_author_type]
  	
  		@book_author_type = BookAuthorType.find(params[:id])

  		respond_to do |format|
            
  		  if @book_author_type.update_attributes(params[:book_author_type])
  			# format.html { redirect_to(panel_announcement_back_end_bulletins_url) }
  			format.js { render 'update_writing_book_setting' }
  		  end
  		end
		
	elsif params[:book_paper_type]
  	
  		@book_paper_type = BookPaperType.find(params[:id])

  		respond_to do |format|
            
  		  if @book_paper_type.update_attributes(params[:book_paper_type])
  			# format.html { redirect_to(panel_announcement_back_end_bulletins_url) }
  			format.js { render 'update_writing_book_setting' }
  		  end
  		end
		
  	else
	
      @writing_book = WritingBook.find(params[:id])
  	
      @writing_book.update_user_id = current_user.id
  	
  	  params[:writing_book][:tag_ids] ||=[]

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

    end
  
  end

  # DELETE /writing_books/1
  # DELETE /writing_books/1.xml
  def destroy
    @writing_book = WritingBook.find(params[:id])
    @writing_book.destroy

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

  def delete
    if params[:ids]
      writing_books = WritingBook.any_in(:_id => params[:ids]).destroy_all
    end
    redirect_to panel_personal_book_back_end_writing_books_url(:direction => params[:direction], :sort => params[:sort], :sort_options => params[:sort_options])
  end

  def data_share

    if params[:ids]
    
      @writing_books = WritingBook.any_in(:_id => params[:ids])
      
      @writing_books.each do |writing_book|

        writing_book.is_hidden = params[:disable]

        writing_book.save
      end

    end

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