class Panel::PersonalConference::Desktop::ConferenceCoAuthorsController < ApplicationController
  def index
    page = params[:page]
    page ||= 1
    @conference_co_authors = ConferenceCoAuthor.where(name_id: current_user.id).asc(:co_author)
    @conference_co_author_relations = ConferenceCoAuthorRelation.all

    @conference_co_authors = @conference_co_authors.page(page).per(40)

    respond_to do |format|
      format.html { render :layout => false}
    end
  end

  def new
    @conference_co_author = ConferenceCoAuthor.new
    @conference_co_author_relations = ConferenceCoAuthorRelation.all

    respond_to do |format|
      format.html { render :layout => false}
    end
  end

  def edit
    @conference_co_author = ConferenceCoAuthor.find(params[:id])
    @conference_co_author_relations = ConferenceCoAuthorRelation.all
    respond_to do |format|
      format.html { render :layout => false}
    end
  end

  def create
    @conference_co_author = ConferenceCoAuthor.new(params[:conference_co_author])
    @conference_co_author.name_id= current_user.id

    if @conference_co_author.save
      render json: {success:true, msg: t('create_success')}.to_json
    else
      error_msg = @conference_co_author.errors.full_messages.join("<br />")
      render json: {success: false, msg: error_msg}.to_json
    end
  end

  def update
    @conference_co_author = ConferenceCoAuthor.find(params[:id])

    if @conference_co_author.update_attributes(params[:conference_co_author])
      render json: {success:true, msg: t('update_success')}.to_json
    else
      error_msg = @conference_co_author.errors.full_messages.join("<br />")
      render json: {success: false, msg: error_msg}.to_json
    end
  end

  def destroy
    @conference_co_author = ConferenceCoAuthor.find(params[:id])
    @conference_co_author.destroy

    render :json => {success: true, msg: "delete_success"}
  end
end
