class Admins::TagDisciplinesController < Admins::BaseController

  def index
    @sub_discipline = current_sub_discipline
    @tag_disciplines = current_sub_discipline.tag_disciplines
  end

  def create
    name = params[:name].to_s.strip
    return render_error('名称重复') if current_sub_discipline.tag_disciplines.where(name: name).exists?
    TagDiscipline.create!(name: name, sub_discipline_id: current_sub_discipline.id, user_id: current_user.id)
    render_ok
  end

  def edit
    @tag_discipline = current_tag_discipline
  end

  def update
    if params[:tag_discipline] && params[:tag_discipline][:name].present?
      name = params[:tag_discipline][:name].to_s.strip
      current_tag_discipline.update_attributes!(name: name)
    else
      current_tag_discipline.update_attributes!(setting_params)
    end
    @tag_disciplines = current_tag_discipline.sub_discipline&.tag_disciplines
  end

  def destroy
    @tag_discipline_id = params[:id]
    current_tag_discipline.destroy!
  end

  private

  def current_sub_discipline
    @_current_sub_discipline = SubDiscipline.find params[:sub_discipline_id]
  end

  def current_tag_discipline
    @_current_tag_discipline = TagDiscipline.find params[:id]
  end

  def setting_params
    params.permit(:shixun, :subject, :question)
  end
end