无故事王国
2024-03-08 4966eb10cbf5ce9c6ee37a13d393fb0ae2d85b60
WanPai/Root/Course/VC/AddStudentVC.swift
@@ -11,234 +11,235 @@
class AddStudentVC: BaseVC {
    @IBOutlet weak var img_profile: UIImageView!
   @IBOutlet weak var view_profile: UIView!
    @IBOutlet weak var tf_name: QMUITextField!
    @IBOutlet weak var tf_gender: UITextField!
    @IBOutlet weak var tf_idCard: UITextField!
    @IBOutlet weak var tf_height: QMUITextField!
   @IBOutlet weak var view_height: UIView!
   @IBOutlet weak var view_weight: UIView!
   @IBOutlet weak var view_phone: UIView!
   @IBOutlet weak var tf_weight: QMUITextField!
    @IBOutlet weak var tf_phone: QMUITextField!
    @IBOutlet weak var tf_birthday: UITextField!
            @IBOutlet weak var img_profile: UIImageView!
            @IBOutlet weak var view_profile: UIView!
            @IBOutlet weak var tf_name: QMUITextField!
            @IBOutlet weak var tf_gender: UITextField!
            @IBOutlet weak var tf_idCard: UITextField!
            @IBOutlet weak var tf_height: QMUITextField!
            @IBOutlet weak var view_height: UIView!
            @IBOutlet weak var view_weight: UIView!
            @IBOutlet weak var view_phone: UIView!
            @IBOutlet weak var tf_weight: QMUITextField!
            @IBOutlet weak var tf_phone: QMUITextField!
            @IBOutlet weak var tf_birthday: UITextField!
    var profileImg:UIImage?
            var profileImg:UIImage?
    var studentModel = StudentProfileModel()
   var studentInfo:StudentProfile1Model?
   var activityDetailPartModel:ActivityDetailPartModel?
    var type:ItemType!
            var studentModel = StudentProfileModel()
            var studentInfo:StudentProfile1Model?
            var activityDetailPartModel:ActivityDetailPartModel?
            var type:ItemType!
    var verifyIdCard:Bool = false
            var verifyIdCard:Bool = false
    init(type:ItemType,studentInfo:StudentProfile1Model? = nil) {
        super.init(nibName: nil, bundle: nil)
        self.type = type
      self.studentInfo = studentInfo
    }
            init(type:ItemType,studentInfo:StudentProfile1Model? = nil) {
                        super.init(nibName: nil, bundle: nil)
                        self.type = type
                        self.studentInfo = studentInfo
            }
   init(activityDetailPartModel:ActivityDetailPartModel? = nil) {
      super.init(nibName: nil, bundle: nil)
      self.type = .activity
      self.activityDetailPartModel = activityDetailPartModel
   }
            init(activityDetailPartModel:ActivityDetailPartModel? = nil) {
                        super.init(nibName: nil, bundle: nil)
                        self.type = .activity
                        self.activityDetailPartModel = activityDetailPartModel
            }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
            required init?(coder: NSCoder) {
                        fatalError("init(coder:) has not been implemented")
            }
    override func viewDidLoad() {
        super.viewDidLoad()
        if type == .course{
            title = "添加运动营成员"
         view_profile.isHidden = false
         if studentInfo != nil{
            title = "编辑运动营成员"
            tf_name.placeholder = "请输入运动营成员姓名"
            tf_height.placeholder = "请输入运动营成员身高"
            tf_weight.placeholder = "请输入运动营成员体重"
            tf_phone.placeholder = "请输入运动营成员联系方式"
            tf_idCard.placeholder = "请输入运动营成员身份证号"
            tf_name.text = studentInfo!.stuName
            tf_idCard.text = studentInfo!.idCard
            tf_height.text = String(format: "%.0lf", studentInfo!.stuHeight)
            tf_weight.text = String(format: "%.0lf", studentInfo!.stuWeight)
            tf_phone.text = studentInfo!.stuPhone
            tf_birthday.text = studentInfo!.birthday
            img_profile.sd_setImage(with: URL(string: studentInfo!.stuHeadImg))
            studentModel.headImg = studentInfo!.stuHeadImg
            studentModel.stuId = studentInfo!.stuId
            tf_gender.text = studentModel.sex == .man ? "男":"女"
         }
        }else{
         view_profile.isHidden = true
            title = "添加人员"
         tf_name.placeholder = "请输入人员姓名"
         tf_height.placeholder = "请输入身高"
         tf_weight.placeholder = "请输入体重"
         tf_phone.placeholder = "请输入联系方式"
         tf_idCard.placeholder = "请输入身份证号"
         if let m = activityDetailPartModel{
            title = "编辑人员"
            tf_name.text = m.name
            tf_idCard.text = m.idcard
            tf_height.text = String(format: "%.0lf", m.height)
            tf_weight.text = String(format: "%.0lf", m.weight)
            tf_phone.text = m.phone
            tf_birthday.text = m.birthday
            tf_gender.text = m.gender == .man ? "男":"女"
            view_height.isHidden = true
            view_weight.isHidden = true
            view_phone.isHidden = true
            override func viewDidLoad() {
                        super.viewDidLoad()
                        if type == .course{
                                    title = "添加运动营成员"
                                    view_profile.isHidden = false
                                    if studentInfo != nil{
                                                title = "编辑运动营成员"
                                                tf_name.placeholder = "请输入运动营成员姓名"
                                                tf_height.placeholder = "请输入运动营成员身高"
                                                tf_weight.placeholder = "请输入运动营成员体重"
                                                tf_phone.placeholder = "请输入运动营成员联系方式"
                                                tf_idCard.placeholder = "请输入运动营成员身份证号"
                                                tf_name.text = studentInfo!.stuName
                                                tf_idCard.text = studentInfo!.idCard
                                                tf_height.text = String(format: "%.0lf", studentInfo!.stuHeight)
                                                tf_weight.text = String(format: "%.0lf", studentInfo!.stuWeight)
                                                tf_phone.text = studentInfo!.stuPhone
                                                tf_birthday.text = studentInfo!.birthday
                                                img_profile.sd_setImage(with: URL(string: studentInfo!.stuHeadImg))
                                                studentModel.headImg = studentInfo!.stuHeadImg
                                                studentModel.stuId = studentInfo!.stuId
                                                tf_gender.text = studentModel.sex == .man ? "男":"女"
                                    }
                        }else{
                                    view_profile.isHidden = true
                                    title = "添加人员"
                                    tf_name.placeholder = "请输入人员姓名"
                                    tf_height.placeholder = "请输入身高"
                                    tf_weight.placeholder = "请输入体重"
                                    tf_phone.placeholder = "请输入联系方式"
                                    tf_idCard.placeholder = "请输入身份证号"
                                    if let m = activityDetailPartModel{
                                                title = "编辑人员"
                                                tf_name.text = m.name
                                                tf_idCard.text = m.idcard
                                                tf_height.text = String(format: "%.0lf", m.height)
                                                tf_weight.text = String(format: "%.0lf", m.weight)
                                                tf_phone.text = m.phone
                                                tf_birthday.text = m.birthday
                                                tf_gender.text = m.gender == .man ? "男":"女"
                                                view_height.isHidden = true
                                                view_weight.isHidden = true
                                                view_phone.isHidden = true
            tf_name.isEnabled = false
            tf_birthday.isEnabled = false
            tf_gender.isEnabled = false
         }
        }
                                                tf_name.isEnabled = false
                                                tf_birthday.isEnabled = false
                                                tf_gender.isEnabled = false
                                    }
                        }
        let idCardView = IDCardView()
        idCardView.box.bind { [weak self] text in
            self?.tf_idCard.text = text
        }
        tf_idCard.inputView = idCardView
                        let idCardView = IDCardView()
                        idCardView.box.bind { [weak self] text in
                                    self?.tf_idCard.text = text
                        }
                        tf_idCard.inputView = idCardView
        let tap = UITapGestureRecognizer(target: self, action: #selector(userImgUploadAction))
        img_profile.isUserInteractionEnabled = true
        img_profile.addGestureRecognizer(tap)
    }
                        let tap = UITapGestureRecognizer(target: self, action: #selector(userImgUploadAction))
                        img_profile.isUserInteractionEnabled = true
                        img_profile.addGestureRecognizer(tap)
            }
    override func setUI() {
        tf_birthday.delegate = self
        tf_gender.delegate = self
    }
            override func setUI() {
                        tf_birthday.delegate = self
                        tf_gender.delegate = self
            }
    @objc func userImgUploadAction(){
        JQ_ImagePickerTool.getSharedInstance().singleImage({ [weak self] image in
            self?.img_profile.image = image
            self?.profileImg = image
        }, clipSize: CGSize(width: JQ_ScreenW, height: JQ_ScreenW))
    }
            @objc func userImgUploadAction(){
                        JQ_ImagePickerTool.getSharedInstance().singleImage({ [weak self] image in
                                    self?.img_profile.image = image
                                    self?.profileImg = image
                        }, clipSize: CGSize(width: JQ_ScreenW, height: JQ_ScreenW))
            }
    @IBAction func completeAction(_ sender: UIButton) {
      if studentInfo == nil && type == .course{
         guard profileImg != nil else {alertError(msg: "请上传运动营成员头像");return}
      }
            @IBAction func completeAction(_ sender: UIButton) {
                        if studentInfo == nil && type == .course{
                                    guard profileImg != nil else {alertError(msg: "请上传运动营成员头像");return}
                        }
      guard !tf_name.text!.isEmpty else {alertError(msg: tf_name.placeholder!);return}
      guard !tf_birthday.text!.isEmpty else {alertError(msg: "请选择生日");return}
      guard !tf_gender.text!.isEmpty else {alertError(msg: "请选择性别");return}
                        guard !tf_name.text!.isEmpty else {alertError(msg: tf_name.placeholder!);return}
                        guard !tf_birthday.text!.isEmpty else {alertError(msg: "请选择生日");return}
                        guard !tf_gender.text!.isEmpty else {alertError(msg: "请选择性别");return}
      if type == .course{
         guard !tf_height.text!.isEmpty else {alertError(msg: tf_height.placeholder!);return}
         guard !tf_weight.text!.isEmpty else {alertError(msg: tf_weight.placeholder!);return}
         guard tf_height.text != "0" else {alertError(msg: "请输入正确的身高");return}
         guard tf_weight.text != "0" else {alertError(msg: "请输入正确的体重");return}
      }
                        if type == .course{
                                    guard !tf_height.text!.isEmpty else {alertError(msg: tf_height.placeholder!);return}
                                    guard !tf_weight.text!.isEmpty else {alertError(msg: tf_weight.placeholder!);return}
                                    guard tf_height.text != "0" else {alertError(msg: "请输入正确的身高");return}
                                    guard tf_weight.text != "0" else {alertError(msg: "请输入正确的体重");return}
                        }
        if !tf_phone.isEmpty{
            guard tf_phone.text!.jq_isPhone else {alertError(msg: "请输入正确的手机号");return}
        }
        if !tf_idCard.isEmpty || activityDetailPartModel != nil{
            guard tf_idCard.text!.jq_idCard() else {alertError(msg: "请输入正确的身份证号码");return}
        }
                        if !tf_phone.isEmpty{
                                    guard tf_phone.text!.jq_isPhone else {alertError(msg: "请输入正确的手机号");return}
                        }
        studentModel.birthday = tf_birthday.text!
        studentModel.name = tf_name.text!
        studentModel.height = tf_height.text!.toDouble
        studentModel.weight = tf_weight.text!.toDouble
        studentModel.phone = tf_phone.text!
        studentModel.idCard = tf_idCard.text!
        studentModel.name = tf_name.text!
//                        if !tf_idCard.isEmpty || activityDetailPartModel != nil{
//
//                        }
                        guard tf_idCard.text!.jq_idCard() else {alertError(msg: "请输入正确的身份证号码");return}
        if profileImg != nil{
            showHUD("正在上传头像")
            profileImg!.uploadImgToService().subscribe(onNext: { [weak self] imgUrl in
                guard let weakSelf = self else { return }
                weakSelf.studentModel.headImg = imgUrl
                hiddenHUD()
                weakSelf.addStudent()
            }, onError: { error in
                hiddenHUD()
                alertError(msg: error.localizedDescription)
            }).disposed(by: disposeBag)
        }else{
            addStudent()
        }
    }
                        studentModel.birthday = tf_birthday.text!
                        studentModel.name = tf_name.text!
                        studentModel.height = tf_height.text!.toDouble
                        studentModel.weight = tf_weight.text!.toDouble
                        studentModel.phone = tf_phone.text!
                        studentModel.idCard = tf_idCard.text!
                        studentModel.name = tf_name.text!
    private func addStudent(){
        if type == .course{
         if studentInfo == nil{
            Services.addStudent(studentModel).subscribe(onNext: { [weak self] data in
               self?.addStudentSuccess(text: "编辑成功")
            }).disposed(by: disposeBag)
         }else{
            Services.editStu(studentModel).subscribe(onNext: {[weak self]data in
               self?.addStudentSuccess(text: "添加成功")
               NotificationCenter.default.post(name: StudentUpdate_Nofi, object: nil)
            }) { error in
                        if profileImg != nil{
                                    showHUD("正在上传头像")
                                    profileImg!.uploadImgToService().subscribe(onNext: { [weak self] imgUrl in
                                                guard let weakSelf = self else { return }
                                                weakSelf.studentModel.headImg = imgUrl
                                                hiddenHUD()
                                                weakSelf.addStudent()
                                    }, onError: { error in
                                                hiddenHUD()
                                                alertError(msg: error.localizedDescription)
                                    }).disposed(by: disposeBag)
                        }else{
                                    addStudent()
                        }
            }
            }.disposed(by: disposeBag)
         }
        }else{
         if activityDetailPartModel != nil{
            activityDetailPartModel!.idcard = tf_idCard.text!
            Services.editParticipant(activityDetailPartModel!).subscribe(onNext: {[weak self] data in
               self?.addStudentSuccess(text: "编辑成功")
            }).disposed(by: disposeBag)
            private func addStudent(){
                        if type == .course{
                                    if studentInfo == nil{
                                                Services.addStudent(studentModel).subscribe(onNext: { [weak self] data in
                                                            self?.addStudentSuccess(text: "编辑成功")
                                                }).disposed(by: disposeBag)
                                    }else{
                                                Services.editStu(studentModel).subscribe(onNext: {[weak self]data in
                                                            self?.addStudentSuccess(text: "添加成功")
                                                            NotificationCenter.default.post(name: StudentUpdate_Nofi, object: nil)
                                                }) { error in
         }else{
            Services.addParticipant(studentModel).subscribe(onNext: {[weak self] data in
               self?.addStudentSuccess(text: "添加成功")
            }).disposed(by: disposeBag)
         }
        }
    }
                                                }.disposed(by: disposeBag)
                                    }
                        }else{
                                    if activityDetailPartModel != nil{
                                                activityDetailPartModel!.idcard = tf_idCard.text!
                                                Services.editParticipant(activityDetailPartModel!).subscribe(onNext: {[weak self] data in
                                                            self?.addStudentSuccess(text: "编辑成功")
                                                }).disposed(by: disposeBag)
   private func addStudentSuccess(text:String){
      alertSuccess(msg: text)
      DispatchQueue.main.asyncAfter(deadline: .now()+1) {
         self.navigationController?.popViewController()
         NotificationCenter.default.post(name: StudentUpdate_Nofi, object: nil)
      }
   }
                                    }else{
                                                Services.addParticipant(studentModel).subscribe(onNext: {[weak self] data in
                                                            self?.addStudentSuccess(text: "添加成功")
                                                }).disposed(by: disposeBag)
                                    }
                        }
            }
    deinit{
        JQ_ImagePickerTool.destroy()
    }
            private func addStudentSuccess(text:String){
                        alertSuccess(msg: text)
                        DispatchQueue.main.asyncAfter(deadline: .now()+1) {
                                    self.navigationController?.popViewController()
                                    NotificationCenter.default.post(name: StudentUpdate_Nofi, object: nil)
                        }
            }
            deinit{
                        JQ_ImagePickerTool.destroy()
            }
}
extension AddStudentVC:UITextFieldDelegate{
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        if textField == tf_birthday{
         view.endEditing(true)
            CommonDatePickerView.show(before: 60, after: 0, type: .YMD) { year, month, day,_,_ in
                textField.text = String(format: "%ld-%02ld-%02ld", year!,month!,day!)
            }
            return false
        }
            func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
                        if textField == tf_birthday{
                                    view.endEditing(true)
                                    CommonDatePickerView.show(before: 60, after: 0, type: .YMD) { year, month, day,_,_ in
                                                textField.text = String(format: "%ld-%02ld-%02ld", year!,month!,day!)
                                    }
                                    return false
                        }
        if textField == tf_gender{
                        if textField == tf_gender{
            let alertVC = UIAlertController(title: "性别", message: nil, preferredStyle: .actionSheet)
            alertVC.addAction(UIAlertAction(title: "男", style: .default) { _ in
                self.tf_gender.text = "男"
            })
                                    let alertVC = UIAlertController(title: "性别", message: nil, preferredStyle: .actionSheet)
                                    alertVC.addAction(UIAlertAction(title: "男", style: .default) { _ in
                                                self.tf_gender.text = "男"
                                    })
            alertVC.addAction(UIAlertAction(title: "女", style: .default) { _ in
                self.tf_gender.text = "女"
            })
                                    alertVC.addAction(UIAlertAction(title: "女", style: .default) { _ in
                                                self.tf_gender.text = "女"
                                    })
            alertVC.addAction(UIAlertAction(title: "取消", style: .cancel))
            present(alertVC, animated: true)
            return false
        }
        return true
    }
                                    alertVC.addAction(UIAlertAction(title: "取消", style: .cancel))
                                    present(alertVC, animated: true)
                                    return false
                        }
                        return true
            }
}