//
//  RDSSetPwdVC.m
//  RDSmartPlug
//
//  Created by Rayson on 2020/5/19.
//  Copyright © 2020 RDIOT. All rights reserved.
//

#import "RDSSetPwdVC.h"
#import "YJJTextField.h"
#import "NSString+verify.h"
#import "UIView+RDSClip.h"
#import "NSString+Hash.h"// md5

@interface RDSSetPwdVC ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *titleLab;
@property (weak, nonatomic) IBOutlet UILabel *subTitleLab;

@property (weak, nonatomic) IBOutlet UITextField *textField1;
@property (weak, nonatomic) IBOutlet UITextField *textField2;

@property (weak, nonatomic) IBOutlet UIButton *sureBtn;

@property (weak, nonatomic) IBOutlet UIStackView *bottomView;



@end

@implementation RDSSetPwdVC

- (void)viewDidLoad {
    [super viewDidLoad];


    //self.view.backgroundColor = [UIColor whiteColor];

    [self p_setupTF];
}

- (void)setType:(Type)type{
    _type = type;
    
    if(type == TypeSetPwd){
        self.titleLab.text = @"设置密码";
        self.subTitleLab.text = @"欢迎注册账号";
        self.textField1.placeholder = @"请输入6-20位字母密码+数字组合密码";
        self.textField2.placeholder = @"确认输入的新密码";
        self.bottomView.hidden = NO;
    }else{
        self.titleLab.text = @"重置密码";
        self.subTitleLab.text = @"";
        self.textField1.placeholder = @"请输您的手机号";
        self.textField2.placeholder = @"请输入验证码";
        self.bottomView.hidden = YES;
    }
}


- (IBAction)onSureBtnClick {
    
    [self.view endEditing:YES];
    
    if (self.textField1.text.length < 0){
        [RDSHudShower showCenterToast:@"请输入6位以上密码"];
        return;
    }
    else if (![self.textField1.text isEqualToString:self.textField2.text]){
        [self rds_alertMessage:@"两次密码不一致"];
        return;
    }
    
    // md5加密
    NSString *pwd = [self.textField1.text md5String];
    
    RDS_WEAKSELF(weakSelf)
    [RDSDemoApiHelper rds_setPassword:pwd success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] != 0) {
           
        }
        else{
            
            [RDSHudShower showCenterToast:@"设置成功"];
            [weakSelf popViewControllerAnimated:YES];
        }
        
    } failure:^(NSError *error) {
        
    }];

}

#pragma mark - UITextFieldDelegate

- (void)p_setupTF {
    
    self.textField1.delegate = self;
    self.textField2.delegate = self;

    
}

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

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

@end
