//
//  RDSResetPwdVC.m
//  Temperature
//
//  Created by RD on 2022/11/23.
//

#import "RDSResetPwdVC.h"
#import "WLCaptcheButton.h"
#import "UIView+RDSClip.h"
#import "NSString+verify.h"
#import "NSString+Hash.h"// md5
#import "NSString+extension.h"

@interface RDSResetPwdVC ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UITextField *pwdTF;
@property (weak, nonatomic) IBOutlet WLCaptcheButton *getCodeBtn;

@end

@implementation RDSResetPwdVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self.getCodeBtn rds_setCornerRadius:self.getCodeBtn.rds_height*0.5 borderLineWidth:1 lineColor:RDSGreenColor];
    
    
    [self p_setupTF];
    _phoneTF.text = TheDataManager.phone;
}



- (IBAction)onGetCodeBtnClick:(WLCaptcheButton *)sender {
    
    if (![self.phoneTF.text isPhoneNum]) {
        [RDSHudShower showCenterToast:@"请输入正确的手机号码"];
        return;
    }
    
    [sender fire];
    
    _codeTF.text = @"";
    [_codeTF becomeFirstResponder];
    
    [RDSHudShower showWithStatus:@"正在获取验证码…" autoDismiss:YES];
    RDS_WEAKSELF(weakSelf)
    [RDSDemoApiHelper rds_getVerifyCodePhone:self.phoneTF.text type:2 success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] != 0) {
            [RDSHudShower showCenterToast:responseObject[@"message"]];
        }
        else{
            //[RDSHudShower showCenterToast:@"获取验证码成功！"];
        }
        
    } failure:^(NSError *error) {
        DDLog(@"json data error:/n%@", error.description);
        [RDSHudShower showCenterToast:@"连接服务器失败"];
    }];
}


- (IBAction)onSureBtnClick{
    
    [self.view endEditing:YES];
    
    if (self.codeTF.text.length <4){
        [RDSHudShower showCenterToast:@"请输入验证码"];
        return;
    }
    else if (![self.pwdTF.text isPwd]){
        [RDSHudShower showCenterToast:@"请输入6位以上新密码"];
        return;
    }
    
    // md5加密
    NSString *pwd = [self.pwdTF.text md5String];
    RDS_WEAKSELF(weakSelf)
    [RDSDemoApiHelper rds_resetPwdPhone:_phoneTF.text password:pwd code:self.codeTF.text success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] != 0) {
            [RDSHudShower showCenterToast:responseObject[@"message"]];
        }
        else{
            
            TheDataManager.phone = _phoneTF.text;
            TheDataManager.pwd = pwd;
            [RDSHudShower showCenterToast:@"设置成功"];
            [weakSelf popViewControllerAnimated:YES];
        }
        
    } failure:^(NSError *error) {
        [RDSHudShower showCenterToast:@"连接服务器失败"];
    }];
}

#pragma mark - UITextFieldDelegate

- (void)p_setupTF {
    
    self.codeTF.delegate = self;
    self.pwdTF.delegate = self;

    
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    textField.text = [textField.text removeAllSapce];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    if (textField == self.codeTF) {
        [self.pwdTF becomeFirstResponder];
    }
    else if (textField == self.pwdTF) {
        [self onSureBtnClick];
    }
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
}

@end
