//
//  SLForgetPWViewController.m
//  SLAiELTS
//
//  Created by Gusont on 2023/2/27.
//

#import "SLForgetPWViewController.h"
#import "SLRetrievePWViewController.h"

#import "SLVerifiButton.h"

@interface SLForgetPWViewController ()

@property (weak, nonatomic) IBOutlet SLVerifiButton *codeBtn;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwAgainTextField;

@end

@implementation SLForgetPWViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationBar.hidden = YES;
    //  获取到clearbutton 可以获取样式上
    UIButton *btn = [self.phoneTextField valueForKey:@"_clearButton"];
    [btn setImage:ImageName(@"icon_clear_circle") forState:UIControlStateNormal];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (IBAction)backBtnClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)retrieveBtnClick:(id)sender {
    NSString *phoneStr = self.phoneTextField.text;
    if (!phoneStr.length) {
        [ZFToast ShowWithMessage:@"请输入手机号"];
        return;
    }
    if (phoneStr.length != 11) {
        [ZFToast ShowWithMessage:@"手机号输入有误"];
        return;
    }
    if (!self.codeTextField.text.length) {
        [ZFToast ShowWithMessage:@"请输入验证码"];
        return;
    }
    if (!self.pwTextField.text.length) {
        [ZFToast ShowWithMessage:@"请输入密码"];
        return;
    }
    if (![self.pwTextField.text isEqualToString:self.pwAgainTextField.text]) {
        [ZFToast ShowWithMessage:@"两次输入密码不一致"];
        return;
    }
    [[SLHttpCenter SharedInstance] postWithUrl:@"/api/Token/VerifyPhoneUpdatePass" parameter:@{@"phone" : phoneStr, @"password" : self.pwTextField.text, @"code" : self.codeTextField.text} success:^(id responseObject) {
        [ZFToast ShowWithMessage:@"修改成功"];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(SPRequestError *error) {
        
    }];
    
//    SLRetrievePWViewController *vc = [SLRetrievePWViewController loadViewControllewWithNib];
//    vc.phoneStr = self.phoneTextField.text;
//    vc.codeStr = self.codeTextField.text;
//    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)codeBtnClick:(id)sender {
    NSString *phoneStr = self.phoneTextField.text;
    if (!phoneStr.length) {
        [ZFToast ShowWithMessage:@"请输入手机号"];
        return;
    }
    if (phoneStr.length != 11) {
        [ZFToast ShowWithMessage:@"手机号输入有误"];
        return;
    }
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[SLHttpCenter SharedInstance] getWithUrl:@"/api/Token/GetPhoneUpdatePass" parameter:@{@"phone" : phoneStr} success:^(id responseObject) {
        SLVerifiButton *btn = (SLVerifiButton *)sender;
        [btn startTiming];
        [self.codeTextField becomeFirstResponder];
        [hub hideAnimated:YES];
    } failure:^(SPRequestError *error) {
        [hub hideAnimated:YES];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
