class ItemBanksController < ApplicationController
  include PaginateHelper
  before_action :require_login
  #before_action :certi_identity_auth, only: [:destroy]
  before_action :find_item, except: [:index, :create]
  before_action :edit_auth, only: [:update, :set_public, :set_private, :destroy, :edit]
  before_action :check_account, only: [:index]
  #before_action :identity_auth, only: [:index]

  def index
    items = ItemBankQuery.call(params)
    @items_count = items.size
    @items = paginate items.includes(:item_analysis, :user, :container)
    exam = ExaminationBank.find_by(id: params[:exam_id]) if params[:exam_id].present?
    exam_setting = ExaminationIntelligentSetting.find_by(id: params[:exam_setting_id]) if params[:exam_setting_id].present?
    @item_basket_ids = if exam
                         exam.examination_items.pluck(:item_bank_id)
                       elsif exam_setting
                         exam_setting.item_baskets.pluck(:item_bank_id)
                       else
                         current_user.item_baskets.pluck(:item_bank_id)
                       end
    @shixun_manager = current_user.admin_or_business?
    @is_cer_teacher = current_user.is_certification_teacher
  end

  def create
    item_bank = ItemBank.new(user: current_user)
    ItemBanks::SaveItemService.call(item_bank, form_params)
    render_ok
  rescue ApplicationService::Error => ex
    render_error(ex.message)
  end

  def edit

  end

  def update
    ItemBanks::SaveItemService.call(@item, form_params)
    render_ok
  rescue ApplicationService::Error => ex
    render_error(ex.message)
  end

  def destroy
    tip_exception(403, "无权限") unless current_user.admin? || @item.user == current_user
    ActiveRecord::Base.transaction do
      ApplyAction.where(container_type: "ItemBank", container_id: @item.id).destroy_all
      if @item.item_type == "PROGRAM"
        @item.container&.destroy!
      else
        @item.destroy!
      end
      render_ok
    end
  end

  def set_public
    tip_exception(-1, "该试题已公开") if @item.public?
    tip_exception(-1, "请勿重复提交申请") if ApplyAction.where(container_id: @item.id, container_type: 'ItemBank', status: 0).exists?
    ApplyAction.create!(container_id: @item.id, container_type: 'ItemBank', user_id: current_user.id)
    # @item.update_attributes!(public: 1)
    render_ok
  end

  def set_private
    tip_exception(-1, "该试题已私有") unless @item.public?
    ActiveRecord::Base.transaction do
      @item.update_attributes!(public: 0)
      ApplyAction.where(container_id: @item.id, container_type: 'ItemBank', status: 0).update_all(status: 3)
    end
    render_ok
  end

  private

  def find_item
    @item = ItemBank.find_by!(id: params[:id])
  end

  def edit_auth
    tip_exception(403, "无权限") unless current_user.admin_or_business? || @item.user == current_user
  end

  def form_params
    params.permit(:discipline_id, :sub_discipline_id, :item_type, :difficulty, :name, :analysis, :is_ordered, tag_discipline_id: [], choices: %i[choice_text is_answer], standard_answers: [:position, :answer_text=>[]], :answer_texts=> [])
  end

end