class Panel::PersonalResearch::Desktop::PersonalResearchsController < ApplicationController

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

  def new
    @research = Research.new
    render :layout => false
  end

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

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

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

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

  def research_window
    render :layout => false
  end
end