class Panel::PersonalConference::Desktop::ConferencePagesController < ApplicationController
  def index
    @view_by = params[:view]
    @writing_conferences = WritingConference.where(create_user_id: current_user.id)
    page = params[:page]
    page ||= 1

    if @view_by.nil?
      @writing_conferences = @writing_conferences.asc(:paper_title)
    else
      @writing_conferences = @writing_conferences.asc(@view_by).asc(:paper_title)
    end

    @level_types = ConferencePaperType.all

    @writing_conferences = @writing_conferences.page(page).per(50)

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

  def show
  end

  def new
    @writing_conference = WritingConference.new
    @paper_types = ConferencePaperType.all
    @author_types = ConferenceAuthorType.all

    @conference_candidate =
      WritingConference.where(create_user_id: current_user.id).map{|j|j.conference_title}.uniq

    if (not params[:q].nil?) and (current_user.name.include?params[:q])
      @user = [{ :id => 0, :text => current_user.name, :email => current_user.email}]       # self account name
    else
      @user = []
    end

    @co_authors = ConferenceCoAuthor.where(name_id: current_user.id, :co_author => /#{params[:q]}/).asc(:co_author)
    # search string + self account name + match pattern
    @co_authors = [{ :id => params[:q], :text => params[:q], :email => "#{t("add")} #{t("author")}" }] +
                  @user +
                  @co_authors.map{|m| { :id => m.id, :text => m.co_author, :email => m.email } }

    respond_to do |format|
      format.html { render :layout => false}
      format.json { render :json => {:results => @co_authors}.to_json }
    end
  end

  def edit
    @writing_conference = WritingConference.find(params[:id])
    @paper_types = ConferencePaperType.all
    @author_types = ConferenceAuthorType.all
    respond_to do |format|
      format.html { render :layout => false}
    end
  end

  def create
    params[:writing_conference][:create_user_id] = current_user.id
    @writing_conference = WritingConference.new(params[:writing_conference])

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

  def update
    params[:writing_conference][:update_user_id] = current_user.id
    @writing_conference= WritingConference.find(params[:id])

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

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

  def conference_type
    page = params[:page]
    page ||= 1

    conference_types = ConferencePaperType.all
    all_conference_lists = WritingConference.where(create_user_id: current_user.id)
    all_conference_lists = all_conference_lists.asc(:conference_title)
    all_conference_lists = all_conference_lists.page(page).per(50)
    all_conference_lists = all_conference_lists.map do |j|
      [ j.conference_title,
        j.conference_paper_type_ids.map do |type|
          conference_types.find(type).title
        end
      ]
    end

    @conference_lists = all_conference_lists

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

