//
//  AreaPickerView.m
//  YanFarmwork
//
//  Created by HG on 2019/9/25.
//  Copyright © 2019 Yanhuaqiang. All rights reserved.
//

#import "AreaPickerView.h"

@interface AreaPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property(nonatomic,strong) UIPickerView *addresspPickerView;

@property (retain, nonatomic) NSMutableArray *proviceArray, *cityArray, *areaArray;

@property (nonatomic) int proviceIndex, cityIndex, areaIndex;

@end

@implementation AreaPickerView

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

- (void)createPickerView {
    
    _proviceArray = [[NSMutableArray alloc] init];
    _cityArray = [[NSMutableArray alloc] init];
    _areaArray = [[NSMutableArray alloc] init];
    
    _showTag = 1;
    [self requestProvice];
    
    
    typeof(self) wSelf = self;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        wSelf.addresspPickerView = [[UIPickerView alloc] initWithFrame:wSelf.showPickerView.bounds];
        wSelf.addresspPickerView.delegate = self;
        wSelf.addresspPickerView.dataSource = self;
        //        _pickerView.showsSelectionIndicator = YES;
        [wSelf.showPickerView addSubview:wSelf.addresspPickerView];
    });
}
#pragma mark - dataSouce
//有几行
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    if (_addressType == address_type) {
        return 3;
    }else if (_addressType == bank_type) {
        return 2;
    }
    return 0;
}
//行中有几列
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return _proviceArray.count;
    }
    if (component == 1) {
        return _cityArray.count;
    }
    return _areaArray.count;
}
//列显示的数据
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger) row forComponent:(NSInteger)component {
    if (component == 0) {
        return [[_proviceArray objectAtIndex:row] objectForKey:@"name"];
    }
    if (component == 1) {
        return [[_cityArray objectAtIndex:row] objectForKey:@"name"];
    }
    return [[_areaArray objectAtIndex:row] objectForKey:@"name"];
}
// 选中某一组的某一行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        _proviceIndex = (int)row;
        
        _cityArray = [[NSMutableArray alloc] init];
        _cityIndex = 0;
        _areaArray = [[NSMutableArray alloc] init];
        _areaIndex = 0;
        
        [_addresspPickerView reloadComponent:1];
        if (_addressType == address_type) {
            [_addresspPickerView reloadComponent:2];
        }else if (_addressType == bank_type) {
            
        }
        
        
        [self requestCity];
        
    }else if (component == 1) {
        _cityIndex = (int)row;
        
        _areaArray = [[NSMutableArray alloc] init];
        _areaIndex = 0;
        
        if (_addressType == address_type) {
            [_addresspPickerView reloadComponent:2];
            
            [self requestArea];
        }else if (_addressType == bank_type) {
            
        }
        
    }else if (component == 2) {
        _areaIndex = (int)row;
    }
}
- (IBAction)AddressBtnClick:(id)sender {
    UIButton *button = (id)sender;
    if (button.tag == 1) {
        if (_cityArray.count == 0) {
            [XYProgressHUD showMessage:@"请选择城市"];
            return;
        }
        
        if (_addressType == address_type) {
            if (_areaArray.count == 0) {
                [XYProgressHUD showMessage:@"请选择区域"];
                return;
            }
            
            NSLog(@"%@----%@----%@",[_proviceArray objectAtIndex:_proviceIndex],[_cityArray objectAtIndex:_cityIndex], [_areaArray objectAtIndex:_areaIndex]);
            if (_selectProviceAndCityBlock) {
                _selectProviceAndCityBlock([_proviceArray objectAtIndex:_proviceIndex], [_cityArray objectAtIndex:_cityIndex], [_areaArray objectAtIndex:_areaIndex]);
            }
        }else if (_addressType == bank_type) {
            NSLog(@"%@----%@----%@",[_proviceArray objectAtIndex:_proviceIndex],[_cityArray objectAtIndex:_cityIndex], [NSDictionary new]);
            if (_selectProviceAndCityBlock) {
                _selectProviceAndCityBlock([_proviceArray objectAtIndex:_proviceIndex], [_cityArray objectAtIndex:_cityIndex],  [NSDictionary new]);
            }
        }
    }
    
    
    [self removeFromSuperview];
}
- (void)requestProvice {
    NSString *url = @"http://47.101.206.243:8080/show/getProvinceList.do";
//    [KKLoadingAnimationView show];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [NetworkRequest Get3:url Params:params Success:^(id _Nonnull objc) {
//        [KKLoadingAnimationView dismiss];
        NSLog(@"objc:%@",objc);
//        if ([state isEqualToString:@"200"]) {
//            wSelf.proviceArray = [response objectForKey:@"data"];
//
//            [wSelf.addresspPickerView reloadComponent:0];
//
//            if (wSelf.proviceArray.count != 0) {
//                if (wSelf.proviceIndex == 0) {
//                    [self requestCity];
//                }
//            }
//
//        }else{
//            [self alertShowMessage:describe];
//        }
        
        
    } Failure:^(NSError* error) {
        [KKLoadingAnimationView dismiss];
        NSLog(@"error:%@",error);
//        [self alertShowMessage:@"网络异常"];
    }];
}

- (void)requestCity {
    
//    [_cityArray removeAllObjects];
//    [_addresspPickerView reloadAllComponents];
//
//    [XYProgressHUD SVProgressHUDShowStatus:nil WithMask:YES];
//    typeof(self) wSelf = self;
//
//    NSString *UrlString;
//    if (_addressType == address_type) {
//        UrlString = pub_qryOrganizationList;
//    }else{
//        UrlString = pub_qryOrganizationBankList;
//    }
//
//    NSDictionary *parametDic = [[NSDictionary alloc] initWithObjectsAndKeys:
//                                [NSString stringWithFormat:@"%@",[[_proviceArray objectAtIndex:_proviceIndex] objectForKey:@"code"]],@"parentCode",
//                                nil];
//    [JCNetworking getWithUrl:MergeURL(UrlString)
//              refreshRequest:NO
//                       cache:NO
//                      params:parametDic
//             showErrorInView:nil
//               needShowError:YES
//                successBlock:^(id response) {
//        [ToolsObject SVProgressHUDDismiss];
//        if ([[response objectForKey:@"code"] intValue] == 200) {
//
//            wSelf.cityArray = [response objectForKey:@"data"];
//
//            [wSelf.addresspPickerView reloadComponent:1];
//
//            if (_addressType == address_type) {
//                if (wSelf.cityArray.count != 0) {
//                    if (wSelf.cityIndex == 0) {
//                        [self requestArea];
//                    }
//                }
//
//            }
//        }else{
//            //filed
//            [ToolsObject showMessageTitle:[response objectForKey:@"message"] andDelay:1.0f andImage:nil];
//        }
//
//    } failBlock:^(NSError *error) {
//        NSLog(@"test filed ");
//        [ToolsObject SVProgressHUDDismiss];
//    }];
}

- (void)requestArea {
    
//    [ToolsObject SVProgressHUDShowStatus:nil WithMask:YES];
//    typeof(self) wSelf = self;
//
//    NSString *UrlString;
//    if (_addressType == address_type) {
//        UrlString = pub_qryOrganizationList;
//    }else{
//        UrlString = pub_qryOrganizationBankList;
//    }
//
//    NSDictionary *parametDic = [[NSDictionary alloc] initWithObjectsAndKeys:
//                                [NSString stringWithFormat:@"%@",[[_cityArray objectAtIndex:_cityIndex] objectForKey:@"code"]],@"parentCode",
//                                nil];
//    [JCNetworking getWithUrl:MergeURL(UrlString)
//              refreshRequest:NO
//                       cache:NO
//                      params:parametDic
//             showErrorInView:nil
//               needShowError:YES
//                successBlock:^(id response) {
//        [ToolsObject SVProgressHUDDismiss];
//        if ([[response objectForKey:@"code"] intValue] == 200) {
//
//            wSelf.areaArray = [response objectForKey:@"data"];
//
//            [wSelf.addresspPickerView reloadComponent:2];
//
//
//        }else{
//            //filed
//            [ToolsObject showMessageTitle:[response objectForKey:@"message"] andDelay:1.0f andImage:nil];
//        }
//
//    } failBlock:^(NSError *error) {
//        NSLog(@"test filed ");
//        [ToolsObject SVProgressHUDDismiss];
//    }];
}
@end
