class Panel::PersonalLab::Desktop::PersonalLabsController < ApplicationController

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

  def new
    @lab = Lab.new
    render :layout => false
  end

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

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

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

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

  def lab_window
    render :layout => false
  end
end