class Panel::PersonalProject::Desktop::PersonalProjectsController < ApplicationController

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

  def new
    @project = Project.new
    render :layout => false
  end

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

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

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

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

  def project_window
    render :layout => false
  end
end