//
//  logInputView.m
//  YBEducation
//
//  Created by IOS1 on 2020/2/22.
//  Copyright © 2020 IOS1. All rights reserved.
//

#import "logInCodePutView.h"

@implementation logInCodePutView{
    UITextField *nameT;
    UITextField *pwdT;
    UIButton *logBtn;
    UIButton *codeBtn;
    NSTimer *messsageTimer;
    int messageIssssss;//短信倒计时  60s

}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self creatUI];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeBtnBackground) name:UITextFieldTextDidChangeNotification object:nil];

    }
    return self;
}
- (void)ChangeBtnBackground{
    if (nameT.text.length == 11 && pwdT.text.length == 6) {
        logBtn.alpha = 1;
        logBtn.userInteractionEnabled = YES;
    }else{
        logBtn.alpha = 0.5;
        logBtn.userInteractionEnabled = NO;
    }
    if (nameT.text.length == 11) {
        [codeBtn setTitleColor:normalColors forState:0];
        codeBtn.userInteractionEnabled = YES;
    }else{
        [codeBtn setTitleColor:RGB_COLOR(@"#c9c9c9", 1) forState:0];
        codeBtn.userInteractionEnabled = NO;

    }
}
- (void)creatUI{
    NSArray *array = @[@"请输入您的手机号",@"请输入验证码"];
    for (int i = 0; i < array.count; i ++) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, i * 70, self.width, 70)];
        [self addSubview:view];
        UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(25, 30, self.width-50, 40)];
        text.placeholder = array[i];
        text.font = SYS_Font(16);
        text.keyboardType = UIKeyboardTypeNumberPad;
        [view addSubview:text];
        [[WYToolClass sharedInstance] lineViewWithFrame:CGRectMake(text.left, 69, text.width, 1) andColor:RGB_COLOR(@"#E8E8E8", 1) andView:view];
        if (i == 0) {
            nameT = text;
        }else{
            text.width = (self.width-50)/2;
            pwdT = text;
            codeBtn = [UIButton buttonWithType:0];
            [codeBtn setTitle:@"获取验证码" forState:0];
            [codeBtn setTitleColor:RGB_COLOR(@"#c9c9c9", 1) forState:0];
            codeBtn.titleLabel.font = SYS_Font(16);
            [codeBtn addTarget:self action:@selector(getCode) forControlEvents:UIControlEventTouchUpInside];
            codeBtn.userInteractionEnabled = NO;
            [view addSubview:codeBtn];
            [codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(view).offset(-25);
                make.bottom.height.equalTo(text);
            }];
        }
    }
    logBtn = [UIButton buttonWithType:0];
    [logBtn setTitle:@"登录" forState:0];
    [logBtn setBackgroundImage:[UIImage imageNamed:@"buttonBack"] forState:0];
    [logBtn addTarget:self action:@selector(doLogin) forControlEvents:UIControlEventTouchUpInside];
    logBtn.frame = CGRectMake(25, 175, _window_width-50, 45);
    logBtn.layer.cornerRadius = 22.5;
    logBtn.layer.masksToBounds = YES;
    logBtn.clipsToBounds = YES;
    logBtn.alpha = 0.5;
    logBtn.userInteractionEnabled = NO;
    [self addSubview:logBtn];

}
- (void)getCode{
    codeBtn.userInteractionEnabled = NO;
    NSString *sign = [WYToolClass sortString:@{@"account":nameT.text}];
    [WYToolClass postNetworkWithUrl:@"Login.GetCode" andParameter:@{@"account":nameT.text,@"sign":sign,@"type":@"0"} success:^(int code, id  _Nonnull info, NSString * _Nonnull msg) {
        [MBProgressHUD showError:msg];

        if (code == 0) {
            if (messsageTimer == nil) {
                messageIssssss = 60;
                messsageTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(daojishi) userInfo:nil repeats:YES];
            }
            [pwdT becomeFirstResponder];
        }else{
            if (code == 1002) {
                [pwdT becomeFirstResponder];
            }
            codeBtn.userInteractionEnabled = YES;

        }
    } fail:^{
        codeBtn.userInteractionEnabled = YES;
    }];

}
//获取验证码倒计时
-(void)daojishi{
    messageIssssss-=1;
    [codeBtn setTitle:[NSString stringWithFormat:@"%ds",messageIssssss] forState:UIControlStateNormal];
    codeBtn.userInteractionEnabled = NO;
    
    if (messageIssssss<=0) {
        [codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        codeBtn.userInteractionEnabled = YES;
        [messsageTimer invalidate];
        messsageTimer = nil;
        messageIssssss = 60;
    }
}
- (void)doLogin{
    [self endEditing:YES];
    [MBProgressHUD showMessage:@"正在登录"];
    NSString *pushid = @"";

    NSDictionary *Login = @{
                            @"username":nameT.text,
                            @"code":pwdT.text,
                            @"source":@"2",
                            @"pushid":pushid
                            };
    
    [WYToolClass postNetworkWithUrl:@"Login.LoginByCode" andParameter:Login success:^(int code, id  _Nonnull info, NSString * _Nonnull msg) {
        [MBProgressHUD hideHUD];
        if (code == 0) {
            NSDictionary *infos = [info objectAtIndex:0];
            if (![minstr([infos valueForKey:@"gradeid"]) isEqual:@"0"]) {
                NSDictionary *gdic = @{@"id":minstr([infos valueForKey:@"gradeid"]),@"name":minstr([infos valueForKey:@"gradename"])};
                [[NSUserDefaults standardUserDefaults] setObject:gdic forKey:@"selectClassIteam"];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }

            if (self.block) {
                self.block();
            }
        }else {
            [MBProgressHUD showError:msg];
        }

    } fail:^{
        [MBProgressHUD hideHUD];
    }];

}
@end
