class Panel::PersonalJournal::Desktop::JournalPagesController < ApplicationController
  def index
    @writing_journals = WritingJournal.where(create_user_id: current_user.id)
    @level_types = JournalLevelType.all
    @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 = " " 
      @writing_journals = @writing_journals.asc(:paper_title)
    else
      @writing_journals = @writing_journals.asc(@view_by).asc(:paper_title)
    end

    @level_types = ConferencePaperType.all

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

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

  def new
    @writing_journal = WritingJournal.new
    @level_types = JournalLevelType.all
    @author_types = JournalAuthorType.all
    @paper_types= JournalPaperType.all

    @journal_candidate =
      WritingJournal.where(create_user_id: current_user.id).map{|j|j.journal_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 = JournalCoAuthor.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_journal = WritingJournal.find(params[:id])
    @level_types = JournalLevelType.all
    @author_types = JournalAuthorType.all
    @paper_types= JournalPaperType.all
    respond_to do |format|
      format.html { render :layout => false}
    end
  end

  def create
    params[:writing_journal][:create_user_id] = current_user.id
    @writing_journal = WritingJournal.new(params[:writing_journal])

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

  def update
    params[:writing_journal][:update_user_id] = current_user.id
    @writing_journal= WritingJournal.find(params[:id])

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

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

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

    level_types = JournalLevelType.all
    all_journal_lists = WritingJournal.where(create_user_id: current_user.id)
    all_journal_lists = all_journal_lists.asc(:journal_title)
    all_journal_lists = all_journal_lists.page(page).per(50)
    all_journal_lists = all_journal_lists.map do |j|
      [ j.journal_title,
        j.journal_level_type_ids.map do |type|
         level_types.find(type).title
       end]
    end

    @journal_lists = all_journal_lists

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