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

#import "SLRegistViewController.h"
#import "SLRoleLabelViewController.h"

#import "SLVerifiButton.h"
#import "SLUserModel.h"

@interface SLRegistViewController ()

@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 UIButton *protocolBtn;

@end

@implementation SLRegistViewController

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

- (IBAction)backBtnClick:(id)sender {
    [self.navigationController popViewControllerAnimated: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/GetPhoneValidateCode" parameter:@{@"phone" : phoneStr} success:^(id responseObject) {
        SLVerifiButton *btn = (SLVerifiButton *)sender;
        [btn startTiming];
        [self.codeTextField becomeFirstResponder];
        [hub hideAnimated:YES];
    } failure:^(SPRequestError *error) {
        [hub hideAnimated:YES];
    }];
}

- (IBAction)registLoginBtnClick:(id)sender {
    NSString *phoneStr = self.phoneTextField.text;
    NSString *codeStr = self.codeTextField.text;
    NSString *pwStr = self.pwTextField.text;
    if (!phoneStr.length) {
        [ZFToast ShowWithMessage:@"请输入手机号"];
        return;
    }
    if (phoneStr.length != 11) {
        [ZFToast ShowWithMessage:@"手机号输入有误"];
        return;
    }
    if (!codeStr.length) {
        [ZFToast ShowWithMessage:@"请输入验证码"];
        return;
    }
    if (!pwStr.length) {
        [ZFToast ShowWithMessage:@"请输入密码"];
        return;
    }
    if (pwStr.length < 6 || pwStr.length > 16) {
        [ZFToast ShowWithMessage:@"密码长度为6到16位"];
        return;
    }
    if (!self.protocolBtn.selected) {
        [ZFToast ShowWithMessage:@"请勾选协议"];
        return;
    }
    NSDictionary *parameter = @{@"phone" : phoneStr, @"password" : pwStr, @"code" : codeStr};
    [[SLHttpCenter SharedInstance] postWithUrl:@"/api/Token/isPhoneCode" parameter:parameter success:^(id responseObject) {
        NSDictionary *dataDict = [responseObject objectForKey:@"data"];
        SLLoginInfo *loginModel = [SLLoginInfo mj_objectWithKeyValues:dataDict];
        [SLGlobalInfo SharedInstance].loginInfo = loginModel;
        [SLUserModel requestUserRoleModel];
        SLRoleLabelViewController *vc = [SLRoleLabelViewController loadViewControllewWithNib];
        [self.navigationController pushViewController:vc animated:YES];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self removeFromParentViewController];
        });
    } failure:^(SPRequestError *error) {
        
    }];
}
- (IBAction)protocolBtnClick:(id)sender {
    UIButton *btn = (UIButton *)sender;
    btn.selected = !btn.selected;
}

- (IBAction)userProtocolBtnClick:(id)sender {
    UIButton *btn = (UIButton *)sender;
    NSString *urlStr = @"http://115.238.47.234:8446/index.html";
    if (btn.tag == 2001) {
        urlStr = @"http://115.238.47.234:8446/mzsm.html";
    }
    SLWebViewController *vc = [SLWebViewController loadViewControllewWithNib];
    vc.webUrlStr = urlStr;
    vc.title = btn.titleLabel.text;
    [self navPushViewController:vc animated: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
