//
//  WJHandelAddressAlertView.m
//  KKCivilAd
//
//  Created by 雷俊博 on 2020/4/13.
//  Copyright © 2020 雷俊博. All rights reserved.
//

#import "WJHandelAddressAlertView.h"
#import "CYAddressSelectedView.h"
#import "LoginVC.h"

@interface WJHandelAddressAlertView ()<UITextFieldDelegate,UIGestureRecognizerDelegate>
{
    NSString* selectedProvinceId;
    NSString* selectedCityId;
    NSString* selectedcountieId;
}

/** 弹窗主内容view */
@property (nonatomic,strong) UIView   *contentView;

@property (nonatomic, strong)UITextField*tf_province;

@property (nonatomic, strong)UITextField* tf_city;

@property (nonatomic, strong)UITextField* tf_county;

@property (nonatomic, strong)NSArray *array_province;

@property (nonatomic, strong)NSArray *array_city;

@property (nonatomic, strong)NSArray *array_counties;

@property (nonatomic, strong)NSDictionary*contentDic;

@property (nonatomic, copy)NSString*name;

@property (nonatomic, copy)NSString*idNumber;
@property (nonatomic, strong)UIViewController* vc;

@property (nonatomic, copy)void (^Callback)(NSString* url,NSDictionary*params) ;

@end

@implementation WJHandelAddressAlertView
#pragma mark - life cycle
- (instancetype)initWithName:(NSString *)name idStr:(NSString *)idStr contentDic:(NSDictionary *)content Controller:(UIViewController *)viewController Callback:(void (^)(NSString* url,NSDictionary*params)  )Callback
{
    self = [super init];
    if (self) {
        self.vc = viewController;
        self.contentDic = content;
        self.name = idStr;
        self.idNumber = idStr;
        self.Callback = Callback;
        [self setupUI];
        
        [self getProvince];
    }
    return self;
}

#pragma mark - UI搭建
/** UI搭建 */
- (void)setupUI
{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    
    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
    
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc] init];
    self.contentView.bounds = CGRectMake(0, 0, 295*WIDTHRADIU, 300 * WIDTHRADIU);
    self.contentView.center = self.center;
    self.contentView.layer.cornerRadius = 15 * WIDTHRADIU;
    self.contentView.layer.masksToBounds = YES;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    //顶部背景图片
    UIImageView* topImg = [[UIImageView alloc]init];
    topImg.image = [UIImage imageNamed:@"address_bg"];
    
    [self.contentView addSubview:topImg];
    
    //标题
    UILabel*lb_title = [[UILabel alloc]init];
    lb_title.text = @"请确认您申报地区";
    lb_title.font = [UIFont boldSystemFontOfSize:17 * WIDTHRADIU];
    lb_title.textColor = [UIColor whiteColor];
    [topImg addSubview:lb_title];
    
    
    
    //省
    UILabel* lb_province = [[UILabel alloc]init];
    lb_province.text = @"省:";
    lb_province.textColor = [UIColor colorWithHex:0x808080];
    lb_province.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
    
    [self.contentView addSubview:lb_province];
    
    UITextField* tf_province = [[UITextField alloc]init];
    tf_province.placeholder = @"请选择您申报省";
    [self.contentView addSubview:tf_province];
    tf_province.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    tf_province.textColor = [UIColor colorWithHex:0x4d4d4d];
    
    self.tf_province = tf_province;
    
    if (@available(iOS 13.0,*)) {
        NSMutableAttributedString *placeholderFont = [[NSMutableAttributedString alloc] initWithString:@"请选择您申报省" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15.0 * WIDTHRADIU],NSForegroundColorAttributeName : [UIColor colorWithHex:0xcccccc]}];
                                         
        tf_province.attributedPlaceholder = placeholderFont;
      }else{
        tf_province.placeholder = @"请选择您申报省";
        [tf_province setValue:[UIFont systemFontOfSize:15.0 * WIDTHRADIU] forKeyPath:@"_placeholderLabel.font"];
        [tf_province setValue:[UIColor colorWithHex:0xcccccc] forKeyPath:@"_placeholderLabel.textColor"];
      }
    
    
    UIImageView* arrowImg_province = [[UIImageView alloc]init];
    arrowImg_province.image = [UIImage imageNamed:@"pullDownImage"];
    [self.contentView addSubview:arrowImg_province];
    
    UIView*provinceLine = [[UIView alloc]init];
    provinceLine.backgroundColor = [UIColor colorWithHex:0xcccccc];
    [self.contentView addSubview:provinceLine];
    
    //市
    UILabel* lb_city = [[UILabel alloc]init];
    lb_city.text = @"市:";
    lb_city.textColor = [UIColor colorWithHex:0x808080];
    lb_city.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
    
    [self.contentView addSubview:lb_city];
    
    UITextField* tf_city = [[UITextField alloc]init];
    tf_city.placeholder = @"请选择您申报市区";
    [self.contentView addSubview:tf_city];
    tf_city.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    tf_city.textColor = [UIColor colorWithHex:0x4d4d4d];
    
    self.tf_city = tf_city;
    
    if (@available(iOS 13.0,*)) {
        NSMutableAttributedString *placeholderFont = [[NSMutableAttributedString alloc] initWithString:@"请选择您申报市区" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15.0 * WIDTHRADIU],NSForegroundColorAttributeName : [UIColor colorWithHex:0xcccccc]}];
                                         
        tf_city.attributedPlaceholder = placeholderFont;
      }else{
        tf_city.placeholder = @"请选择您申报市区";
        [tf_city setValue:[UIFont systemFontOfSize:15.0 * WIDTHRADIU] forKeyPath:@"_placeholderLabel.font"];
        [tf_city setValue:[UIColor colorWithHex:0xcccccc] forKeyPath:@"_placeholderLabel.textColor"];
      }
    
    
    UIImageView* arrowImg_city = [[UIImageView alloc]init];
    arrowImg_city.image = [UIImage imageNamed:@"pullDownImage"];
    [self.contentView addSubview:arrowImg_city];
    
    UIView*cityLine = [[UIView alloc]init];
    cityLine.backgroundColor = [UIColor colorWithHex:0xcccccc];
    [self.contentView addSubview:cityLine];
    
    
    //区县
    UILabel* lb_county = [[UILabel alloc]init];
    lb_county.text = @"县:";
    lb_county.textColor = [UIColor colorWithHex:0x808080];
    lb_county.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
       
    [self.contentView addSubview:lb_county];
       
    UITextField* tf_county = [[UITextField alloc]init];
    tf_county.placeholder = @"请选择您申报县";
    [self.contentView addSubview:tf_county];
    tf_county.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    tf_county.textColor = [UIColor colorWithHex:0x4d4d4d];
    
    self.tf_county = tf_county;
       
    if (@available(iOS 13.0,*)) {
           NSMutableAttributedString *placeholderFont = [[NSMutableAttributedString alloc] initWithString:@"请选择您申报县" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15.0 * WIDTHRADIU],NSForegroundColorAttributeName : [UIColor colorWithHex:0xcccccc]}];
                                            
           tf_county.attributedPlaceholder = placeholderFont;
        }else{
           tf_county.placeholder = @"请选择您申报县";
           [tf_county setValue:[UIFont systemFontOfSize:15.0 * WIDTHRADIU] forKeyPath:@"_placeholderLabel.font"];
           [tf_county setValue:[UIColor colorWithHex:0xcccccc] forKeyPath:@"_placeholderLabel.textColor"];
        }
       
       
    UIImageView* arrowImg_county = [[UIImageView alloc]init];
    arrowImg_county.image = [UIImage imageNamed:@"pullDownImage"];
    [self.contentView addSubview:arrowImg_county];
       
    UIView*countyLine = [[UIView alloc]init];
    countyLine.backgroundColor = [UIColor colorWithHex:0xcccccc];
    [self.contentView addSubview:countyLine];
    
    
    
    
    //message
    UILabel *lb_message = [[UILabel alloc]init];
    lb_message.text = @"该地址为您的申报地址，若不对请重新选择";
    lb_message.textAlignment = NSTextAlignmentCenter;
    lb_message.textColor = [UIColor colorWithHex:0x999999];
    lb_message.font = [UIFont systemFontOfSize:12 * WIDTHRADIU];
    
    [self.contentView addSubview:lb_message];
    
    //uibutton
    
    UIButton* commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [commitBtn setTitle:@"确认提交" forState:UIControlStateNormal];
    [commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    commitBtn.layer.cornerRadius = 8 * WIDTHRADIU;
    [commitBtn setBackgroundColor:K_MainColor];
    commitBtn.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    [commitBtn addTarget:self action:@selector(commitBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:commitBtn];
    
    
    
    self.tf_province.delegate = self;
    self.tf_city.delegate = self;
    self.tf_county.delegate = self;
    
    if (self.contentDic) {
        self.tf_province.text = self.contentDic[@"provincename"];
        self.tf_city.text = self.contentDic[@"cityname"];
        self.tf_county.text = self.contentDic[@"countryname"];
        selectedProvinceId = [NSString stringWithFormat:@"%@",self.contentDic[@"provinceid"]];
        selectedCityId = [NSString stringWithFormat:@"%@",self.contentDic[@"cityid"]];
        selectedcountieId = [NSString stringWithFormat:@"%@",self.contentDic[@"countryid"]];
        
        [self getCity:selectedProvinceId];
        [self getCounty:selectedCityId];
    }
    
    
    [topImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.offset(60 * WIDTHRADIU);
    }];
    
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(topImg);
    }];
    
    
    [lb_province setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [lb_province setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [lb_province mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topImg.mas_bottom).offset(27 * WIDTHRADIU);
        make.left.offset(27 * WIDTHRADIU);
    }];
    
    [tf_province mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lb_province);
        make.left.equalTo(lb_province.mas_right).offset(10 * WIDTHRADIU);
        make.height.offset(30 * WIDTHRADIU);
        make.right.offset(- 64 * WIDTHRADIU);
    }];
    
    [arrowImg_province mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(tf_province.mas_right).offset(5 * WIDTHRADIU);
        make.centerY.equalTo(tf_province);
    }];
    
    [provinceLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lb_province.mas_right).offset(3 * WIDTHRADIU);
        make.bottom.equalTo(tf_province.mas_bottom).offset(- 3 * WIDTHRADIU);
        make.right.offset(- 39 * WIDTHRADIU);
        make.height.offset(0.5);
    }];
    
    
    [lb_city setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [lb_city setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [lb_city mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tf_province.mas_bottom).offset(15 * WIDTHRADIU);
        make.left.offset(27 * WIDTHRADIU);
    }];
    
    [tf_city mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lb_city);
        make.left.equalTo(lb_city.mas_right).offset(10 * WIDTHRADIU);
        make.height.offset(30 * WIDTHRADIU);
        make.right.offset(- 64 * WIDTHRADIU);
    }];
    
    [arrowImg_city mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(tf_city.mas_right).offset(5 * WIDTHRADIU);
        make.centerY.equalTo(tf_city);
    }];
    
    [cityLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lb_city.mas_right).offset(3 * WIDTHRADIU);
        make.bottom.equalTo(tf_city.mas_bottom).offset(- 3 * WIDTHRADIU);
        make.right.offset(- 39 * WIDTHRADIU);
        make.height.offset(0.5);
    }];
    
    
    [lb_county setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [lb_county setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [lb_county mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tf_city.mas_bottom).offset(15 * WIDTHRADIU);
        make.left.offset(27 * WIDTHRADIU);
    }];
    
    [tf_county mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lb_county);
        make.left.equalTo(lb_county.mas_right).offset(10 * WIDTHRADIU);
        make.height.offset(30 * WIDTHRADIU);
        make.right.offset(- 64 * WIDTHRADIU);
    }];
    
    [arrowImg_county mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(tf_county.mas_right).offset(5 * WIDTHRADIU);
        make.centerY.equalTo(tf_county);
    }];
    
    [countyLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lb_county.mas_right).offset(3 * WIDTHRADIU);
        make.bottom.equalTo(tf_county.mas_bottom).offset(- 3 * WIDTHRADIU);
        make.right.offset(- 39 * WIDTHRADIU);
        make.height.offset(0.5);
    }];
    
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(270 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
        make.bottom.offset(-15 * WIDTHRADIU);
        make.height.offset(40 * WIDTHRADIU);
    }];
    
    [lb_message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(commitBtn);
        make.bottom.equalTo(commitBtn.mas_top).offset(-8 * WIDTHRADIU);
    }];
    
    
    
}



#pragma mark - 请求地址接口
- (void)getProvince
{
    WeakSelf
    NSString *urlString = [NSString stringWithFormat:@"%@%@",[NetworkConfig ipAddressConfig],getProvinceListRequest];
    
    NSLog(@"获取省份url:%@",urlString);
    [NetworkRequest Get7:urlString Params:nil Success:^(id _Nonnull obj) {
        NSLog(@"obj:%@",obj);
        
        if ([obj isKindOfClass:[NSDictionary class]]) {
            NSString *state = [NSString stringWithFormat:@"%@",obj[@"state"]];
            if ([state isEqualToString:@"200"]) {
                NSArray *provinces = obj[@"result"];
                if ([provinces isKindOfClass:[NSArray class]]) {
                    weakSelf.array_province = provinces;
                }else{
                    weakSelf.array_province = nil;
                }
            }else{
                [Tools showMessage:@"省份请求失败"];
            }
        }else {
            [Tools showMessage:@"服务器异常"];
        }
        
        
    } Failure:^(NSError* error) {
        NSLog(@"error:%@",error);
        [Tools showMessage:@"无网络"];
    }];
}

- (void)getCity:(NSString *)province_id
{
    WeakSelf
      NSString *urlString = [NSString stringWithFormat:@"%@%@",[NetworkConfig ipAddressConfig],getCityListRequest];
    NSDictionary *params = @{
        @"provice_id":province_id
    };
      NSLog(@"获取城市url:%@",urlString);
    NSLog(@"params:%@",params);
      [NetworkRequest Get7:urlString Params:params Success:^(id _Nonnull obj) {
          NSLog(@"obj:%@",obj);
          
          if ([obj isKindOfClass:[NSDictionary class]]) {
              NSString *state = [NSString stringWithFormat:@"%@",obj[@"state"]];
              if ([state isEqualToString:@"200"]) {
                  NSArray *citys = obj[@"result"];
                  if ([citys isKindOfClass:[NSArray class]]) {
                      weakSelf.array_city = citys;
                  }else{
                      weakSelf.array_city = nil;
                  }
              }else{
                  [Tools showMessage:@"城市请求失败"];
              }
          }else {
              [Tools showMessage:@"服务器异常"];
          }
          
          
      } Failure:^(NSError* error) {
          NSLog(@"error:%@",error);
          [Tools showMessage:@"无网络"];
      }];
}

- (void)getCounty:(NSString *)city_id
{
    WeakSelf
         NSString *urlString = [NSString stringWithFormat:@"%@%@",[NetworkConfig ipAddressConfig],getCountyListRequest];
       NSDictionary *params = @{
           @"city_id":city_id
       };
         NSLog(@"获取区县url:%@",urlString);
       NSLog(@"params:%@",params);
         [NetworkRequest Get7:urlString Params:params Success:^(id _Nonnull obj) {
             NSLog(@"obj:%@",obj);
             
             if ([obj isKindOfClass:[NSDictionary class]]) {
                 NSString *state = [NSString stringWithFormat:@"%@",obj[@"state"]];
                 if ([state isEqualToString:@"200"]) {
                     NSArray *countys = obj[@"result"];
                     if ([countys isKindOfClass:[NSArray class]]) {
                         weakSelf.array_counties = countys;
                     }else{
                         weakSelf.array_counties = nil;
                     }
                 }else{
                     [Tools showMessage:@"区县请求失败"];
                 }
             }else {
                 [Tools showMessage:@"服务器异常"];
             }
             
             
         } Failure:^(NSError* error) {
             NSLog(@"error:%@",error);
             [Tools showMessage:@"无网络"];
         }];
}


#pragma mark - 提交信息

- (NSDictionary *)gaolingdengjiRequestPara
{
    
    NSDictionary* params = @{
        @"provinceName":self.tf_province.text,
        @"cityName":self.tf_city.text,
        @"countryName":self.tf_county.text,
        @"identityNumber":[EnercryTool AES256Encrypt:self.idNumber key:MZAESKEY],
        @"verifyType":@(10),
        @"workType":[self getWorkType]
    };
    return params;
}

- (void)gaolingdengjiRequest
{
    WeakSelf
    NSString *urlString = [NSString stringWithFormat:@"%@%@",[NetworkConfig ipYouFuVerityConfig],GaolingDengjiRequest];
     NSLog(@"高龄登记查询url:%@",urlString);
      NSLog(@"params:%@",[self gaolingdengjiRequestPara]);
      NSString *token = [UserDefault objectForKey:@"token"];
      [SVProgressHUD show];
    [NetworkRequest Get6:urlString Params:[self gaolingdengjiRequestPara] Token:token Success:^(id  _Nonnull responseObject) {
        [SVProgressHUD dismiss];
        NSLog(@"responseObject:%@",responseObject);
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSString*state = [NSString stringWithFormat:@"%@",responseObject[@"state"]];
            NSString* describe = [NSString stringWithFormat:@"%@",responseObject[@"describe"]];
            if ([state isEqualToString:@"200"]) {

                NSString* url = [NSString stringWithFormat:@"%@",responseObject[@"result"]];
                if (![Tools isBlankString:url]) {
                    [weakSelf dismiss];
                    NSDictionary*params =  @{
                        @"province":[NSString stringWithFormat:@"%@,%@",self->selectedProvinceId,self.tf_province.text],
                        @"city":[NSString stringWithFormat:@"%@,%@",self->selectedCityId,self.tf_city.text],
                        @"county":[NSString stringWithFormat:@"%@,%@",self->selectedcountieId,self.tf_county.text]
                    };
                    
                    weakSelf.Callback(url,params);
                }
                
                

            }else if ([state isEqualToString:@"700"]){
                [SVProgressHUD showInfoWithStatus:describe];
                [[NSNotificationCenter defaultCenter]postNotificationName:KKSBJumpLoginVCNotificationName object:nil];
                return;
            }else{
                [SVProgressHUD showErrorWithStatus:describe];
            }
        }else{
            [SVProgressHUD showErrorWithStatus:@"服务器异常"];
        }
    } Failure:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
        NSLog(@"error:%@",error);
        [SVProgressHUD showErrorWithStatus:@"网络异常"];
    }];
    
      
}




#pragma mark - UIGestureRecognizerDelegate

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{

    

    if ([touch.view isDescendantOfView:self.contentView]) {

        return NO;

    }

    

    return YES;

    
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    WeakSelf
    if (textField == self.tf_province) {
        if (!self.array_province) {
            [self getProvince];
        }else{
            
            [CYAddressSelectedView showAddressPickerWithTitle:@"请选择省份" data:self.array_province  addressType:AddressTypeProvince resultBlock:^(NSDictionary * _Nonnull selectValue) {
                NSLog(@"采集省份:%@",selectValue);
                weakSelf.tf_province.text = selectValue[@"provice_name"];
                weakSelf.tf_city.text = @"";
                weakSelf.tf_county.text = @"";
                 
                NSString *provice_id = [NSString stringWithFormat:@"%@",selectValue[@"provice_id"]];
                self->selectedProvinceId = provice_id;
                [weakSelf getCity:provice_id];
            }];
        }
        
        
    }else if (textField == self.tf_city){
        if (self.tf_province.text.length == 0) {
            [Tools showMessage:@"请先选择省份"];
            return NO;
        }
        
        if (!self.array_city) {
            [self getCity:selectedProvinceId];
            
        }else{
            [CYAddressSelectedView showAddressPickerWithTitle:@"请选择城市" data:self.array_city addressType:AddressTypeCity resultBlock:^(NSDictionary * _Nonnull selectValue) {
                NSLog(@"采集城市:%@",selectValue);
                weakSelf.tf_city.text = selectValue[@"city_name"];
                weakSelf.tf_county.text = @"";
                
                NSString *city_id = [NSString stringWithFormat:@"%@",selectValue[@"city_id"]];
                self->selectedCityId = city_id;
                
                [weakSelf getCounty:city_id];
            }];
        }
        
        
        return NO;
    }else if (textField == self.tf_county){
        
         if (self.tf_province.text.length == 0) {
            [Tools showMessage:@"请先选择省份"];
            return NO;
        }else if (self.tf_city.text.length == 0){
            [Tools showMessage:@"请先选择城市"];
            return NO;
        }
        
        if (!self.array_counties) {
            [self getCounty:selectedCityId];
        }else{
            
            [CYAddressSelectedView showAddressPickerWithTitle:@"请选择区县" data:self.array_counties addressType:AddressTypeCounties resultBlock:^(NSDictionary * _Nonnull selectValue) {
                       NSLog(@"采集区县:%@",selectValue);
                       weakSelf.tf_county.text = selectValue[@"county_name"];
                       
                       NSString *county_id = [NSString stringWithFormat:@"%@",selectValue[@"county_id"]];
                       self->selectedcountieId = county_id;
                           
                }];
        }
        
    }
    
    return NO;
}


#pragma mark - 判断是属于60y岁到80岁,还是80岁
- (NSNumber *)getWorkType
{
    NSString* age = [self calculateAgeStr:self.idNumber];
    if (age.integerValue >= 60 && age.integerValue < 80) {
        
        return @1;
        
    }else if (age.integerValue >= 80){
        return @0;
    }
    
    return nil;
}



+ (void)showWithViewController:(UIViewController *)viewController name:(NSString *)name idStr:(NSString *)idStr contentDic:(NSDictionary *)contentDic Callback:(void (^)(NSString * _Nonnull, NSDictionary * _Nonnull))Callback
{
    UIWindow* keyWindow = viewController.view.window;
    
    if (keyWindow == nil) {
        keyWindow = [UIApplication sharedApplication].keyWindow;
    }
    
    WJHandelAddressAlertView *alertView = [[WJHandelAddressAlertView alloc]initWithName:name idStr:idStr contentDic:contentDic Controller:viewController Callback:Callback];
    
    [alertView show:keyWindow];
}

#pragma mark - event response
- (void)commitBtnClick:(UIButton *)sender
{
    if ([Tools isBlankString:selectedProvinceId]) {
        [Tools showMessage:@"请选择申报省"];
        return;
    }
    
    if ([Tools isBlankString:selectedCityId]) {
        [Tools showMessage:@"请选择申报市"];
        return;
    }
    if ([Tools isBlankString:selectedcountieId]) {
        [Tools showMessage:@"请选择申报县"];
        return;
    }
    
    [self gaolingdengjiRequest];
    
    
}
- (void)tapClick
{
    [self dismiss];
}


#pragma mark - 显示或隐藏
/** 弹出此弹窗 */
- (void)show:(UIWindow *)window {
    // 出场动画
    self.alpha = 0;
    self.contentView.transform = CGAffineTransformScale(self.contentView.transform, 1.3, 1.3);
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 1;
        self.contentView.transform = CGAffineTransformIdentity;
    }];
    
    [window addSubview:self];
}

/** 移除此弹窗 */
- (void)dismiss {
    [self removeFromSuperview];
}

#pragma mark - private methods
//截取身份证的出生日期并转换为日期格式
//18位
-(NSString *)subsIDStrToDate:(NSString *)str{
    NSMutableString *result = [NSMutableString stringWithCapacity:0];
    
    NSString *dateStr = [str substringWithRange:NSMakeRange(6, 8)];
    NSString  *year = [dateStr substringWithRange:NSMakeRange(0, 4)];
    NSString  *month = [dateStr substringWithRange:NSMakeRange(4, 2)];
    NSString  *day = [dateStr substringWithRange:NSMakeRange(6,2)];
    
    [result appendString:year];
    [result appendString:@"-"];
    [result appendString:month];
    [result appendString:@"-"];
    [result appendString:day];
    
    return result;
}
//14位

-(NSString *)subs2IDStrToDate:(NSString *)str{
    NSMutableString *result = [NSMutableString stringWithCapacity:0];
    
    NSString *dateStr = [str substringWithRange:NSMakeRange(6, 6)];
    NSString  *year = [dateStr substringWithRange:NSMakeRange(0, 2)];
    year = [NSString stringWithFormat:@"19%@",year];
    NSString  *month = [dateStr substringWithRange:NSMakeRange(2, 2)];
    NSString  *day = [dateStr substringWithRange:NSMakeRange(4,2)];
    
    [result appendString:year];
    [result appendString:@"-"];
    [result appendString:month];
    [result appendString:@"-"];
    [result appendString:day];
    
    return result;
}


//计算年龄
-(NSString *)calculateAgeStr:(NSString *)str{
  //截取身份证的出生日期并转换为日期格式
    NSString *dateStr = nil;
    
    if (str.length == 18) {
       dateStr = [self subsIDStrToDate:str];
    }else if (str.length == 15){
        dateStr = [self subs2IDStrToDate:str];
    }
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-mm-dd";
    NSDate *birthDate =  [formatter dateFromString:dateStr];
    NSTimeInterval dateDiff = [birthDate timeIntervalSinceNow];
       
       // 计算年龄
    int age  =  trunc(dateDiff/(60*60*24))/365;
    NSString *ageStr = [NSString stringWithFormat:@"%d", -age];
    
//    NSDate* currentDate = [NSDate date];
//
//     NSArray*birthArray = [dateStr componentsSeparatedByString:@"-"];
//     NSString*birthYear = birthArray[0];
//     NSString*birthMonth = birthArray[1];
//
//     NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
//     [dateformatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];
//     [dateformatter setDateFormat:@"yyyy-MM"];
//
//     //    今年
//
//     NSString *thisYearString=[dateformatter stringFromDate:currentDate];
//
//     NSArray*currentArray = [thisYearString componentsSeparatedByString:@"-"];
//     NSString*currentYear = currentArray[0];
//     NSString*currentMonth = currentArray[1];
//
//     int age = currentYear.intValue -birthYear.intValue;
//
//     if (currentMonth.intValue < birthMonth.intValue) {
//         age = age - 1;
//     }
//
//
//
//
//
//     NSString *ageStr = [NSString stringWithFormat:@"%d", age];

     return ageStr;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
