class Weapps::CourseMemberAttendancesController < ApplicationController
  before_action :require_login
  before_action :find_course, :user_course_identity, only: [:update_status]

  def index
    attendance = CourseAttendance.find params[:attendance_id]

    @page = params[:page] || 1
    @limit = params[:limit] || 2

    @member_attendances = attendance.course_member_attendances
    if params[:group_ids].present?
      @member_attendances = @member_attendances.joins(:course_member).where(course_members: {course_group_id: params[:group_ids]})
    end
    @member_attendances = @member_attendances.where(attendance_status: params[:attendance_status]) if params[:attendance_status].present?
    @member_attendances = @member_attendances.order("attendance_status=1 desc, course_member_attendances.updated_at desc, course_member_attendances.id asc")
    @members_count = @member_attendances.uniq.count
    @member_attendances = paginate @member_attendances.preload(user: :user_extension)

    # if attendance.course_attendance_groups.first&.course_group_id.to_i == 0
    #   @members = attendance.course.students
    # else
    #   @members = attendance.course.students.where(course_group_id: attendance.course_attendance_groups.pluck(:course_group_id))
    # end
    # @member_attendances = attendance.course_member_attendances
    # if params[:group_ids].present?
    #   @members = @members.where(course_group_id: params[:group_ids])
    # end
    #
    # if params[:attendance_status].present?
    #   @members = @members.joins(:course_member_attendances).where(course_member_attendances: {course_attendance_id: attendance.id, attendance_status: params[:attendance_status]})
    # end
    #
    # @members = @members.joins(:course_member_attendances).order("attendance_status=1 desc, course_member_attendances.updated_at desc")
    # @members_count = @members.uniq.count
    # @members = paginate @members.preload(user: :user_extension).uniq
  end

  def create
    tip_exception("签到码不能为空") if params[:attendance_mode] != "QUICK" && params[:code].blank?
    tip_exception("attendance_mode参数不对") unless ["NUMBER", "QRCODE", "QUICK"].include?(params[:attendance_mode])

    if params[:attendance_mode] == "QUICK"
      attendance = CourseAttendance.find_by(id: params[:attendance_id])
    else
      attendance = CourseAttendance.find_by(id: params[:attendance_id], attendance_code: params[:code])
    end
    tip_exception("该签到不存在") if attendance.blank? || attendance.course.blank?

    member = attendance.course.students.find_by(user_id: current_user.id)
    tip_exception("该签到不存在") if member.blank?

    tip_exception("不在签到时间内") unless attendance.current_attendance?

    tip_exception("只支持数字签到") if attendance.mode != "ALL" && attendance.mode == "NUMBER" && params[:attendance_mode] != "NUMBER"
    tip_exception("只支持二维码签到") if attendance.mode != "ALL" && attendance.mode == "QRCODE" && params[:attendance_mode] != "QRCODE"
    tip_exception("只支持快捷签到") if attendance.mode == "QUICK" && params[:attendance_mode] != "QUICK"

    current_attendance = attendance.course_member_attendances.find_by(user_id: current_user.id)
    if current_attendance.present?
      tip_exception("请勿重复签到") if current_attendance.attendance_status == "NORMAL"
      tip_exception("您当前是请假状态，无法签到") if current_attendance.attendance_status == "LEAVE"
      tip_exception("您当前是旷课状态，无法签到") if current_attendance.attendance_status == "ABSENCE" && current_attendance.attendance_mode == "TEACHER"
      current_attendance.update!(attendance_status: "NORMAL", attendance_mode: params[:attendance_mode])
    else
      attendance.course_member_attendances.create!(course_member_id: member.id, user_id: current_user.id, course_id: attendance.course_id,
                                                   course_group_id: member.course_group_id, attendance_status: "NORMAL", attendance_mode: params[:attendance_mode])
    end

    render_ok
  end

  def update_status
    tip_exception("user_id不能为空") if params[:user_id].blank?
    tip_exception(403, "无权限调整签到状态") if @user_course_identity > Course::ASSISTANT_PROFESSOR
    tip_exception("attendance_status参数不对") unless ["NORMAL", "LEAVE", "ABSENCE"].include?(params[:attendance_status])
    attendance = @course.course_attendances.find_by!(id: params[:attendance_id])
    current_attendance = attendance.course_member_attendances.find_by(user_id: params[:user_id])
    if current_attendance.present?
      current_attendance.update!(attendance_status: params[:attendance_status], attendance_mode: "TEACHER")
    else
      member = attendance.course.students.find_by(user_id: params[:user_id])
      tip_exception( "该用户非课堂学生") if member.blank?
      attendance.course_member_attendances.create!(course_member_id: member.id, user_id: params[:user_id], course_id: attendance.course_id,
                                                   course_group_id: member.course_group_id, attendance_status: params[:attendance_status], attendance_mode: "TEACHER")
    end
    render_ok
  end

end