//
//  socialRecord.m
//  Open
//
//  Created by 看看智能 on 2017/3/16.
//  Copyright © 2017年 黄江涛 . All rights reserved.
//

#import "socialRecord.h"
#import "socallCell.h"
#import "AppDelegate.h"
#import "UITableView+FDTemplateLayoutCell.h"

#import "Constants.h"





@interface socialRecord ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UITableView *socTab;
@property(nonatomic)NSUInteger page;
@property(nonatomic,strong)NSMutableArray *baocun;
@property (nonatomic,strong) HXProvincialCitiesCountiesPickerview *regionPickerView;
@property(nonatomic,strong)	 UILabel *addressLabel;
@property(nonatomic,strong)	 NSString *numbertag;

@property(nonatomic,strong,)NSString *navTitle;
@property(nonatomic,strong)	 NSMutableArray *array2;
@property(nonatomic,strong)	 NSMutableArray *array0;
@property(nonatomic,strong)	 NSMutableArray *dataArr;
@property(nonatomic,strong)UIButton *titleButton;



@end

@implementation socialRecord
-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr =[NSMutableArray new];
    }
    return _dataArr;
}
-(NSMutableArray *)array0
{
    if (!_array0) {
        _array0 =[NSMutableArray new];
        
    }
    return _array0;
}
-(NSMutableArray *)array2
{
    if (!_array2 ) {
        _array2 =[NSMutableArray array];
        
    }
    return _array2;
    
}
-(NSMutableArray *)baocun
{
    if (!_baocun) {
        _baocun =[NSMutableArray new];
    }
    return _baocun;
}
- (void)viewDidLoad {
    [super viewDidLoad];
//     if ([getPeopleID applePaycity]) {
    self.socTab.estimatedRowHeight = 140;
    self.socTab.rowHeight = UITableViewAutomaticDimension;
    NSString *version = [UIDevice currentDevice].systemVersion;

//    if (@available(iOS 11.0, *) ) {
//
//        if( [UIScreen mainScreen].bounds.size.height == 812 || [UIScreen mainScreen].bounds.size.height == 896){
//            self.socTab.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//            self.socTab.contentInset = UIEdgeInsetsMake(88, 0, 49, 0);
//            self.socTab.scrollIndicatorInsets = self.socTab.contentInset;
//        }else{
//            self.socTab.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//            self.socTab.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
//            self.socTab.scrollIndicatorInsets = self.socTab.contentInset;
//        }
//    }
    //雷俊博
    //防止隐藏导航栏,内容向上移动
  //  self.edgesForExtendedLayout = UIRectEdgeNone;
    
    
    if (@available(iOS 11.0, *) ) {
            self.socTab.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            self.socTab.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
            self.socTab.scrollIndicatorInsets = self.socTab.contentInset;
        
        if (self.isHomeVC) {
            self.socTab.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            self.socTab.contentInset = UIEdgeInsetsMake(cl_kNavigationBarHeight, 0, 49, 0);
            self.socTab.scrollIndicatorInsets = self.socTab.contentInset;
        }
    }
    
    

    [self getprovinceMessage];

    self.page =1;
    
    [self addNaviButton];
    
     self.socTab.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
       __block   NSString *path =nil;
     self.socTab.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
         
 
         if (self.record == 1) {
            path = [IPDIZHI stringByAppendingString:@"/socialsecurity/kankan/getInsertUserLibState"];
         }else{
          path = [IPDIZHI stringByAppendingString:@"/socialsecurity/kankan/myCertificationRecord"]; //展示采集的用户信息接口
         }
    [CityNetManager myCertificationUserID:[getPeopleID PeopleID] andPage:1 province:self.navTitle andPath:path completionHandle:^(socialModelRecord *model, NSError *error) {
        if (error) {
            [SVProgressHUD showErrorWithStatus:@"暂时无法使用"];
            return;
        }
        if (model.code != 0) {
            codeSelected *code = [codeSelected MustTakeReturn:model.code];
            [code returnCodeMethod];
        }
        [self.baocun removeAllObjects];
        if (!model.result) {
          
        }else{
//  
            [self.baocun removeAllObjects];
            for (socialResultModel *models in model.result) {
                
                [self.baocun addObject:models];
                NSLog(@"dataArr:%@",models.identityNumber);
                
            }
            
            
            
        }
        [self.socTab reloadData];
        [self.socTab.mj_footer resetNoMoreData];
        [_socTab.mj_header endRefreshing];

    }];
     }];
    self.socTab.mj_footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
          self.page +=1;
    
        [CityNetManager myCertificationUserID:[getPeopleID PeopleID] andPage:self.page province:self.navTitle andPath:path  completionHandle:^(socialModelRecord *model, NSError *error) {
          
            [self.baocun addObjectsFromArray:model.result];
            NSLog(@"self.bao:%@",self.baocun);
            [self.socTab reloadData];
            if (model.result.count == 0) {
                 [_socTab.mj_footer endRefreshingWithNoMoreData];
            }else
            {
            [_socTab.mj_footer endRefreshing];
            }
        }];

    }];
    [self.socTab.mj_header beginRefreshing];
    

}
//导航栏的按钮
-(void)addNaviButton
{
   self.titleButton =[[UIButton alloc]init];
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
   
    if ( delegate.isLogin == YES) {
        
        if ( [getPeopleID getprovinceNightSix]  != nil  )  {
            self.navTitle =[getPeopleID getprovinceNightSix];
        }else{
            self.navTitle = [getPeopleID province];
        }
        
        
    }
    _titleButton.frame=CGRectMake(0, 20, 150, 40);
   _titleButton.titleLabel.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:17];
       _titleButton.titleLabel.textAlignment =NSTextAlignmentCenter;
    [_titleButton setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
    [_titleButton setTitle:self.navTitle forState:UIControlStateNormal];
    
//     设置背景图片
    [_titleButton addTarget:self action:@selector(titleButtonClickd:) forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.titleView = _titleButton;
}
-(void)titleButtonClickd:(UIButton *)sender{

    NSString *address = _addressLabel.text;
    //用空格来分割字符串
    NSArray *array = [address componentsSeparatedByString:@" "];
    provinceLabel = array[0];
    [self.regionPickerView showPickerWithProvinceName:provinceLabel cityName:nil countyName:nil index:1];
}
-(void)savensuserDefault
{
    NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];
    [userDefaults setObject: self.array2 forKey:@"myArray"];
    [userDefaults synchronize];
    
}
- (HXProvincialCitiesCountiesPickerview *)regionPickerView {
    if (!_regionPickerView) {
        _regionPickerView = [[HXProvincialCitiesCountiesPickerview alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        __weak typeof(self) wself = self;
        wself.regionPickerView.completion = ^(NSString *provinceName,NSString *numberSign,NSString *countyName) {
            __strong typeof(wself) self = wself;
         self.titleButton.titleLabel.text=[getPeopleID province];
            self.navTitle = provinceName;
            [_titleButton setTitle:self.navTitle forState:UIControlStateNormal];
                [_titleButton setImage:[UIImage imageNamed:@"navigationbar_arrow_up"] forState:UIControlStateNormal];
            [self.baocun removeAllObjects];
            [self.socTab reloadData];
//            [self.socTab.mj_footer beginRefreshing];
            [self.view reloadInputViews];
            [self.socTab.mj_header beginRefreshing];
            
            
        };
        [self.navigationController.view addSubview:_regionPickerView];
    }
    return _regionPickerView;
        }
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.baocun.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    self.socTab.userInteractionEnabled =NO;

   socallCell *cell   =[tableView dequeueReusableCellWithIdentifier:@"socialCell" forIndexPath:indexPath];
    
    socialResultModel *socModel =self.baocun[indexPath.row];
   
    if (self.record == 1 ) {
        cell.record = 1;
         cell.socModel =socModel;

        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
        return cell;
    }else{
        cell.record = 0;
         cell.socModel =socModel;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}
-(void)getprovinceMessage
{
    NSString *path =[IPDIZHI stringByAppendingString:@"/socialsecurity/kankan/getProvince"];
    NSLog(@"%@",path);
    NSString *NameEnerypt =  [EnercryTool A:@"黄江涛"];
    NSString *IdentiftyEnerypt = [EnercryTool A:@"42112619940907171X"];
    [CityNetManager getMoreCityForThisMethod:@"name" encrypt:NameEnerypt andTheOther:@"identityNumber" twoEncrypt:IdentiftyEnerypt  andPutThePathForThis:path andID:[getPeopleID PeopleID] completionHandle:^(CityModel *model, NSError *error) {
        if(error){
            [SVProgressHUD showErrorWithStatus:@"服务器无法连接"];
            return;
        }
        [self.array0 addObjectsFromArray:model.result];
        for (int row = 0 ; row < 30 ; row++) {
            
            if (self.array0 == nil) {
                return;
            }
            
            CityResultModel *city =self.array0[row];
            [self.array2 addObject:city.provice_name];
            
            NSData *data =[NSKeyedArchiver archivedDataWithRootObject:city.provice_id];
            
            [self.dataArr addObject:data];
//            NSLog(@"%@",self.dataArr[row]);
            
            NSLog(@"huangjiangtao:%@",self.array2[row]);
            
        }
        NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];
        [userDefaults setObject:self.dataArr forKey:@"myPrivinceModel"];
        [userDefaults synchronize];
        [self  savensuserDefault ];
    }];



}
//-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//
//    return UITableViewAutomaticDimension;
//
//}
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//
//////            socialResultModel *socModel =self.baocun[indexPath.row]
////                    return [tableView fd_heightForCellWithIdentifier:@"socialCell" cacheByKey:entity.identifier configuration:^(socialRecord *cell) {
////        //                [self configureCell:cell atIndexPath:indexPath];
////                    }];
//    return  [tableView fd_heightForCellWithIdentifier:@"socialCell" cacheByIndexPath:indexPath configuration:^(socallCell * cell) {
//        socialResultModel *socModel =self.baocun[indexPath.row];
//
//        if (self.record == 1 ) {
//            cell.record = 1;
//            cell.socModel =socModel;
//            cell.selectionStyle=UITableViewCellSelectionStyleNone;
//        }else{
//            cell.record = 0;
//            cell.socModel =socModel;
//            cell.selectionStyle=UITableViewCellSelectionStyleNone;
//        }
//
//
//    }];
//}

    
    
    
    
//                return [tableView fd_heightForCellWithIdentifier:@"socialCell" cacheByKey:entity.identifier configuration:^(socialRecord *cell) {
//    //                [self configureCell:cell atIndexPath:indexPath];
//                }];
    
//    return [tableView fd_heightForCellWithIdentifier:@"socialCell" configuration:^(socallCell *cell) {
//        cell.fd_enforceFrameLayout = NO;
//        socialResultModel *socModel =self.baocun[indexPath.row];
//        if (self.record == 1 ) {
//            cell.record = 1;
//            cell.socModel =socModel;
//            cell.selectionStyle=UITableViewCellSelectionStyleNone;
//        }else{
//            cell.record = 0;
//            cell.socModel =socModel;
//            //        CGSize size = [.text boundingRectWithSize:cell.address.bounds.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:cell.address.font} context:nil].size;
//            cell.selectionStyle=UITableViewCellSelectionStyleNone;
 
//        }
//    }];
    

// static NSString *collectionCellID = @"socialCell";
//socallCell * cells = [[socallCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:collectionCellID];
//
//
//  socialResultModel *socModel =self.baocun[indexPath.row];
//    cells.socModel = socModel;
//    NSLog(@"%f",cells.nameTs);
//
//
//
//
//
////    CGSize size = [self.socCell sizeThatFits:CGSizeMake(self.address.bounds.size.width,CGFLOAT_MAX)];
////    //        NSLog(@"%lf-------%lf",size.height,size.height,cell.address.bounds.size.height);
////
////    CGSize sizeTWO = [self.suggestts sizeThatFits:CGSizeMake(self.suggestts.bounds.size.width, CGFLOAT_MAX)];
////
//
//  //社保采集
////    if (self.record ==1) {
////          return 125 +cell.reminderHeight;
////    }else if(self.record == 0){
//////社保认证
////          return 130  + self.addressCg+self.reminderHeight;
////    }else{
////
////        return 0;
////    }
//
//    return 10;
//
//}




@end
