//
//  BCRealNameInfoIdentifyController.m
//  Open
//
//  Created by 雷俊博 on 2020/11/3.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "BCRealNameInfoIdentifyController.h"
#import "BCRealNameCell.h"
#import "UIView+frameAdjust.h"

#import "SMSVerificationController.h"

@interface BCRealNameInfoIdentifyController ()<UITextFieldDelegate>{
    CGFloat currentMaxY;
}

@property (nonatomic, strong)UIScrollView* scrollView;

@property (nonatomic, strong)UITextField* tf_cardNo;

@property (nonatomic, strong)UITextField* tf_cardMobile;

@property (nonatomic, strong)UIButton* nextBtn;

@end

@implementation BCRealNameInfoIdentifyController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithHex:0xf4f4f4];
    
    [self.view addSubview:self.scrollView];
    
}

#pragma mark - UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    

    [self refreshBtnState];
    
    
    return YES;
    
}

#pragma mark - 刷新按钮状态
- (void)refreshBtnState
{
    if ( self.tf_cardNo.text.length  <= 3 || self.tf_cardMobile.text.length < 10) {
        self.nextBtn.backgroundColor = RGBCOLOR(197, 197, 197);
        self.nextBtn.userInteractionEnabled =NO;
    }else{
       self.nextBtn.backgroundColor =[UIColor colorWithRed:25/255.0 green:149/255.0 blue:109/255.0 alpha:1.0];
       self.nextBtn.userInteractionEnabled =YES;
    }
}

#pragma mark - 比财实名
- (NSDictionary *)realNameAuthenticationRequestPara
{
    NSMutableDictionary *params = [NetworkPublicParams getBCPublicParams];
    
    [params setObject:[getPeopleID PeopleIphoneNmber] forKey:@"loginPhoneNum"];
    [params setObject:self.idcardBackPhoto forKey:@"idcardBackPhoto"];
    [params setObject:self.idcardFrontPhoto forKey:@"idcardFrontPhoto"];
    [params setObject:@"2" forKey:@"transcoding"];
    [params setObject:self.tf_cardNo.text forKey:@"bankCardNo"];
    [params setObject:self.tf_cardMobile.text forKey:@"mobile"];
    
    
    
    return params;
}

//比财
- (void)realNameAuthenticationRequest
{
    NSString* url = [NSString stringWithFormat:@"%@%@",[NetworkConfig ipYinShuConfig],BCOCRImageUploadRequest];
    NSLog(@"比财实名url:%@",url);
    
    
    
    NSLog(@"params:%@",[self realNameAuthenticationRequestPara]);
    
    [KKLoadingAnimationView show];
    [NetworkRequest Post6:url Params:[self realNameAuthenticationRequestPara] Success:^(id _Nonnull objc) {
        [KKLoadingAnimationView dismiss];
        NSLog(@"objc:%@",objc);
        NSString *state = [NSString stringWithFormat:@"%@",objc[@"state"]];
        NSString *describe = [NSString stringWithFormat:@"%@",objc[@"describe"]];
        if ([state isEqualToString:@"200"]) {
            NSString *codeVerify = [NSString stringWithFormat:@"%@",objc[@"result"][@"codeVerify"]];
            
            if ([codeVerify isEqualToString:@"Y"]) {
                SMSVerificationController*vc = [SMSVerificationController new];
                vc.title = @"实名短信验证";
                vc.idcardBackPhoto = self.idcardBackPhoto;
                vc.idcardFrontPhoto = self.idcardFrontPhoto;
                vc.bankNo = self.tf_cardNo.text;
                vc.bcPrePhone = self.tf_cardMobile.text;
                vc.realName = self.idCardName;
                vc.idcardNo = self.idcardNo;
                vc.type = SMSSendTypeBCSystemIdentify;
                vc.bcInfoModel = self.bcInfoModel;
        
                [self.navigationController pushViewController:vc animated:YES];
            }else if ([codeVerify isEqualToString:@"N"]) {
                
            }
            
        }else{
            [Tools showMessage:describe];
        }

    } Failure:^(NSError* error) {
        [KKLoadingAnimationView dismiss];
        NSLog(@"error:%@",error);
        
        [Tools showMessage:@"网络出小差了,请稍后重试"];
        
    }];
    
}

#pragma mark - event reseponse
- (void)nextBtnClick:(UIButton *)sender
{
    self.tf_cardMobile.text = [self.tf_cardMobile.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    self.tf_cardNo.text = [self.tf_cardNo.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (![Tools isMobileNumber:self.tf_cardMobile.text]) {
        [Tools showMessage:@"手机号码有误"];
        return;
    }
    
    [self realNameAuthenticationRequest];
}


#pragma mark - getters
- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, cl_kNavigationBarHeight, ScreenWidth, ScreenHeight - cl_kNavigationBarHeight)];
        _scrollView.showsVerticalScrollIndicator = NO;
        //_scrollView.delegate = self;
        _scrollView.contentSize = CGSizeMake(ScreenWidth, 0);
        
        UILabel *lb_title = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, ScreenWidth - 30, 30)];
        lb_title.text = @"个人信息";
        lb_title.font = [UIFont systemFontOfSize:13];
        lb_title.textColor = [UIColor colorWithHex:0x4d4d4d];
        [_scrollView addSubview:lb_title];
        currentMaxY = lb_title.maxY;
        
        BCRealNameCell *nameCell = [[NSBundle mainBundle]loadNibNamed:@"BCRealNameCell" owner:nil options:nil].lastObject;
        nameCell.frame = CGRectMake(0, currentMaxY, ScreenWidth, 50);
        nameCell.tf_content.userInteractionEnabled = NO;
        nameCell.lb_title.text = @"姓 名";
        nameCell.tf_content.text = [NSString stringWithFormat:@"%@**",[self.idCardName substringToIndex:1]];
        [_scrollView addSubview:nameCell];
        currentMaxY = nameCell.maxY;
        
        
        BCRealNameCell *idCell = [[NSBundle mainBundle]loadNibNamed:@"BCRealNameCell" owner:nil options:nil].lastObject;
        idCell.frame = CGRectMake(0, currentMaxY, ScreenWidth, 50);
        idCell.tf_content.userInteractionEnabled = NO;
        idCell.lb_title.text = @"身份证号";
        idCell.tf_content.text = [NSString stringWithFormat:@"%@*************%@",[self.idcardNo substringToIndex:3],[self.idcardNo substringFromIndex:self.idcardNo.length - 2]];
        idCell.line.hidden = YES;
        [_scrollView addSubview:idCell];
        currentMaxY = idCell.maxY;
        
        
        UILabel *lb_title2 = [[UILabel alloc]initWithFrame:CGRectMake(15, currentMaxY, ScreenWidth - 30, 30)];
        lb_title2.text = @"请输入银行卡号和预留手机号";
        lb_title2.font = [UIFont systemFontOfSize:13];
        lb_title2.textColor = [UIColor colorWithHex:0x4d4d4d];
        [_scrollView addSubview:lb_title2];
        currentMaxY = lb_title2.maxY;
        
        BCRealNameCell *cardCell = [[NSBundle mainBundle]loadNibNamed:@"BCRealNameCell" owner:nil options:nil].lastObject;
        cardCell.frame = CGRectMake(0, currentMaxY, ScreenWidth, 50);
        cardCell.lb_title.text = @"银行卡号";
        cardCell.tf_content.placeholder = @"请输入银行卡号";
        [_scrollView addSubview:cardCell];
        currentMaxY = cardCell.maxY;
        
        
        
        BCRealNameCell *phoneCell = [[NSBundle mainBundle]loadNibNamed:@"BCRealNameCell" owner:nil options:nil].lastObject;
        phoneCell.frame = CGRectMake(0, currentMaxY, ScreenWidth, 50);
        phoneCell.lb_title.text = @"预留手机号";
        phoneCell.tf_content.placeholder = @"请输入预留手机号";
        [_scrollView addSubview:phoneCell];
        currentMaxY = phoneCell.maxY;
        
        self.tf_cardNo = cardCell.tf_content;
        self.tf_cardMobile = phoneCell.tf_content;
        
        self.tf_cardNo.delegate = self;
        self.tf_cardMobile.delegate = self;
        
        
        UIButton* nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        nextBtn.frame = CGRectMake(15, currentMaxY + 30, ScreenWidth - 30, 45);
        nextBtn.backgroundColor = RGBCOLOR(197, 197, 197);
        [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
        [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        nextBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        nextBtn.layer.cornerRadius = 8;
        [nextBtn addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        //nextBtn.userInteractionEnabled = NO;
        [_scrollView addSubview:nextBtn];
        
        self.nextBtn = nextBtn;
        
        currentMaxY = nextBtn.maxY;
        
        
        _scrollView.contentSize = CGSizeMake(ScreenWidth, currentMaxY + 10);
    }
    return _scrollView;
}

/*
#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
