//
//  KWMCustomsClearanceVC.m
//  iCemarose
//
//  Created by HouWeiBin on 2017/6/28.
//  Copyright © 2017年 kollway. All rights reserved.
//

#import "KWMCustomsClearanceVC.h"
#import "KWMStringUtil.h"

@interface KWMCustomsClearanceVC ()

@property (weak, nonatomic) IBOutlet UITextField *tfName;

@property (weak, nonatomic) IBOutlet UITextField *tfIdCode;

@end

@implementation KWMCustomsClearanceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

-(void)initView{
    [self initTitle];
    [self initTf];
}

-(void)initTitle{
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(onClickLeftButton)];
    self.navigationItem.leftBarButtonItem = leftButton;
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"存储" style:UIBarButtonItemStylePlain target:self action:@selector(onClickRightButton)];
    self.navigationItem.rightBarButtonItem = rightButton;
}

-(void)initTf{
    self.tfName.borderStyle = UITextBorderStyleNone;
    self.tfIdCode.borderStyle = UITextBorderStyleNone;
    self.tfIdCode.delegate = self;
    
    self.customsClearance = [KWMCustomsClearance new];
    self.customsClearance.name = @"侯卫彬";
    self.customsClearance.idCode = @"445222199211153557";
    if(self.customsClearance){
        self.tfName.text = self.customsClearance.name;
        self.tfIdCode.text = self.customsClearance.idCode;
        if(self.customsClearance.idCode.length >10){
            self.tfIdCode.text = [self halfSecretCode:self.customsClearance.idCode];
        }
    }else{
        self.customsClearance = [KWMCustomsClearance new];
    }
}

-(void)onClickLeftButton{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)onClickRightButton{
    NSString *name = self.tfName.text;
    NSString *idCode = self.tfIdCode.text;
    if([KWMStringUtil isEmpty:name]){
        [self showToast:@"请填写姓名"];
    }else if([KWMStringUtil isEmpty:idCode]){
        [self showToast:@"请填写身份证号"];
    }else{
        //身份证是否为没有编辑过--如果是且仍为半密文状态.
        BOOL noEdited = [self.tfIdCode.text rangeOfString:@"***"].location != NSNotFound;
        self.customsClearance.name = self.tfName.text;
        self.customsClearance.idCode = noEdited?self.customsClearance.idCode:self.tfIdCode.text;
        
        if(self.delegate){
            [self.delegate kwm_customsClearance:self.customsClearance];
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark -- idCode StringUtil
//替换字符串 445555199011123333 => 44555************3
-(NSString *)halfSecretCode:(NSString *)idCode{
    NSString *mIdCode = self.customsClearance.idCode;
    NSMutableString *secretCode = [[NSMutableString alloc] init];
    for (NSInteger i = 0; i < idCode.length - 6; i++) {
        [secretCode appendString:@"*"];
    }
    NSString *halfSecretCode = [mIdCode stringByReplacingCharactersInRange:NSMakeRange(5, mIdCode.length-6) withString:secretCode];
    return halfSecretCode;
}

#pragma mark -- UITextFiledDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    textField.clearsOnBeginEditing = NO;
}

//简单的输入限制 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHANUM] invertedSet];
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    return [string isEqualToString:filtered];
}


@end