class Ecs::CourseTargetsController < Ecs::CourseBaseController
  def index
    @course_targets = current_course.ec_course_targets.includes(:ec_graduation_subitems)

    respond_to do |format|
      format.json
      format.xlsx do
        filename = "#{current_year.year}届_#{current_course.name}_课程目标_#{Time.current.strftime('%Y%m%d%H%M%S')}.xlsx"
        render xlsx: 'index', filename: filename
      end
    end
  end

  def create
    Ecs::CreateCourseTargetsService.call(current_course, create_params)
    @course_targets = current_course.ec_course_targets.includes(:ec_graduation_subitems)

    render 'index'
  end

  def with_achievement_methods
    @course_targets = current_course.ec_course_targets
                        .includes(ec_course_achievement_methods:
                                      [:ec_course_evaluation, ec_achievement_evaluation_relates: :ec_course_evaluation_subitem])
  end

  private

  def create_params
    params.permit(course_targets: %i[id content standard_grade weight graduation_subitem_id])
  end
end