//
//  SignPasswayController.m
//  Open
//
//  Created by 雷俊博 on 2020/12/2.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "SignPasswayController.h"
#import "BCSignBottomView.h"
#import "UIImageView+WebCache.h"
#import "UIImage+Custom.h"
#import "SMSVerificationController.h"

@interface SignPasswayController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView* tableView;

@property (nonatomic, strong)BCSignBottomView* bottomView;


@end

@implementation SignPasswayController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithHex:0xf4f4f4];
    
    [self.view addSubview:self.tableView];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    };
    
    UIButton* nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    nextBtn.frame = CGRectMake(10, cl_kNavigationBarHeight + 140, ScreenWidth - 20, 45);
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    nextBtn.backgroundColor = K_MainColor;
    [nextBtn addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    nextBtn.layer.cornerRadius = 5;
    [self.view addSubview:nextBtn];
    
    [self.view addSubview:self.bottomView];
    
}



#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.backgroundColor = [UIColor whiteColor];
    }
    if (indexPath.row == 0) {
        cell.textLabel.text = @"付款账户";
        if (self.bcInfoModel.cardList.count > 0) {
            BCCardListModel* cardModel = self.bcInfoModel.cardList[0];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@%@",cardModel.bankCardName,cardModel.lastFourCardNo];
        }
        
        
    }else if (indexPath.row == 1){
        cell.textLabel.text = @"预留手机号";
        if (self.bcInfoModel.cardList.count > 0) {
            BCCardListModel* cardModel = self.bcInfoModel.cardList[0];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@****%@",[cardModel.bankCardPhone substringToIndex:3] ,[cardModel.bankCardPhone substringFromIndex:cardModel.bankCardPhone.length - 4]];
        }
    }
    cell.textLabel.textColor = [UIColor colorWithHex:0x4d4d4d];
    cell.detailTextLabel.textColor = [UIColor colorWithHex:0x808080];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

#pragma mark - event response
- (void)nextBtnClick:(UIButton *)sender {
    
    if ([self.signType isEqualToString:@"Z"]) {
        SMSVerificationController*vc = [SMSVerificationController new];
        vc.title = @"输入代收通道签约验证码";
        vc.bcAccountInfoModel = self.bcInfoModel;
        vc.type = SMSSendTypeBCSignAtiveVerif;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}
- (void)phoneBtnClick:(UIButton *)sender {
    if (self.bcInfoModel.custServiceHotLine.count > 0) {
        if (![Tools isBlankString:self.bcInfoModel.custServiceHotLine[0]]) {
            NSString *callPhone = [NSString stringWithFormat:@"telprompt:%@",self.bcInfoModel.custServiceHotLine[0]];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
        }else{
            [Tools showMessage:@"暂无客服电话"];
        }
    }else{
        [Tools showMessage:@"暂无客服电话"];
    }
}

#pragma mark - getters and setters
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, cl_kNavigationBarHeight, ScreenWidth, 90) style:UITableViewStyleGrouped];
        
        _tableView.showsVerticalScrollIndicator = NO;
        
        _tableView.dataSource = self;
        _tableView.delegate = self;
        
        _tableView.estimatedSectionHeaderHeight = 10;
        
    }
    return _tableView;
}

- (BCSignBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[NSBundle mainBundle]loadNibNamed:@"BCSignBottomView" owner:self options:nil].lastObject;
        _bottomView.backgroundColor = [UIColor clearColor];
        _bottomView.frame = CGRectMake(0, ScreenHeight - 134, ScreenWidth, 134);
        [_bottomView.bankBtn setTitle:self.bcInfoModel.orgName forState:UIControlStateNormal];
//        [_bottomView.bankBtn sd_setImageWithURL:[NSURL URLWithString:self.bcInfoModel.orgLogo] forState:UIControlStateNormal];
        [_bottomView.bankBtn layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleLeft imageTitleSpace:5];
        UIImageView *img = [UIImageView new];
        [img sd_setImageWithURL:[NSURL URLWithString:self.bcInfoModel.orgLogo] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [self->_bottomView.bankBtn setImage:[UIImage imageWithImage:image scaledToSize:CGSizeMake(30, 30)] forState:UIControlStateNormal];
        }];
        
       
        [_bottomView.phoneBtn addTarget:self action:@selector(phoneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _bottomView;
}

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