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

#import "ChangeMyBankCardViewController.h"
#import "NSString+Category.h"
#import "MyBankClass.h"
#import "JavenBankCards.h"
@interface ChangeMyBankCardViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *labelCardNO;
@property (weak, nonatomic) IBOutlet UITextField *labelName;
@property (weak, nonatomic) IBOutlet UITextField *labelCardName;
@property (weak, nonatomic) IBOutlet UIButton *btnBottom;

@end

@implementation ChangeMyBankCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self setUpDefaults];
    

    // Do any additional setup after loading the view from its nib.
}


- (void)setUpDefaults {
    JavenBankCards *bankCard;
    if (self.reseller.certificate.bankCards.count > 0) {
        bankCard = self.reseller.certificate.bankCards[0];
    }else{
        bankCard = [[JavenBankCards alloc] init];
    }
    self.labelCardName.text = bankCard.bank;
    self.labelCardNO.text = bankCard.cardNo;
    self.labelName.text = bankCard.carrier;
    
    self.title = @"修改银行卡";
    self.labelCardNO.delegate = self;
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //check cardNo validate
    NSString *strBankCardID = [textField.text stringByReplacingCharactersInRange:range withString:string];//变化后的字符串
    if ([strBankCardID isBankCard]) {
        NSString *strBankNameAndType = [MyBankClass returnBankName:strBankCardID];
        self.labelCardName.text = [strBankNameAndType componentsSeparatedByString:@"·"][0];
    }else{
        self.labelCardName.text = @"";
    }
    
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    self.labelCardName.text = @"";
    return YES;
}

- (IBAction)actionChange:(id)sender {
    NSString *strCardNO = self.labelCardNO.text;
    NSString *strCardName = self.labelCardName.text;
    NSString *strName = self.labelName.text;
    
    if (strCardNO.length > 0 && strCardName.length > 0 && strName.length > 0) {
        [self changeResellerInfoWithDic:[self getParams]];
        
    }else{
        
    }
    
}


/** 发送网络请求的参数 */
- (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.labelCardNO.text;
    bankCard.bank = self.labelCardName.text;
    bankCard.carrier = self.labelName.text;
    
    NSDictionary *dic = [bankCard dictionaryRepresentation];
    
    self.reseller.certificate.bankCards = @[dic];
    NSDictionary *params = [self getParamsWithReseller:self.reseller];
    return params;
}

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

/** 把修改的信息上传到服务器 */
- (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 (kIsResponseSuccess(responseObject)) {
            
            [MBProgressHUD Javen_showSuccess:@"修改成功！" onView:weakSelf.view  delay:0.7 complete:^{
                [weakSelf popAction];
            }];
        }else{
            [MBProgressHUD Javen_showError:responseObject[@"message"] onView:weakSelf.view delay:0.7 complete:nil];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [MBProgressHUD hideHUDForView:weakSelf.view];

    }];
}


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