class Admins::EnrollListsController < Admins::BaseController

  def index
    @competition = current_competition
    default_sort('created_at', 'desc')
    enroll_lists = Admins::CompetitionEnrollListQuery.call(@competition, params)

    @params_page = params[:page] || 1
    @enroll_lists = paginate enroll_lists.preload(competition_team: [:user, :teachers], user: { user_extension: :school })
    @personal = @competition.personal?

    respond_to do |format|
      format.js
      format.html
    end
  end

  def export
    default_sort('created_at', 'desc')
    @enroll_lists = Admins::CompetitionEnrollListQuery.call(current_competition, params)
    @enroll_lists = @enroll_lists.preload(competition_team: [:user, :teachers], user: { user_extension: :school })
    @competition_scores = current_competition.competition_scores.where(competition_stage_id: 0).order("score desc, cost_time desc").pluck(:competition_team_id)
    @personal = current_competition.personal?
    filename = ["#{current_competition.name}竞赛报名列表", Time.zone.now.strftime('%Y-%m-%d%H:%M:%S')].join('-') << '.xlsx'
    render xlsx: 'export', filename: filename
  end

  private
  def current_competition
    @_current_competition ||= Competition.find(params[:competition_id])
  end

end