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

#import "AddDetailInfoViewController.h"
#import "MyBankClass.h"
#import "JavenBankCards.h"
#import "MyBandCardViewController.h"
@interface AddDetailInfoViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *labelBankName;
@property (weak, nonatomic) IBOutlet UILabel *labelBankCardID;
@property (weak, nonatomic) IBOutlet UITextField *labelName;
@property (weak, nonatomic) IBOutlet UIButton *btnNext;
@property (weak, nonatomic) IBOutlet UIButton *btnAgree;


@end

@implementation AddDetailInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setDefauts];
    [self machineStrings];
    
    
    // Do any additional setup after loading the view from its nib.
}

//设置默认的一些信息
- (void)setDefauts {
    self.title = @"添加银行卡";
    self.labelName.delegate = self;
    
}


//对文字进行加工并赋值
- (void)machineStrings {
    self.labelBankName.text = [MyBankClass returnBankName:self.bankCardID];
    self.labelBankCardID.text = [NSString stringWithFormat:@"***************%@",[self.bankCardID substringWithRange:NSMakeRange(self.bankCardID.length - 4, 4)]];
}




//检测下一步是否可以点
- (void)checkNextBtnIsEnabledWithName:(NSString *)name {
    self.btnNext.enabled = name.length > 0;
}


- (IBAction)actionComplete:(id)sender {
    [self changeResellerInfoWithDic:[self getParams]];
}

- (NSDictionary *)getParams {
    
    JavenBankCards *bankCard;
    if (self.reseller.certificate.bankCards.count > 0) {
        bankCard = self.reseller.certificate.bankCards[0];
    }else{
        bankCard = [[JavenBankCards alloc] init];
    }
    bankCard.cardNo = self.bankCardID;
    bankCard.bank = [self.labelBankName.text componentsSeparatedByString:@"·"][0];
    bankCard.carrier = self.labelName.text;
    NSDictionary *dic = [bankCard dictionaryRepresentation];
    self.reseller.certificate.bankCards = @[dic];
    
    NSDictionary *params = [self getParamsWithReseller:self.reseller];
    return params;
}

/** 把修改的信息上传到服务器 */
- (void)changeResellerInfoWithDic:(NSDictionary *)params
{
    [MBProgressHUD Javen_showMessage:@"提交中…" onView:self.view];
    WS(weakSelf)
    [[HTTPCilent shareCilent] POST:@"reseller/save" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        [MBProgressHUD hideHUDForView:weakSelf.view];
        
        if ([responseObject[@"code"] isEqualToNumber:@0]) {
            [MBProgressHUD Javen_showSuccess:@"设置成功！" onView:weakSelf.view  delay:0.7 complete:nil];
            
            for (UIViewController *vc in weakSelf.navigationController.viewControllers) {
                if ([vc isKindOfClass:[MyBandCardViewController class]]) {
                    [weakSelf.navigationController popToViewController:vc animated:YES];
                }
            }
            
            
        }else{
            [MBProgressHUD Javen_showSuccess:responseObject[@"message"] onView:weakSelf.view  delay:0.7 complete:^{
            }];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [MBProgressHUD hideHUDForView:weakSelf.view];

    }];
}

- (NSDictionary *)getParamsWithReseller:(JavenResellerBigModel *)resellerModel {
    UserInfo *user = [UserInfo shareInstance];
    NSDictionary *reseller = [resellerModel dictionaryRepresentation];
    NSDictionary *params = @{@"operCtx" : @{@"time" : [[NSDate date] timeStampNumber],
                                            @"domain" : kDomain,
                                            @"operator" : @{@"id" : user.uuid,
                                                            @"fullName" : user.name}},
                             @"reseller" : reseller};
    return params;
}

#pragma mark *********** UITextFiedDelegate ***********

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *name = [textField.text stringByReplacingCharactersInRange:range withString:string];//变化后的字符串
    
    [self checkNextBtnIsEnabledWithName:name];
    
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    self.btnNext.enabled = NO;
    return YES;
}

- (IBAction)actionAgree:(UIButton *)btn {
    self.btnAgree.selected = !self.btnAgree.selected;
    [self checkNextBtnIsEnabledWithName:self.labelName.text];
}




- (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
