class Panel::PersonalPatent::Desktop::PersonalPatentsController < ApplicationController

	def index
    @patents = WritingPatent.where(create_user_id: current_user.id)
    @view_by = params[:view]
    page = params[:page]
    page ||= 1

    @per_column = 5

    case @view_by
    when "abstract"
      @per_column = 1
    when "file"
      @per_column = 2
    end

    if @view_by.nil?
      @view_by = " " 
      @patents = @patents.asc(:paper_title)
    else
      @patents = @patents.asc(@view_by).asc(:paper_title)
    end
    @patents = @patents.page(page).per(50)
		respond_to do |format|
    	format.html { render :layout => false}
    end
	end

  def new
    @patent = WritingPatent.new
    render :layout => false
  end

  def create
    params[:writing_patent][:create_user_id] = current_user.id
    @patent = WritingPatent.new(params[:writing_patent])
    if @patent.save
      render json: {success: true, msg: t('create_success')}.to_json
    else
      error_msg = @patent.errors.full_messages.join("<br />")
      render json: {success: false, msg: error_msg}.to_json
    end
  end

  def edit
    @patent = WritingPatent.find(params[:id])
    render :layout => false
  end

  def update
    params[:writing_patent][:create_user_id] = current_user.id
    @patent = WritingPatent.find(params[:id])
    if @patent.update_attributes(params[:writing_patent])
      render json: {success: true, msg: t('create_success')}.to_json
    else
      error_msg = @patent.errors.full_messages.join("<br />")
      render json: {success: false, msg: error_msg}.to_json
    end
  end

  def destroy 
    @patent = WritingPatent.find(params[:id])
    @patent.destroy
    render :json => {success: true, msg: t('delete_success')}
  end

  def patent_window
    render :layout => false
  end
end