class Panel::PersonalHonor::Desktop::PersonalHonorsController < ApplicationController

	def index
    @honors = Honor.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 = " " 
      @honors = @honors.asc(:paper_title)
    else
      @honors = @honors.asc(@view_by).asc(:paper_title)
    end
    @honors = @honors.page(page).per(50)
		respond_to do |format|
    	format.html { render :layout => false}
    end
	end

  def new
    @honor = Honor.new
    render :layout => false
  end

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

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

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

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

  def honors_window
    render :layout => false
  end
end