class CompetitionNoticesController < ApplicationController
  include ApplicationHelper
  before_action :find_competition, only: [:show, :index, :update, :destroy, :create]
  # before_action :require_admin, only: [:show, :index, :update, :destroy, :create]
  before_action :manager_competition_zones, only: [:enroll_list]

  def index
    competition_notices = CompetitionNotice.where(competition_info_id: @competition_info.id).order("created_at desc")
    if current_user.logged?
      competition_notices.each do |notice|
        CompetitionNoticeUser.find_or_create_by!(user_id: current_user.id, competition_notice_id: notice.id)
      end
    end
    @competition_notices_count = competition_notices.size
    @competition_notices = paginate(competition_notices)
  end

  def create
    @competition_notice = CompetitionNotice.new(competition_notice_params)
    @competition_notice.user = current_user
    @competition_notice.competition_info = @competition_info
    @competition_notice.save
    if params[:attachment_ids].present?
      params[:attachment_ids].each do |id|
        attachment = Attachment.where_id_or_uuid(id).first
        unless attachment.blank?
          attachment.container = @competition_notice
          attachment.author_id = current_user.id
          attachment.description = ""
          attachment.save
        end
      end
    end
    render_ok
  end

  # PATCH/PUT /competition_infos/1.json
  def update
    @competition_notice = CompetitionNotice.find params[:id]
    @competition_notice.update(competition_notice_params)
    @competition_notice.attachments.update_all(container_id: nil, container_type: nil)
    if params[:attachment_ids].present?
      params[:attachment_ids].each do |id|
        attachment = Attachment.where_id_or_uuid(id).first
        unless attachment.blank?
          attachment.container = @competition_notice
          attachment.author_id = current_user.id
          attachment.description = ""
          attachment.save
        end
      end
    end
    render_ok
  end

  def show
    @competition_notice = CompetitionNotice.find params[:id]
  end

  def upload
    competition_user = CompetitionUser.find_by(competition_info: @competition_notice.id, user_id: current_user.id)
    tip_exception "未报名，请先报名" if competition_user.blank?
    tip_exception "附件参数attachment_ids不能为空" if params[:attachment_ids].blank?
    # tip_exception "您已经提交作品!" if competition_user.present? && competition_user.attachments.present?
    competition_user.attachments.update_all(container_id: nil, container_type: nil)

    render_ok
  end

  def destroy
    @competition_notice = CompetitionNotice.find params[:id]
    @competition_notice.destroy
    render_ok
  end

  private

  def competition_notice_params
    params.permit(:title, :content, :description, :img_url)
  end

  def find_competition
    if CompetitionInfo.where(:identifier => params[:competition_info_id]).exists?
      @competition_info = CompetitionInfo.where(:identifier => params[:competition_info_id]).first
    else
      @competition_info = CompetitionInfo.find(params[:competition_info_id])
    end
  end

  def manager_competition_zones
    if current_user.admin?

    end
  end
end
