//
//  SavePhoneModtifyController.m
//  Open
//
//  Created by 雷俊博 on 2020/8/26.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "SavePhoneModtifyController.h"
#import "SavePhoneMotifyView.h"
#import "SMSVerificationController.h"
#import "KBankListModel.h"

@interface SavePhoneModtifyController ()

@property (nonatomic, strong)UIScrollView* scrollView;

@property (nonatomic, strong)SavePhoneMotifyView* topView;

@property (nonatomic, assign)BOOL textIsEmpty;

@end

@implementation SavePhoneModtifyController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:0 target:nil action:nil];
    [self.navigationItem setBackBarButtonItem:bar];
    
    [GTCountSDK trackCountEvent:@"save_wudangChangeBindPhoneCount" withArgs:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithHex:0xf4f4f4];
    
    SavePhoneMotifyView*topView = [self getXiblWithClassName:@"SavePhoneMotifyView"];
    [topView.nextBtn addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:topView];
    self.topView = topView;
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(ScreenWidth);
        make.height.mas_greaterThanOrEqualTo(70);
    }];
    
    //初始化
    self.textIsEmpty = YES;
    [self setButtonState];
    
    SaveAccountSubUserInfoModel*userInfo = self.infoModel.SUBPACKS[0];
    topView.lb_bank.text = [NSString stringWithFormat:@"%@(%@)",[self returnBankName:userInfo.SIGCARD],[userInfo.SIGCARD substringFromIndex:userInfo.SIGCARD.length - 4]];
    
    [self.topView.tf_phone addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
}

#pragma mark - textFieldDidChange
- (void)textFieldDidChange
{
    if (self.topView.tf_phone.text.length == 11) {
        self.textIsEmpty = NO;
    }else{
        self.textIsEmpty = YES;
    }
    [self setButtonState];
}
#pragma mark - public methods
-(id)getXiblWithClassName:(NSString *)className
{
    return [[NSBundle mainBundle]loadNibNamed:className owner:self options:nil].lastObject;
}

#pragma mark - event response
- (void)nextBtnClick:(UIButton *)sender
{
    [GTCountSDK trackCountEvent:@"save_wudangChangeBindPhoneSMSCount" withArgs:nil];
    SMSVerificationController*vc = [SMSVerificationController new];
    vc.title = @"更换预留手机号码短信验证";
    vc.infoModel = self.infoModel;
    vc.phone = self.topView.tf_phone.text;
    vc.type = SMSSendTypePhoneModtify;
    
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 刷新按钮状态
- (void)setButtonState {
    if (self.textIsEmpty == NO) {
        self.topView.nextBtn.userInteractionEnabled = YES;
        self.topView.nextBtn.alpha = 1;
    } else {
        self.topView.nextBtn.userInteractionEnabled = NO;
        self.topView.nextBtn.alpha = 0.5;
    }
}

#pragma mark - 根据卡号获取银行名称
//根据卡号判断银行
- (NSString *)returnBankName:(NSString *)cardName {
    
    
    if (cardName.length < 6) {
        return @"";
    }
    NSString*bankName = [KBankListModel getBankNameWithStr:cardName];
    
    return bankName;
    
}

#pragma mark - getters and setters


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