part of reset_pwd;

class ResetPwdController extends GetxController {
  // 手机号
  final String phone;
  // 验证码
  final String code;
  ResetPwdController({required this.phone, required this.code});
  // 新密码
  final TextEditingController pwdInput = TextEditingController();
  // 确认密码
  final TextEditingController pwd2Input = TextEditingController();
  // 显示密码
  final ValueNotifier<bool> showPassword = ValueNotifier(false);
  // 显示密码
  final ValueNotifier<bool> showPassword2 = ValueNotifier(false);
  // 按钮是否可用
  bool  _enable = false;
  bool get enable => _enable;


  void onShowPassword() => showPassword.value = !showPassword.value;
  void onShowPassword2() => showPassword2.value = !showPassword2.value;

  @override
  void onClose() {
    pwd2Input.dispose();
    pwdInput.dispose();
    super.onClose();
  }

  /// 是否能点击
  void setCanClick(){
    if (ValidatorTool.isValidPassword(pwdInput.text) && ValidatorTool.isValidPassword(pwd2Input.text) && (pwdInput.text == pwd2Input.text)){
      Console.log('-------${pwdInput.text}-------------${pwd2Input.text}');
      _enable = true;
    }
    else{
      _enable = false;
    }
    update();
  }

  /// 重置密码
  void resetPassword(BuildContext context) async {
    final result = await AccountAPI.resetPassword(
      phone: phone,
      code: code,
      password: EncryptUtil.encodeMd5(EncryptUtil.encodeMd5(pwdInput.text)),
      rePassword: EncryptUtil.encodeMd5(EncryptUtil.encodeMd5(pwd2Input.text))
    );
    if (result){
      if (context.mounted){
        // context.goNamed(Routes.login);
        context.pop();
        context.pop();
      }
    }
    else{

    }
  }
}
