//
//  CityTypeSelectView.m
//  Open
//
//  Created by 雷俊博 on 2019/8/2.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

#import "CityTypeSelectView.h"
#import "SaveJobModel.h"


@interface CityTypeSelectView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic, strong) UIPickerView *CityTypePicker;//城市类型选择器;

@property (copy, nonatomic) NSString *title;//标题

@property (strong, nonatomic) BRCityTypeResultBlock resultBlock;

@property (strong, nonatomic) BRPlanTypeResultBlock planBlock;

@property (strong, nonatomic) BRJobTypeResultBlock jobBlock;

/**
 *  城市类型
 */
@property (nonatomic, strong) NSArray* cityTypes;
/**
 *  城市类型2
 */
@property (nonatomic, strong) NSArray* cityTypes2;

@property (nonatomic, strong)NSArray *cardTypes;

@property (nonatomic, strong)NSArray *planTypes;

@property (nonatomic, strong)NSArray *jobTypes;


/**
 *  选中的城市类型
 */
@property (nonatomic, strong) NSString* selectedCityType;
/**
*  选中的方案类型
*/
@property (nonatomic, strong) wxyjModel* selectedPlanType;
/**
*  选中的工作类型
*/
@property (nonatomic, weak) id selectedJobType;


@end
@implementation CityTypeSelectView
#pragma mark - life cycle
- (instancetype)initWithtype:(SelectTypes)type  ResultBlock:(BRCityTypeResultBlock)resultBlock
{
    self = [super init];
    if (self) {
        [self initUI];
        //设置默认选中的省份是provinces中的第一个元素
        self.type = type;
        
        switch (self.type) {
            case SelectTypesHuKou:
                self.selectedCityType = self.cityTypes[0];
                break;
            case SelectTypesNongYe:
                self.selectedCityType = self.cityTypes2[0];
            break;
            case SelectTypesCard:
                self.selectedCityType = self.cardTypes[0];
            break;
            case SelectTypesPlan:
                self.selectedCityType = self.planTypes[0];
            break;
                
            default:
                break;
        }
       
        _resultBlock = resultBlock;
    }
    return self;
}

- (instancetype)initWithtype:(SelectTypes)type dataArray:(NSArray *)dataArray block:(BRPlanTypeResultBlock)block
{
    self = [super init];
    if (self) {
        [self initUI];
        //设置默认选中的省份是provinces中的第一个元素
        self.type = type;
        self.selectedPlanType = dataArray[0];
        self.planTypes = dataArray;
        _planBlock = block;
    }
    return self;
}

- (instancetype)initWithtype2:(SelectTypes)type dataArray:(NSArray *)dataArray block:(BRJobTypeResultBlock)block
{
    self = [super init];
    if (self) {
        [self initUI];
        //设置默认选中的省份是provinces中的第一个元素
        self.type = type;
        self.selectedJobType = dataArray[0];
        self.jobTypes = dataArray;
        _jobBlock = block;
    }
    return self;
}



- (void)initUI
{
    [super initUI];
    
    
    //添加省份选择器
    [self.alertView addSubview:self.CityTypePicker];
}


#pragma mark - public methods
+ (void)showCityTypeViewWithResultBlock:(BRCityTypeResultBlock)resultBlock
{
    CityTypeSelectView *cityPicker = [[CityTypeSelectView alloc]initWithtype:SelectTypesHuKou ResultBlock:resultBlock];
    
    
    [cityPicker showWithAnimation:YES];
}

+ (void)show2CityTypeViewWithResultBlock:(BRCityTypeResultBlock)resultBlock
{
    CityTypeSelectView *cityPicker = [[CityTypeSelectView alloc]initWithtype:SelectTypesNongYe ResultBlock:resultBlock];
    
    
    [cityPicker showWithAnimation:YES];
}
+ (void)show3CityTypeViewWithResultBlock:(BRCityTypeResultBlock)resultBlock
{
    CityTypeSelectView *cityPicker = [[CityTypeSelectView alloc]initWithtype:SelectTypesCard ResultBlock:resultBlock];
       
    
       
    [cityPicker showWithAnimation:YES];
}
+ (void)show4WithArray:(NSArray *)dataArray block:(BRPlanTypeResultBlock)block
{
    CityTypeSelectView *cityPicker = [[CityTypeSelectView alloc]initWithtype:SelectTypesPlan dataArray:dataArray block:block];
       
       
    [cityPicker showWithAnimation:YES];
}

+ (void)show5WithArray:(NSArray *)dataArray type:(SelectTypes)type block:(BRJobTypeResultBlock)block
{
    CityTypeSelectView *cityPicker = [[CityTypeSelectView alloc]initWithtype2:type dataArray:dataArray block:block];
          
    [cityPicker showWithAnimation:YES];
}



#pragma mark - UIPickerViewDataSource
/**
 *  一共多少咧
 *
 *  @param pickerView
 *
 *  @return
 */
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

/**
 *  返回每一列的行数
 *
 *  @param pickerView
 *  @param component
 *
 *  @return
 */
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    if (self.type == SelectTypesHuKou) {
        return self.cityTypes.count;
    }else if (self.type == SelectTypesNongYe){
        return self.cityTypes2.count;
    }else if (self.type == SelectTypesCard){
         return self.cardTypes.count;
    }else if (self.type == SelectTypesJob ||  self.type == SelectTypesBCJob || self.type == SelectTypesBCIndustry){
        return self.jobTypes.count;
    }else{
          return self.planTypes.count;
    }
    
  
}

//每一行的数据
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    if (self.type == SelectTypesHuKou) {
           return self.cityTypes[row];
       }else if (self.type == SelectTypesNongYe){
           return self.cityTypes2[row];
       }else if (self.type == SelectTypesCard){
           return self.cardTypes[row];
       }else if (self.type == SelectTypesJob){
           SaveJobModel *model = self.jobTypes[row];
           return model.jobName;
       }else if (self.type == SelectTypesBCJob){
           BCDutyModel *model = self.jobTypes[row];
           return model.dutyName;
       }else if (self.type == SelectTypesBCIndustry){
           BCIndustryModel *model = self.jobTypes[row];
           return model.industryName;
       } else{
           
           wxyjModel *model = self.planTypes[row];
           return model.shebao_type;
       }
    
   
    
}

/**
 *  选中某一行后回调 联动的关键
 *
 *  @param pickerView
 *  @param row        用户选择的省份
 *  @param component
 */
- (void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    
    if (self.type == SelectTypesHuKou) {
        _selectedCityType = self.cityTypes[row];
    }else if (self.type == SelectTypesNongYe){
        _selectedCityType = self.cityTypes2[row];
    }else if (self.type == SelectTypesCard){
          _selectedCityType = self.cardTypes[row];
    }else if (self.type == SelectTypesJob ||  self.type == SelectTypesBCJob || self.type == SelectTypesBCIndustry){
        _selectedJobType = self.jobTypes[row];
    }else{
       _selectedPlanType = self.planTypes[row];
    }
    
}
//返回高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 35.0f;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
   
    return SCREEN_WIDTH;
}

#pragma mark - event response
- (void)clickLeftBtn
{
    [self dismissWithAnimation:YES];
}
- (void)clickRightBtn
{
    NSLog(@"点击确定按钮后,执行block回调");
    [self dismissWithAnimation:YES];
    
    NSLog(@"%@",[NSThread currentThread]);
    
    if (self.type == SelectTypesPlan) {
        if (_planBlock && _selectedPlanType) {
            _planBlock(_selectedPlanType);
        }
    }else if (self.type == SelectTypesJob ||  self.type == SelectTypesBCJob || self.type == SelectTypesBCIndustry){
        if (_jobBlock && _selectedJobType) {
            _jobBlock(_selectedJobType);
        }
    }else{
        if (_resultBlock && _selectedCityType) {
            _resultBlock(_selectedCityType);
        }
    }
   
}
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender
{
    [self dismissWithAnimation:NO];
}


#pragma mark - 弹出视图方法
- (void)showWithAnimation:(BOOL)animation
{
    //1.获取当前应用的主窗口
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview: self];
    if (animation) {
        //动画前初始位置
        CGRect rect = self.alertView.frame;
        rect.origin.y = SCREEN_HEIGHT;
        self.alertView.frame = rect;
        
        //浮现动画
        [UIView animateWithDuration:0.3 animations:^{
            CGRect rect = self.alertView.frame;
            rect.origin.y -= kDatePicHeight + kTopViewHeight;
            self.alertView.frame = rect;
        }];
    }
}

#pragma mark - 关闭视图方法
- (void)dismissWithAnimation:(BOOL)animation
{
    //关闭动画
    [UIView animateWithDuration:0.2 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.y += kDatePicHeight + kTopViewHeight;
        self.alertView.frame = rect;
        
        self.backgroundView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.leftBtn removeFromSuperview];
        [self.rightBtn removeFromSuperview];
        [self.titleLabel removeFromSuperview];
        [self.lineView removeFromSuperview];
        [self.topView removeFromSuperview];
        [self.CityTypePicker removeFromSuperview];
        [self.alertView removeFromSuperview];
        [self.backgroundView removeFromSuperview];
        [self removeFromSuperview];
        
        self.leftBtn = nil;
        self.rightBtn = nil;
        self.titleLabel  = nil;
        self.lineView = nil;
        self.topView = nil;
        self.CityTypePicker = nil;
        self.alertView = nil;
        self.backgroundView = nil;
    }];
}




#pragma mark - getters and setters
- (UIPickerView *)CityTypePicker
{
    if (!_CityTypePicker) {
        _CityTypePicker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, kTopViewHeight + 0.5, SCREEN_WIDTH, kDatePicHeight)];
        _CityTypePicker.showsSelectionIndicator = YES;
        _CityTypePicker.delegate = self;
        _CityTypePicker.dataSource = self;
    }
    return _CityTypePicker;
}




- (NSArray*)cityTypes
{
    if (_cityTypes == nil) {
        
        
        _cityTypes = [NSArray arrayWithObjects:@"本地城镇",@"本地农村",@"外地城镇",@"外地农村", nil];
    }
    
    return _cityTypes;
}

- (NSArray*)cityTypes2
{
    if (_cityTypes2 == nil) {
        
        
        _cityTypes2 = [NSArray arrayWithObjects:@"农业",@"非农业", nil];
    }
    
    return _cityTypes2;
}

- (NSArray*)cardTypes
{
    if (_cardTypes == nil) {
        _cardTypes = [NSArray arrayWithObjects:@"不办卡",@"新办卡(不支持补办)", nil];
    }
    
    return _cardTypes;
}


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

@end
