class Ecs::GraduationCourseSupportsController < Ecs::BaseController
  before_action :check_major_manager_permission!, only: [:create]

  def show
    @graduation_subitems = current_year.ec_graduation_subitems
                             .reorder('ec_graduation_requirements.position ASC, ec_graduation_subitems.position ASC')
                             .includes(:ec_graduation_requirement, ec_course_supports: :ec_course)
    @course_count = current_year.ec_courses.count

    respond_to do |format|
      format.json
      format.xlsx do
        filename = "#{current_year.year}届课程体系对毕业要求的支撑_#{Time.current.strftime('%Y%m%d%H%M%S')}.xlsx"
        render xlsx: 'show', filename: filename
      end
    end
  end

  def create
    graduation_subitem = current_year.ec_graduation_subitems.find(params[:graduation_subitem_id])
    @graduation_subitem = Ecs::SaveGraduationCourseSupportsService.call(graduation_subitem, create_params)
  end

  private

  def create_params
    params.permit(course_supports: %i[id ec_course_id weights top_relation])
  end
end