

//
//  addressGatherModel.m
//  Open
//
//  Created by 看看智能 on 2018/1/18.
//  Copyright © 2018年 黄江涛 . All rights reserved.
//

#import "addressGatherModel.h"

@implementation addressGatherModel
-(instancetype)initWithViewController:(UIViewController *)vc
{
    self = [super init];
    if (self) {
         self.vc =vc;
    }
    
    return self;
}
#define mark -这里showpicker
- (HXProvincialCitiesCountiesPickerview *)regionPickerView{
    if (!_regionPickerView) {
        
        _regionPickerView = [[HXProvincialCitiesCountiesPickerview alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        [self.vc.view addSubview:_regionPickerView];
    }
    return _regionPickerView;
}







- (NSMutableArray *)dataArrName{
    if (!_dataArrName) {
        _dataArrName = [NSMutableArray array];
    }
    return _dataArrName;
}
- (NSMutableArray *)dataArrID{
    if (!_dataArrID) {
        _dataArrID = [NSMutableArray new];
    }
    return _dataArrID;
}

//带有返回值的block写法学习
-(NSString * (^)(NSString*))onlySelect_2:(NSArray *)arr Handle:(AddressChooseHandle)completionHandle
{
    if (arr.count == 0) {
        return 0;
    }
    [self.dataArrID removeAllObjects];
    for (NSString *str2   in arr) {
        NSData *data =[NSKeyedArchiver archivedDataWithRootObject:str2];
        [self.dataArrID addObject:data];
    }
    
    NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];
    [userDefaults setObject:arr forKey:@"myCityModel"];
    [userDefaults setObject:self.dataArrID forKey:@"myCityModelID"];
    [userDefaults synchronize];

     [self.regionPickerView showPickerWithProvinceName:@"" cityName:nil countyName:nil index:2];
    //这里右边并不是self.regionPockerView属性的值,它只是一个completion.int一样类型的值而已
   return  self.regionPickerView.completion_int = ^NSString *(NSString *provinceName) {
       completionHandle(provinceName,nil,nil);
       return provinceName;
    };
}




-(void)onlySelect:(NSArray *)arr Handle:(AddressChooseHandle)completionHandle
{
    if (arr.count == 0) {
        return;
    }
      [self.dataArrID removeAllObjects];
    for (NSString *str2   in arr) {
            NSData *data =[NSKeyedArchiver archivedDataWithRootObject:str2];
        [self.dataArrID addObject:data];
    }
    
            NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];
            [userDefaults setObject:arr forKey:@"myCityModel"];
            [userDefaults setObject:self.dataArrID forKey:@"myCityModelID"];
            [userDefaults synchronize];
            [self.regionPickerView showPickerWithProvinceName:@"" cityName:nil countyName:nil index:2];
            _regionPickerView.completion = ^(NSString *provinceName, NSString *numberSign, NSString *countyName) {
                completionHandle(provinceName,numberSign,nil);
            };
  
}



-(void)getProvinceHandle:(AddressChooseHandle)completionHandle{
      __weak typeof(self) wself = self;
    [PersonGatherJobNetManager  getUserprovincecompletionHandle:^(CityModel *model, NSError *error) {
        
        if (error) {
           [SVProgressHUD showErrorWithStatus:@"请检查网络"];
            return;
        }
        if ( model.result.count == 0) {
            return;
        }

        if (model.code == 0 ) {
            [wself.dataArrName removeAllObjects ];
            [wself.dataArrID removeAllObjects];
        
            
            if ([self.stringChoose isEqualToString:@"stringChoose"]) {
                //保存
                           for (int row = 0 ; row < 31 ; row++) {
                               CityResultModel *city =model.result[row];
                               [wself.dataArrName addObject:city.provice_name];
                               NSData *data =[NSKeyedArchiver archivedDataWithRootObject:city.provice_id];
                               [wself.dataArrID addObject:data];
                               NSLog(@"%@",wself.dataArrName[row]);
                               NSLog(@"huangjiangtao:%@",wself.dataArrID[row]);
                               
                           }
            }else{
                
                if (model.result.count == 31) {
                    for (int row = 0 ; row < 3 ; row++) {
                        NSInteger selectIndex = 0;
                        if (row == 0) {
                            selectIndex = 2;
                        }else if (row == 1){
                            selectIndex = 7;
                        }else if (row == 2){
                            selectIndex = 23;
                        }
                        CityResultModel *city =model.result[selectIndex];
                        [wself.dataArrName addObject:city.provice_name];
                        NSData *data =[NSKeyedArchiver archivedDataWithRootObject:city.provice_id];
                        [wself.dataArrID addObject:data];
                        NSLog(@"%@",wself.dataArrName[row]);
                        NSLog(@"huangjiangtao:%@",wself.dataArrID[row]);
                        
                    }
                }
                
            }
           
            

            NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];
            [userDefaults setObject:self.dataArrName forKey:@"myCityModel"];
            [userDefaults setObject:self.dataArrID forKey:@"myCityModelID"];
            [userDefaults synchronize];

        [self.regionPickerView showPickerWithProvinceName:@"" cityName:nil countyName:nil index:2];
            _regionPickerView.completion = ^(NSString *provinceName, NSString *numberSign, NSString *countyName) {
                completionHandle(provinceName,numberSign,error);
            };

        }else{
            [SVProgressHUD showErrorWithStatus:@"请检查网络"];
            
        }
    }];
}

- (void)getDownTown:(NSString*)str Handle:(AddressChooseHandle)completionHandle{
     __weak typeof(self) wself = self;
    [PersonGatherJobNetManager getUserCitytoCity:str completionHandle:^(CitytoCityModel *model, NSError *error) {
        if (model.code == 0 || model.result.count > 0) {
            [wself.dataArrName removeAllObjects ];
            [wself.dataArrID removeAllObjects];
            
            //保存
            for (int row = 0 ; row < model.result.count  ; row++) {
                CityToCityResultModel *city =model.result[row];
                [wself.dataArrName addObject:city.city_name];
                NSData *data =[NSKeyedArchiver archivedDataWithRootObject:city.city_id];
                [wself.dataArrID addObject:data];
                NSLog(@"%@",wself.dataArrName[row]);
                NSLog(@"huangjiangtao:%@",wself.dataArrID[row]);
                
            }
            NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];
            [userDefaults setObject:self.dataArrName forKey:@"myCityModel"];
            [userDefaults setObject:self.dataArrID forKey:@"myCityModelID"];
            [userDefaults synchronize];
            
            [self.regionPickerView showPickerWithProvinceName:@"" cityName:nil countyName:nil index:2];
            _regionPickerView.completion = ^(NSString *provinceName, NSString *numberSign, NSString *countyName) {
                completionHandle(provinceName,numberSign,error);
            };
            
        }else{
            [SVProgressHUD showErrorWithStatus:@"请检查网络"];
            
        }
        
        
        

    }];

}
- (void)getArea:(NSString*)str Handle:(AddressChooseHandle)completionHandle{
    __weak typeof(self) wself = self;
    [PersonGatherJobNetManager getUserareaModel:str completionHandle:^(areaModel *model, NSError *error) {
        if (model.code == 0 || model.result.count > 0) {
            [wself.dataArrName removeAllObjects ];
            [wself.dataArrID removeAllObjects];
            
            //保存
            for (int row = 0 ; row < model.result.count  ; row++) {
                CityToAreaResultModel *city =model.result[row];
                [wself.dataArrName addObject:city.county_name];
                NSData *data =[NSKeyedArchiver archivedDataWithRootObject:city.county_id];
                [wself.dataArrID addObject:data];
                NSLog(@"%@",wself.dataArrName[row]);
                NSLog(@"huangjiangtao:%@",wself.dataArrID[row]);
                
            }
            NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];
            [userDefaults setObject:self.dataArrName forKey:@"myCityModel"];
            [userDefaults setObject:self.dataArrID forKey:@"myCityModelID"];
            [userDefaults synchronize];
            
            [self.regionPickerView showPickerWithProvinceName:@"" cityName:nil countyName:nil index:2];
            _regionPickerView.completion = ^(NSString *provinceName, NSString *numberSign, NSString *countyName) {
                completionHandle(provinceName,numberSign,error);
            };
            
        }else{
            [SVProgressHUD showErrorWithStatus:@"请检查网络"];
            
        }

    }];

}
- (void)getVillage:(NSString*)str Handle:(AddressChooseHandle)completionHandle{
    __weak typeof(self) wself = self;
    [PersonGatherJobNetManager getUsercountry:str completionHandle:^(Town *model, NSError *error) {
        if (model.code == 0 || model.result.count > 0) {
            [wself.dataArrName removeAllObjects ];
            [wself.dataArrID removeAllObjects];
            
            //保存
            for (int row = 0 ; row < model.result.count  ; row++) {
                TownResultModel *city =model.result[row];
                [wself.dataArrName addObject:city.town_name];
                NSData *data =[NSKeyedArchiver archivedDataWithRootObject:city.town_id];
                [wself.dataArrID addObject:data];
                NSLog(@"%@",wself.dataArrName[row]);
                NSLog(@"huangjiangtao:%@",wself.dataArrID[row]);
                
            }
            NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];
            [userDefaults setObject:self.dataArrName forKey:@"myCityModel"];
            [userDefaults setObject:self.dataArrID forKey:@"myCityModelID"];
            [userDefaults synchronize];
            
            [self.regionPickerView showPickerWithProvinceName:@"" cityName:nil countyName:nil index:2];
            _regionPickerView.completion = ^(NSString *provinceName, NSString *numberSign, NSString *countyName) {
                completionHandle(provinceName,numberSign,error);
            };
            
        }else{
            [SVProgressHUD showErrorWithStatus:@"请检查网络"];
            
        }
    }];

}
- (void)getVillageCommitee:(NSString*)str Handle:(AddressChooseHandle)completionHandle{
    __weak typeof(self) wself = self;
    [PersonGatherJobNetManager getUsertown:str completionHandle:^(VillageModel *model, NSError *error) {
        if (model.code == 0 || model.result.count > 0) {
            [wself.dataArrName removeAllObjects ];
            [wself.dataArrID removeAllObjects];
            
            //保存
            for (int row = 0 ; row < model.result.count  ; row++) {
                VillageResultModel *city =model.result[row];
                [wself.dataArrName addObject:city.village_name];
                NSData *data =[NSKeyedArchiver archivedDataWithRootObject:city.village_id];
                [wself.dataArrID addObject:data];
                NSLog(@"%@",wself.dataArrName[row]);
                NSLog(@"huangjiangtao:%@",wself.dataArrID[row]);
                
            }
            NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];
            [userDefaults setObject:self.dataArrName forKey:@"myCityModel"];
            [userDefaults setObject:self.dataArrID forKey:@"myCityModelID"];
            [userDefaults synchronize];
            
            [self.regionPickerView showPickerWithProvinceName:@"" cityName:nil countyName:nil index:2];
            _regionPickerView.completion = ^(NSString *provinceName, NSString *numberSign, NSString *countyName) {
              
                completionHandle(provinceName,numberSign,error);
            };
            
        }else{
            [SVProgressHUD showErrorWithStatus:@"请检查网络"];
            
        }
    }];
}
- (void)getBigCode:(NSString*)smallCode Handle:(AddressChooseHandle)completionHandle{
    __weak typeof(self) wself = self;
    

    
    
    
    [PersonGatherJobNetManager getBigCode:smallCode completionHandle:^(bigcodeModel *model, NSError *error) {
        
        if (model.state == 0 && model.result.count > 0) {
            [wself.dataArrName removeAllObjects ];
            [wself.dataArrID removeAllObjects];
            
            //保存
            for (int row = 0 ; row < model.result.count  ; row++) {
                bigcodeModelResult *bigModel =model.result[row];
                [wself.dataArrName addObject:bigModel.industryName];
                
                NSData *data =[NSKeyedArchiver archivedDataWithRootObject:[NSString stringWithFormat:@"%ld",(long)bigModel.industrySmallCode]];
                [wself.dataArrID addObject:data];
                //检查一下两个数组有没有值
                NSLog(@"%@",wself.dataArrName[row]);
                NSLog(@"huangjiangtao:%@",wself.dataArrID[row]);
                
            }
            NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];
            [userDefaults setObject:self.dataArrName forKey:@"myCityModel"];
            [userDefaults setObject:self.dataArrID forKey:@"myCityModelID"];
            [userDefaults synchronize];
            
            [self.regionPickerView showPickerWithProvinceName:@"" cityName:nil countyName:nil index:2];
            _regionPickerView.completion = ^(NSString *provinceName, NSString *numberSign, NSString *countyName) {
                completionHandle(provinceName,numberSign,error);
            };
            
        }else{
            [SVProgressHUD showErrorWithStatus:@"请检查网络"];
            
        }
        
    }];
    
    
    
    
  
    
}


@end

