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

#import "SLLoginVCViewController.h"
#import "SLForgetPWViewController.h"
#import "SLRegistViewController.h"
#import "SLRoleLabelViewController.h"
#import "SLMessageViewController.h"

#import "SLVerifiButton.h"

@interface SLLoginVCViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UILabel *loginLabel;

@property (weak, nonatomic) IBOutlet UILabel *loginDesLabel;

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

@property (weak, nonatomic) IBOutlet SLVerifiButton *codeBtn;

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

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;

@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UIButton *protocolBtn;

@property (nonatomic, assign) BOOL isCodeLogin;
@end

@implementation SLLoginVCViewController

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

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
    self.codeTextField.text = @"";
}

- (void)clearButtonClick {
    self.phoneTextField.text = @"";
}

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

- (IBAction)forgetPwClick:(id)sender {
    SLForgetPWViewController *vc = [SLForgetPWViewController loadViewControllewWithNib];
    [self navPushViewController:vc animated:YES];
}

- (IBAction)verCodeLoginClick:(id)sender {
    self.isCodeLogin = !self.isCodeLogin;
    self.forgetBtn.hidden = self.isCodeLogin;
    self.codeBtn.hidden = !self.isCodeLogin;
    [self.switchLoginBtn setTitle:self.isCodeLogin ? @"密码登录" : @"验证码登录" forState:UIControlStateNormal];
    self.loginLabel.text = !self.isCodeLogin ? @"密码登录" : @"验证码登录";
    self.loginDesLabel.text = !self.isCodeLogin ? @"密码登陆仅适用于已经注册的用户" : @"未注册的手机号验证通过后将自动注册";
    self.codeTextField.secureTextEntry = !self.isCodeLogin;
    self.codeTextField.text = @"";
}

- (IBAction)loginClick:(id)sender {
    NSString *phoneStr = self.phoneTextField.text;
    NSString *codeStr = self.codeTextField.text;
    if (!phoneStr.length) {
        [ZFToast ShowWithMessage:@"请输入手机号"];
        return;
    }
    if (phoneStr.length != 11) {
        [ZFToast ShowWithMessage:@"手机号输入有误"];
        return;
    }
    if (!codeStr.length) {
        [ZFToast ShowWithMessage:@"请输入密码"];
        return;
    }
    if (!self.protocolBtn.selected) {
        [ZFToast ShowWithMessage:@"请勾选协议"];
        return;
    }
    NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:@{@"phone" : phoneStr, @"password" : codeStr, @"code" : @"0"}];
    [[SLHttpCenter SharedInstance] postWithUrl:@"/api/Token/LoginApp" parameter:parameter success:^(id responseObject) {
        NSDictionary *dataDict = [responseObject objectForKey:@"data"];
        SLLoginInfo *loginModel = [SLLoginInfo mj_objectWithKeyValues:dataDict];
        [SLGlobalInfo SharedInstance].loginInfo = loginModel;
        [SLUserModel requestUserRoleModel];
        if (!loginModel.isAiData) {
            SLRoleLabelViewController *vc = [SLRoleLabelViewController loadViewControllewWithNib];
            [self navPushViewController:vc animated:YES];
        }else {
            AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [appDelegate setTabWithRootWindow];
        }
    } failure:^(SPRequestError *error) {
        
    }];
}

- (IBAction)registBtnClick:(id)sender {
    SLRegistViewController *vc = [SLRegistViewController loadViewControllewWithNib];
    [self navPushViewController:vc animated:YES];
}

- (IBAction)codeBtnClick:(id)sender {
    [[SLHttpCenter SharedInstance] postWithUrl:@"" parameter:@{} success:^(id responseObject) {
        
    } failure:^(SPRequestError *error) {
        
    }];
    SLVerifiButton *btn = (SLVerifiButton *)sender;
    [btn startTiming];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];//取消第一响应者
    return YES;
}

- (IBAction)protocolSelClick:(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];
    
}

@end
