//
//  MyBandCardViewController.m
//  ALand
//
//  Created by Z on 16/5/5.
//  Copyright © 2016年 Z. All rights reserved.
//

#import "MyBandCardViewController.h"
#import "MyBandBottomView.h"
#import "AddBankCardIDViewController.h"
#import "UITableView+Category.h"
#import "BankTableViewCell.h"
#import "ChangeMyBankCardViewController.h"
#define kCellID @"cellID"
@interface MyBandCardViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIImageView *imgNoCardBackview;
@property (nonatomic, strong) MyBandBottomView *bottomView;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation MyBandCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的银行卡";
    [self setUpNoCardBackroundView];
    [self setUpTableView];
    [self addBottomView];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self getReseller];
}

- (void)setUpTableView {
    self.tableView = [UITableView plainTableViewWithTarget:self cellNibName:@"BankTableViewCell" cellId:kCellID];
    self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
    self.tableView.backgroundColor = kBacroundColor;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.frame = CGRectMake(0, 0, kWidth, kHeight - 60 - 64);
    [self.view addSubview:self.tableView];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 130;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.reseller.certificate.bankCards.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BankTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID forIndexPath:indexPath];
    JavenBankCards *bankModel = self.reseller.certificate.bankCards[indexPath.row];
    [cell cellWithModel:bankModel];
    return cell;
}


//没有银行卡的背景
- (void)setUpNoCardBackroundView {
    self.imgNoCardBackview = [UIImageView new];
    [self.view addSubview:self.imgNoCardBackview];
    self.imgNoCardBackview.contentMode = UIViewContentModeScaleAspectFit;
    self.imgNoCardBackview.image = [UIImage imageNamed:@"mine_nocard_backround"];
    self.imgNoCardBackview.sd_layout.centerXEqualToView(self.view).centerYIs(self.view.centerY - 100).leftSpaceToView(self.view, 80).rightSpaceToView(self.view, 80).heightIs(600);
    
}

//添加底部添加按钮
- (void)addBottomView {
    self.bottomView = [MyBandBottomView viewWithNibName:@"MyBandBottomView"];
    self.bottomView.frame = CGRectMake(0, kHeight - 60 - 64, kWidth, 60 + 64);
    kButtonAddAction(self.bottomView.btnAdd, goAddBankCardID:);
    [self.view addSubview:self.bottomView];
    
}

- (void)goAddBankCardID:(UIButton *)btn {
    btn.highlighted = NO;
    if (self.bottomView.btnAdd.isSelected) {
        //修改银行卡
        ChangeMyBankCardViewController *changeVC = [[ChangeMyBankCardViewController alloc] init];
        changeVC.isShowNavigationBar = YES;
        changeVC.reseller = self.reseller;
        [self PushViewController:changeVC animated:YES];
    }else{
        //添加银行卡
        AddBankCardIDViewController *addBankIDVC = [[AddBankCardIDViewController alloc] init];
        addBankIDVC.isShowNavigationBar = YES;
        addBankIDVC.reseller = self.reseller;
        [self.navigationController pushViewController:addBankIDVC animated:YES];
    }
}

- (void)getReseller{
    WS(weakSelf)
    [[HTTPCilent shareCilent] GET:[NSString stringWithFormat:@"reseller/get/%@", [UserInfo shareInstance].reseller.uuid] parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject[@"code"] isEqualToNumber:@0]) {
            weakSelf.reseller = [JavenResellerBigModel modelObjectWithDictionary:responseObject[@"reseller"]];
            [weakSelf.tableView reloadData];
            weakSelf.bottomView.btnAdd.selected = self.reseller.certificate.bankCards.count > 0;

        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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