class Panel::PersonalExperience::Desktop::PersonalExperiencesController < ApplicationController

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

  def new
    @experience = Experience.new
    # @author_types = BookAuthorType.all
    render :layout => false
  end

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

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

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

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

  def experience_window
    render :layout => false
  end
end