//
//  PasswordLoginViewController.m
//  GtDemo
//
//  Created by 雷俊博 on 2019/6/29.
//  Copyright © 2019 雷俊博. All rights reserved.
//

#import "PasswordLoginViewController.h"
#import "FFPasswordViewController.h"
#import "Constants.h"
#import "LoginView.h"
#import <Masonry.h>
#import "Tools.h"
#import "CTAPIBaseManager.h"
#import "LDDKeyboardView.h"
#import "CYRKeyboardButton.h"
#import "XTYRandomKeyboard.h"
//typedef NS_ENUM(NSUInteger,LDDButtonType) {
//    LDDButtonTypeNumber=100,
//    LDDButtonTypeChar,
//    LDDButtonTypeSign,
//    LDDButtonTypeDelete,
//    LDDButtonTypeLogin,
//    LDDButtonTypeShift,
//    LDDButtonTypeSpace,
//};
@interface PasswordLoginViewController ()<loginBtnDelegate, UITextFieldDelegate,LDDKeyboardViewDelegate>
@property (nonatomic, strong)UILabel *lb_Title;

@property (nonatomic, strong)UILabel *lb_InternationalAreaCode;

@property (nonatomic, strong)UILabel *lb_horizontalDividingLine;


@property (nonatomic, strong)UITextField *tf_Account;

@property (nonatomic, strong)UIView *accountLine;

@property (nonatomic, strong) UITextField *tf_password;

@property (nonatomic, strong)UIButton *btn_passwordHidden;


@property (nonatomic, strong) UIView *passwordLine;

@property (nonatomic, strong) UIButton *btn_forgetPassword;

@property (nonatomic, strong) UIButton *loginBtn;


@property (nonatomic, strong) LoginView *wxLogin;

@property (nonatomic, strong) LoginView *directlyLogin;


@property (nonatomic, strong) LoginView * smsLogin;


@property (nonatomic, strong) UIView *leftLine;

@property (nonatomic, strong) UILabel *middleLab;

@property (nonatomic, strong) UIView *rightLine;


@property(nonatomic,strong)LDDKeyboardView *keyboardView;

@end

@implementation PasswordLoginViewController

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.lb_Title];
    [self.view addSubview:self.lb_InternationalAreaCode];
    [self.view addSubview:self.lb_horizontalDividingLine];
    [self.view addSubview:self.tf_Account];
    [self.view addSubview:self.accountLine];
    [self.view addSubview:self.tf_password];
    [self.view addSubview:self.btn_passwordHidden];
    [self.view addSubview:self.passwordLine];
    [self.view addSubview:self.btn_forgetPassword];
    [self.view addSubview:self.loginBtn];
    
    [self.view addSubview:self.wxLogin];
    [self.view addSubview:self.smsLogin];
    [self.view addSubview:self.directlyLogin];
    
    [self.view addSubview:self.leftLine];
    [self.view addSubview:self.middleLab];
    [self.view addSubview:self.rightLine];
    
    _tf_Account.delegate = self;
    
    [_tf_Account addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [_tf_password addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(jiePing) name:UIApplicationUserDidTakeScreenshotNotification  object:nil];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    [self setupUI];
}
- (void)dealloc
{
    NSLog(@"PasswordLoginViewController已销毁");
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}


#pragma mark - loginBtnDelegate
- (void)loginBtnClickWith:(LoginView *)loginView
{
    
    CTLoginBtnType type  = CTLoginBtnTypeDefault;
    
    
        if (loginView == _wxLogin) {
            NSLog(@"点击微信登录");
            type = CTLoginBtnTypeWX;
        }else if (loginView == _directlyLogin){
            NSLog(@"一键登录");
             type = CTLoginBtnTypeDirectly;
        }else if (loginView == _smsLogin){
            NSLog(@"短信登录");
            type = CTLoginBtnTypeSMS;
        }
    
    if ([self.delegate respondsToSelector:@selector(viewController:type:)]) {
        [self.delegate viewController:self type:type];
    }
}

#pragma mark - LDDKeyboardViewDelegate
/**代理方法*/
-(void)clickKeyboardButton:(UIButton*)button{
    
    NSMutableString *str=[NSMutableString stringWithString:self.tf_password.text];

            switch (button.tag) {
                //1，删除按钮
                case LDDButtonTypeDelete:
                    AudioServicesPlaySystemSound(1105);//按键时播放系统声音
                    if ([str isEqualToString:@""])  return;//若密码框为空直接return，
                    [self.tf_password deleteBackward];
                    break;
                    
                //2，大小写切换按钮
                case LDDButtonTypeShift:
                    button.selected=!button.selected;
                    AudioServicesPlaySystemSound(1105);//按钮点击声音
                    for (CYRKeyboardButton *charBtn in self.keyboardView.charView.subviews) {
                        
                        if ([charBtn isKindOfClass:[CYRKeyboardButton class]] && button.selected) {
                            [charBtn setTitle:[NSString stringWithFormat:@"%@",[charBtn.currentTitle uppercaseString]] forState:UIControlStateNormal];
                            charBtn.input=charBtn.currentTitle;
                           
                        }else if ([charBtn isKindOfClass:[CYRKeyboardButton class]] &&!button.selected){
                            [charBtn setTitle:[NSString stringWithFormat:@"%@",[charBtn.currentTitle lowercaseString]] forState:UIControlStateNormal];
                            charBtn.input=charBtn.currentTitle;

                        }
                    }
                    
                    break;
                    
                //3，空格按钮
                case LDDButtonTypeSpace:
                    AudioServicesPlaySystemSound(1105);
                    [button addTarget:self action:@selector(backgroundHighlighted:) forControlEvents:UIControlEventTouchDown];
                    self.tf_password.text=[self.tf_password.text stringByAppendingString:@" "];
                    button.backgroundColor=[UIColor whiteColor];
                    break;
                    
                    
                //4，登录按钮
                case LDDButtonTypeLogin:
                    
                    AudioServicesPlaySystemSound(1105);
                    [self.tf_password resignFirstResponder];
                    break;
                case LDDButtonTypeChar:
                    [self.keyboardView changeToCharModelKeyBoard];
                    break;
                case LDDButtonTypeNumber:
                    [self.keyboardView changeToNumModelKeyBoard];
                    break;
                    
                //5，其他按钮
                default:
                    AudioServicesPlaySystemSound(1104);
                    [self.tf_password insertText:button.currentTitle];
                    break;
            }
    
}

#pragma mark - 按钮绑定方法
//按下空格键，按钮背景变灰色
-(void)backgroundHighlighted:(UIButton*)button{
    
    button.backgroundColor=[UIColor colorWithRed:110/255.0 green:110/255.0 blue:110/255.0 alpha:1];
    
}


#pragma mark - UITextFieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField
{

    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.view endEditing:YES];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField == _tf_Account) {
        if (textField.text.length != 11) {
            [XYProgressHUD showMessage:@"请输入11位的手机号"];
        }
      
    }
}
#pragma mark - setupUI
- (void)setupUI
{
    
    //title
    
    CGSize topSize = [self.lb_Title.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:25*WIDTHRADIU],NSFontAttributeName, nil]];
    CGFloat topH = topSize.height;
    
    [self.lb_Title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(100 * HEIGHTRADIU);
        make.left.offset(30 * WIDTHRADIU);
        make.height.offset(topH*WIDTHRADIU);
    }];
    
    
    [self.lb_InternationalAreaCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lb_Title.mas_bottom).offset(44*WIDTHRADIU);
        make.left.equalTo(self.lb_Title.mas_left).offset(3 * WIDTHRADIU);
        make.width.offset(35);
        make.height.offset(17 * WIDTHRADIU);
    }];
    
    
    [self.lb_horizontalDividingLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.lb_InternationalAreaCode);
        make.left.equalTo(self.lb_InternationalAreaCode.mas_right).offset(12);
        make.width.offset(0.5);
        make.height.offset(25);
    }];
    
    [self.tf_Account mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.lb_InternationalAreaCode);
        make.left.equalTo(self.lb_horizontalDividingLine.mas_right).offset(15);
        make.right.offset(-33 * WIDTHRADIU);
        make.height.offset(44*WIDTHRADIU);
    }];
    [self.accountLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tf_Account.mas_bottom).offset(1*WIDTHRADIU);
        make.left.equalTo(self.lb_Title.mas_left);
        make.right.offset(-30 * WIDTHRADIU);
        make.height.offset(0.5);
    }];
    [self.tf_password mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.accountLine.mas_bottom).offset(9*WIDTHRADIU);
        make.left.equalTo(self.lb_Title.mas_left).offset(3 * WIDTHRADIU);
        make.right.offset(-33 * WIDTHRADIU);
        make.height.equalTo(self.tf_Account.mas_height);
    }];
    
    [self.btn_passwordHidden mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.tf_password);
        make.right.equalTo(self.tf_password.mas_right).offset(-6);
    }];
   
    [self.passwordLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tf_password.mas_bottom).offset(1*WIDTHRADIU);
        make.left.equalTo(self.accountLine.mas_left);
        make.right.equalTo(self.accountLine.mas_right);
        make.height.equalTo(self.accountLine);
    }];
    
    [self.btn_forgetPassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordLine.mas_bottom).offset(3*WIDTHRADIU);
        make.right.equalTo(self.passwordLine.mas_right);
    }];
    
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.btn_forgetPassword.mas_bottom).offset(30*WIDTHRADIU);
        make.left.right.equalTo(self.passwordLine);
        make.height.offset(45*WIDTHRADIU);
    }];
    
    
    [self.directlyLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(-40 * WIDTHRADIU);
        make.centerX.equalTo(self.view.mas_centerX);
        make.height.offset(72 * WIDTHRADIU);
        make.width.offset(60 * WIDTHRADIU);
    }];
    
    
    
    [self.wxLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.directlyLogin.mas_centerY);
        make.right.equalTo(self.directlyLogin.mas_left).offset(-44* WIDTHRADIU);
        make.height.equalTo(self.directlyLogin.mas_height);
        make.width.equalTo(self.directlyLogin.mas_width);
    }];
    [self.smsLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.directlyLogin.mas_centerY);
        make.left.equalTo(self.directlyLogin.mas_right).offset(44* WIDTHRADIU);
        make.height.equalTo(self.directlyLogin.mas_height);
        make.width.equalTo(self.directlyLogin.mas_width);
    }];
    
    CGSize middleSize = [self.middleLab.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil]];
    CGFloat middleH = middleSize.height;
    [self.middleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self -> _directlyLogin.mas_top).offset(-35*WIDTHRADIU);
        make.centerX.equalTo(self.view.mas_centerX);
        make.height.offset(middleH);
    }];
    [self.middleLab setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    [self.leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.middleLab.mas_centerY);
        make.left.equalTo(self.view.mas_left).offset(25 * WIDTHRADIU);
        make.right.equalTo(self -> _middleLab.mas_left).offset(-16);
        make.height.offset(0.5);
    }];
    [self.rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.middleLab.mas_centerY);
        make.left.equalTo(self -> _middleLab.mas_right).offset(16);
        make.right.equalTo(self.view.mas_right).offset(-25 * WIDTHRADIU);
        make.height.offset(0.5);
    }];
    
}




#pragma mark - event response
- (void)resetPasswordClick:(UIButton *)sender
{
    [self.view endEditing:YES];
    
    FFPasswordViewController *ffPasswordVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"FFPasswordViewController"];
    ffPasswordVC.title = @"找回密码";
    [self.navigationController pushViewController:ffPasswordVC animated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
- (void)loginClick:(UIButton *)sender
{
    [self.view endEditing:YES];
    NSLog(@"123");
    if (![Tools isMobileNumber:_tf_Account.text]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return;
    }
    
    if ([Tools isBlankString:_tf_password.text]) {
        [SVProgressHUD showErrorWithStatus:@"密码不能为空"];
        return;
    }
    
//    WeakSelf
    
    [SVProgressHUD show];
    
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    delegate.telephoneLoginStr =_tf_Account.text ;
    delegate.telephoneLoginPassport =_tf_password.text ;
        
        [logNetModel getLogMessage5:nil andTelephone:_tf_Account.text andLoginpassword:_tf_password.text andUid:nil andl:0 andweixinId:nil andPicId:nil  completionHandle:^(lodModel *model, NSError *error) {
            if (error) {
                [SVProgressHUD showErrorWithStatus:@"请检查您的网络状态"];
                return;
            }
            [SVProgressHUD dismiss];
            if ([model.status isEqualToString:@"0"]) {
                NSLog(@"%@",model.code);
                // 判断设备是否支持指纹识别功能
                [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"startAutoLoginState"];
                
                //设置第一次登入为yes
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstlog"];
                //登录方式
                [getPeopleID getLoginType:@"passwordlogin"];
                //存储登录时间
                NSDate* loginDate = [NSDate date];
                [getPeopleID saveLoginTime:loginDate];
                
                
                
//                [getPeopleID getLoginAccount:self->_tf_Account.text];
//                [getPeopleID getLoginPassword:self->_tf_password.text];
                //保存用户昵称
                if (![Tools isBlankString:model.userinfo.cus_name]) {
                    [getPeopleID getPeopletitle:model.userinfo.cus_name];
                }
                //保存用户头像
                if (![Tools isBlankString:model.userinfo.photo]) {
                    [getPeopleID getPeoplePhoto:model.userinfo.photo];
                }
                
                
                //保存用户userId
                [getPeopleID getPeopleID:model.userinfo.ID];
                
                
                
                //存储手机号
                NSString *userBindTel = model.userinfo.telphone;
                if (![Tools isBlankString:userBindTel]) {
                    if ([model.userinfo.telphone containsString:@"undo"]) {
                        userBindTel = [model.userinfo.telphone stringByReplacingOccurrencesOfString:@"undo" withString:@""];
                    }
                    
                    [getPeopleID getPeopleIphoneNmber:userBindTel];
                }
                
                //用户是否授权
                if (model.userinfo.account_type == 0) {
                    [getPeopleID getpeopleLimitedAuthority:YES];
                }else
                {
                    [getPeopleID getpeopleLimitedAuthority:NO];
                }
                
                
                
                //保存用户所在省份
                NSString *provinceName = model.userinfo.provinceName;
                if ([Tools isBlankString:provinceName]) {
                    NSString *province = [UserDefault objectForKey:@"locationProvince"];
                    if ([Tools isBlankString:province]) {
                        [getPeopleID getProvince:@"上海市"];
                    }else{
                        [getPeopleID getProvince:province];
                    }
                }else{
                    [getPeopleID getProvince:provinceName];
                }
                
                
                //存储默认登录手机号
                if (![Tools isBlankString:model.userinfo.telphone]) {
                    [getPeopleID saveDefaultLoginAccount:model.userinfo.telphone];
                }
                //存储默认登录密码
//                if (![Tools isBlankString:model.userinfo.cus_password]) {
//
//                }
                
                
                [getPeopleID saveDefaultLoginPassword:[MyMD5 md5:self->_tf_password.text]];
                
                [getPeopleID socialSate:1];
                [UserDefault setObject:model.userinfo.token forKey:@"loginToken"];
                //[GeTuiSdk bindAlias:[NSString stringWithFormat:@"kksh_%ld",model.userinfo.ID] andSequenceNum:[NSString stringWithFormat:@"%ld",model.userinfo.ID]];
                
                
               
                
               
                
                [[NSNotificationCenter defaultCenter] postNotificationName:KKSBLoginSuccessNotificationName object:nil userInfo:nil];
                
                
                //初始化数据
                [[clickEvent instance] initData];
                [CTAPIBaseManager GET3:LoginCountUrl params:@{} completionHandler:^(id  _Nonnull responseObject, NSError * _Nonnull error) {

                           }];
                
                WeakSelf
                [weakSelf.navigationController dismissViewControllerAnimated:YES completion:^{
                    if (weakSelf.block) {
                        weakSelf.block(YES);
                    }
                }];
                NSLog(@"%@",@"密码登陆成功");
                [NetworkRequest appLogin:0];
            }else{
                
                codeSelected *code = [codeSelected MustTakeReturn:[model.status intValue]];
                [code returnCodeMethod];
               

                
            }
        }];
    
    
}


- (void)defaultLogin:(void (^)(bool success))complete;
{
    NSString *accountStr = [getPeopleID getDefaultLoginAccount];
    NSString *passwordStr = [getPeopleID getDefaultLoginPassword];
    
//    WeakSelf
    [logNetModel getLogMessage6:nil andTelephone:accountStr andLoginpassword:passwordStr andUid:nil andl:0 andweixinId:nil andPicId:nil completionHandle:^(lodModel *model, NSError *error) {
        
        if ([model.status isEqualToString:@"0"])
        {
            
            NSLog(@"%@",model.code);
                          
                  
            // 判断设备是否支持指纹识别功能
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"startAutoLoginState"];
                  
                  
            //设置第一次登入为yes
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstlog"];
            
            //存储登录方式
            [getPeopleID getLoginType:@"passwordlogin"];
                  
            //存储登录时间
            NSDate* loginDate = [NSDate date];
            [getPeopleID saveLoginTime:loginDate];
            
            
            
//                  [getPeopleID getLoginAccount:self->_tf_Account.text];
//                  [getPeopleID getLoginPassword:self->_tf_password.text];
                
            //存储用户昵称
            if (![Tools isBlankString:model.userinfo.cus_name]) {
                [getPeopleID getPeopletitle:model.userinfo.cus_name];
            }
            //存储头像
            NSString *userIcon = nil;
            if (![Tools isBlankString:model.userinfo.photo]) {
                userIcon = model.userinfo.photo;
            }else{
                userIcon = @"https://i.rensheju.com/userPhoto/defaultPhoto.png";
            }
            [getPeopleID getPeoplePhoto:userIcon];
            
            
            //userId
            [getPeopleID getPeopleID:model.userinfo.ID];
           
            //存储手机号
            NSString *userBindTel = model.userinfo.telphone;
            if (![Tools isBlankString:userBindTel]) {
                if ([model.userinfo.telphone containsString:@"undo"]) {
                    userBindTel = [model.userinfo.telphone stringByReplacingOccurrencesOfString:@"undo" withString:@""];
                }
                
                [getPeopleID getPeopleIphoneNmber:userBindTel];
            }
            
            
            
          
            //授权用户
            if (model.userinfo.account_type == 0) {
                [getPeopleID getpeopleLimitedAuthority:YES];
            }else
            {
                [getPeopleID getpeopleLimitedAuthority:NO];
            }
            
            
            //保存省份
            NSString *provinceName = model.userinfo.provinceName;
            if ([Tools isBlankString:provinceName]) {
               
                NSString *province = [UserDefault objectForKey:@"locationProvince"];
                if ([Tools isBlankString:province]) {
                    [getPeopleID getProvince:@"上海市"];
                }else{
                    [getPeopleID getProvince:province];
                }
            
            }else{
                
                [getPeopleID getProvince:provinceName];
            }
                  
        
                  
                  
          [UserDefault setObject:model.userinfo.token forKey:@"loginToken"];
          //[GeTuiSdk bindAlias:[NSString stringWithFormat:@"kksh_%ld",model.userinfo.ID] andSequenceNum:[NSString stringWithFormat:@"%ld",model.userinfo.ID]];
          //初始化数据
          [[clickEvent instance] initData];
          [getPeopleID socialSate:1];
          [[NSNotificationCenter defaultCenter] postNotificationName:KKSBLoginSuccessNotificationName object:nil userInfo:nil];
                  [CTAPIBaseManager GET3:LoginCountUrl params:@{} completionHandler:^(id  _Nonnull responseObject, NSError * _Nonnull error) {

                    }];
            complete(YES);
        }else{
            
            complete(NO);
            codeSelected *code = [codeSelected MustTakeReturn:[model.status intValue]];
            [code returnCodeMethod];
           
        }
    }];
}




- (void)pwdTextSwitch:(UIButton *)sender
{
    sender.selected = !sender.selected;
    
    if (sender.selected) { // 按下去了就是明文

    

           NSString *tempPwdStr = self.tf_password.text;

           self.tf_password.text = @""; // 这句代码可以防止切换的时候光标偏移

           self.tf_password.secureTextEntry = NO;

           self.tf_password.text = tempPwdStr;
        
        [self.btn_passwordHidden setImage:[UIImage imageNamed:@"login_passwordHidden"] forState:UIControlStateNormal];

    

       } else { // 暗文
           NSString *tempPwdStr = self.tf_password.text;

           self.tf_password.text = @"";

           self.tf_password.secureTextEntry = YES;

           self.tf_password.text = tempPwdStr;
           [_btn_passwordHidden setImage:[UIImage imageNamed:@"login_passwordShow"] forState:UIControlStateNormal];

       }
}


- (void)textFieldDidChange:(UITextField *)textField{
    
    if (_tf_Account.text.length >= 11 && _tf_password.text.length >= 6) {
        _loginBtn.userInteractionEnabled = YES;
        _loginBtn.backgroundColor = RGBCOLOR(51, 138, 208);
    }else{
        _loginBtn.userInteractionEnabled = NO;
        _loginBtn.backgroundColor = RGBCOLOR(197, 197, 197);
    }
    
    if ([textField isEqual:_tf_Account]) {
        if (textField.text.length >=11) {
            textField.text = [textField.text substringToIndex:11];
        }
        
        
    }else if ([textField isEqual:_tf_password]){
        if (textField.text.length >=18) {
            [SVProgressHUD showErrorWithStatus:@"只能输6到18位密码"];
            textField.text = [textField.text substringToIndex:18];
        }
        
    }
    
    if (self.tf_password.text.length == 0 || textField != self.tf_password) {
        self.btn_passwordHidden.hidden = YES;
    }else{
        self.btn_passwordHidden.hidden = NO;
    }
    
}


#pragma mark - 通知绑定方法
- (void)jiePing {
    [self alertShowMessage:@"[安全提醒]内含身份信息，只适合当面使用。不要截图或分享给他人以保障信息安全。"];
}

#pragma mark - 提示
- (void)alertShowMessage:(NSString *)msg
{
    if (@available(iOS 9.0,*)) {
        
        UIAlertController * alertV = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertV addAction:confirm];
        
        [self presentViewController:alertV animated:YES completion:nil];
        
    }else{
        UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alertV show];
    }
}

#pragma mark - getters and setters

-(LDDKeyboardView*)keyboardView{
    if (!_keyboardView) {
        _keyboardView=[LDDKeyboardView loadKeyboardFromNib];
    
        _keyboardView.frame=CGRectMake(0, 0, ScreenWidth, 260);
        
        //1,第一种方式使用代理
        _keyboardView.delegate= self;
        
        //2,第二种方式使用block
        /**
        
        __weak typeof(self) weakSelf=self;
        _keyboardView.clickBlock = ^(UIButton *button) {
            [weakSelf clickKeyboardButton:button];
        };
         
         */
    }
    return _keyboardView;
}

- (UILabel *)lb_Title
{
    if (!_lb_Title) {
        _lb_Title = [[UILabel alloc]init];
        _lb_Title.text = @"账号密码登录";
        _lb_Title.textColor = [UIColor colorWithHex:0x4c4c4c];
        _lb_Title.textAlignment = NSTextAlignmentLeft;
        _lb_Title.font = [UIFont systemFontOfSize:27*WIDTHRADIU];
    }
    return _lb_Title;
}

- (UILabel *)lb_InternationalAreaCode
{
    if (!_lb_InternationalAreaCode) {
           _lb_InternationalAreaCode = [[UILabel alloc]init];
           _lb_InternationalAreaCode.text = @"+86";
           _lb_InternationalAreaCode.textColor = [UIColor colorWithHex:0x333333];
           _lb_InternationalAreaCode.textAlignment = NSTextAlignmentLeft;
           _lb_InternationalAreaCode.font = [UIFont systemFontOfSize:17*WIDTHRADIU];
       }
       return _lb_InternationalAreaCode;
}

- (UILabel *)lb_horizontalDividingLine
{
    if (!_lb_horizontalDividingLine) {
        _lb_horizontalDividingLine = [[UILabel alloc]init];
        _lb_horizontalDividingLine.backgroundColor = [UIColor colorWithHex:0xcccccc];
    }
    return _lb_horizontalDividingLine;
}

- (UITextField *)tf_Account
{
    if (!_tf_Account) {
        _tf_Account = [[UITextField alloc]init];
        _tf_Account.keyboardType = UIKeyboardTypeNumberPad;
        _tf_Account.borderStyle = UITextBorderStyleNone;
        _tf_Account.clearButtonMode = UITextFieldViewModeWhileEditing;
        
        
        if (@available(iOS 13.0,*)) {
                        NSMutableAttributedString *placeholderFont = [[NSMutableAttributedString alloc] initWithString:@"请输入账号" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17.0 * WIDTHRADIU],NSForegroundColorAttributeName : RGBCOLOR(153, 153, 153)}];
                               
                               _tf_Account.attributedPlaceholder = placeholderFont;
                    }else{
                         _tf_Account.placeholder = @"请输入账号";
                         [_tf_Account setValue:[UIFont systemFontOfSize:17.0 * WIDTHRADIU] forKeyPath:@"_placeholderLabel.font"];
                        [_tf_Account setValue:RGBCOLOR(153, 153, 153) forKeyPath:@"_placeholderLabel.textColor"];
                    }
        
        _tf_Account.delegate = self;
        
        XTYRandomKeyboard *keyBoad = [[XTYRandomKeyboard alloc] initWithTitleColor:[UIColor blackColor] backGroundImage:[UIImage imageNamed:@"bg.jpg"]];
        [keyBoad setInputView:_tf_Account];
        
    }
    return _tf_Account;
}

- (UIView *)accountLine
{
    if (!_accountLine) {
        _accountLine = [[UIView alloc]init];
        _accountLine.backgroundColor = RGBCOLOR(204, 204, 204);
    }
    return _accountLine;
}

- (UITextField *)tf_password
{
    if (!_tf_password) {
        _tf_password = [[UITextField alloc]init];
        _tf_password.borderStyle = UITextBorderStyleNone;
       
        
        if (@available(iOS 13.0,*)) {
                               NSMutableAttributedString *placeholderFont = [[NSMutableAttributedString alloc] initWithString:@"请输入密码" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17.0 * WIDTHRADIU],NSForegroundColorAttributeName : RGBCOLOR(153, 153, 153)}];
                                      
                                      _tf_password.attributedPlaceholder = placeholderFont;
                           }else{
                                _tf_password.placeholder = @"请输入密码";
                                [_tf_password setValue:[UIFont systemFontOfSize:17.0 * WIDTHRADIU] forKeyPath:@"_placeholderLabel.font"];
                               [_tf_password setValue:RGBCOLOR(153, 153, 153) forKeyPath:@"_placeholderLabel.textColor"];
                           }
        _tf_password.delegate = self;
        _tf_password.secureTextEntry = YES;
        //_tf_password.inputView =[[RandomCustomKeyborad alloc] initWithInputView:_tf_password];
        _tf_password.inputView = self.keyboardView.inputView;
        //_tf_password.inputAccessoryView=self.keyboardView.accessoryView;
        
    }
    return _tf_password;
}


- (UIButton *)btn_passwordHidden
{
    if (!_btn_passwordHidden) {
        _btn_passwordHidden = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btn_passwordHidden setImage:[UIImage imageNamed:@"login_passwordShow"] forState:UIControlStateNormal];
        [_btn_passwordHidden addTarget:self action:@selector(pwdTextSwitch:) forControlEvents:UIControlEventTouchUpInside];
        _btn_passwordHidden.hidden = YES;
    }
    return _btn_passwordHidden;
}



- (UIView *)passwordLine
{
    if (!_passwordLine) {
        _passwordLine = [[UIView alloc]init];
        _passwordLine.backgroundColor = RGBCOLOR(204, 204, 204);
    }
    return _passwordLine;
}

- (UIButton *)btn_forgetPassword
{
    if (!_btn_forgetPassword) {
        _btn_forgetPassword = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btn_forgetPassword setTitle:@"找回密码" forState:UIControlStateNormal];
        [_btn_forgetPassword setTitleColor:RGBCOLOR(153, 153, 153) forState:UIControlStateNormal];
        _btn_forgetPassword.titleLabel.font = [UIFont systemFontOfSize:14*WIDTHRADIU];
        [_btn_forgetPassword addTarget:self action:@selector(resetPasswordClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn_forgetPassword;
}

- (UIButton *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_loginBtn.titleLabel setFont:[UIFont systemFontOfSize:16*WIDTHRADIU]];
        [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_loginBtn setBackgroundColor:RGBCOLOR(142, 142, 142)];
        _loginBtn.layer.cornerRadius = 5*WIDTHRADIU;
        [_loginBtn addTarget:self action:@selector(loginClick:) forControlEvents:UIControlEventTouchUpInside];
        _loginBtn.userInteractionEnabled = NO;
    }
    return _loginBtn;
}




- (LoginView *)wxLogin
{
    if (!_wxLogin) {
        _wxLogin = [[LoginView alloc]initWithTitle:@"微信登录" imageName:@"login_otherlogin_WeChat_normal-1"];
        _wxLogin.delegate = self;
    }
    return _wxLogin;
}
- (LoginView *)directlyLogin
{
    if (!_directlyLogin) {
        _directlyLogin = [[LoginView alloc]initWithTitle:@"一键登录" imageName:@"login_otherlogin_One-click_normal"];
        _directlyLogin.delegate = self;
    }
    return _directlyLogin;
}


- (LoginView *)smsLogin
{
    if (!_smsLogin) {
        _smsLogin = [[LoginView alloc]initWithTitle:@"短信登录" imageName:@"login_otherlogin_SMS_normal"];
        _smsLogin.delegate = self;
    }
    return _smsLogin;
}


- (UIView *)leftLine
{
    if (!_leftLine) {
        _leftLine = [[UIView alloc]init];
        _leftLine.backgroundColor = RGBCOLOR(204, 204, 204);
    }
    return _leftLine;
}

- (UILabel *)middleLab
{
    if (!_middleLab) {
        _middleLab = [[UILabel alloc]init];
        _middleLab.textColor = RGBACOLOR(153, 153, 153, 1.0);
        _middleLab.text = @"其他登录方式";
        _middleLab.textAlignment = NSTextAlignmentCenter;
        _middleLab.font = [UIFont systemFontOfSize:13*WIDTHRADIU];
    }
    return _middleLab;
}

- (UIView *)rightLine
{
    if (!_rightLine) {
        _rightLine = [[UIView alloc]init];
        _rightLine.backgroundColor = RGBCOLOR(204, 204, 204);
    }
    return _rightLine;
}
/*
#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
