class Admins::GlccPrCheckController < Admins::BaseController
    before_action :require_glcc_admin

    def index
        params[:sort_by] = params[:sort_by].presence || 'created_on'
        params[:sort_direction] = params[:sort_direction].presence || 'desc'
        examine_materials = Admins::GlccExamineMaterial.call(params)
        @examine_materials = paginate examine_materials.includes(:glcc_student)
    end

    def send_mail
        year = if params[:date].present?
            params[:date][:year]
        end
        if year.nil?
            return redirect_to admins_glcc_pr_check_index_path
            flash[:error]  = "时间不能为空"
        end
        if params[:term].blank?
            return redirect_to admins_glcc_pr_check_index_path
            flash[:error]  = "考核选项不能为空"
        end

        examine_materials = GlccMediumTermExamineMaterial.where(\
            term: params[:term],
            created_on: [Time.now.change(year:year).beginning_of_year .. Time.now.change(year:year).end_of_year]
        )
        examine_materials.map{ |e| 
            e.send_mail
        }
        flash[:danger] = "#{year} 年 #{params[:term].to_i == 1 ? "中期考核": "结项考核"} PR 检测邮件已全部发送完毕，一共#{examine_materials.count}封邮件"
        redirect_to admins_glcc_pr_check_index_path
    end
end
