//
//  YFGreenChannelVC.m
//  KKCivilAd
//
//  Created by 雷俊博 on 2020/8/3.
//  Copyright © 2020 雷俊博. All rights reserved.
//

#import "YFGreenChannelVC.h"
#import "UIScrollView+UITouch.h"
#import "UIImage+Custom.h"
#import "UIImage+Extension.h"

#import "GreenChannelImageView.h"

#import "TestViewController.h"
#import "AuthenticationFinishController.h"
#import "AuthenticationFailedController.h"
#import "CommitIDInfoController.h"
#import "AuthenticationReviewController.h"
#import "AuthenticationButieFinishVC.h"
@interface YFGreenChannelVC ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    NSInteger _selectIndex;
    
    UIImage *_lastImage;
}

@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)NSMutableArray *imagesArray;
@property (nonatomic, strong)NSMutableArray *images;

@property (nonatomic, strong)UIButton *commitBtn;

//scrollview
@property (nonatomic, strong)UIView *topView;
@property (nonatomic, strong)GreenChannelImageView *cardView;

@property (nonatomic, strong)UILabel *lb_title;

@property (nonatomic, strong)UILabel *lb_remind;

@property (nonatomic, strong)UILabel *lb_five;

@property (nonatomic, strong)UIButton *btn_CustomerService;

@property (nonatomic, strong)UIImagePickerController *imagePicker;


@end

@implementation YFGreenChannelVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.commitBtn];
       
    self.view.backgroundColor = [UIColor whiteColor];
       
       //[self.imagesArray addObject:self.lastVCImage];
       //[self.cardView.btn_left setBackgroundImage:self.lastVCImage forState:UIControlStateNormal];
    _lastImage = self.lastVCImage;
       
       //起点发生变化
    self.edgesForExtendedLayout=UIRectEdgeBottom;
       
    [self configBackBtn];
       
    self.images = [[NSMutableArray alloc]initWithCapacity:2];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
   // [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    //   [self.navigationController.navigationBar setShadowImage:nil];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
      // [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
    if ([self.type isEqualToString:@"butie"]) {
        [GTCountSDK trackCountEvent:@"home_BTAuth_greenChannelPage" withArgs:@{}];
    }else{
        [GTCountSDK trackCountEvent:@"home_YFAuth_greenChannelPage" withArgs:@{}];
    }
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    [self.commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(45 * WIDTHRADIU);
    }];
}


#pragma mark - 导航栏返回按钮及点击事件
- (void)configBackBtn
{
    UIButton *backbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backbtn.frame = CGRectMake(-50, 0, 40, 40);
    [backbtn setTitle:@"返回" forState:UIControlStateNormal];
    [backbtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backbtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [backbtn layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleLeft imageTitleSpace:6];
    
    
    
//    if (self.state == GreenRenZheng) {
//        UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:backbtn];
//
//        self.navigationItem.leftBarButtonItem = backItem;
//    }
}

- (void)backBtnClick:(UIButton *)btn
{
//    BOOL show = [[NSUserDefaults standardUserDefaults]boolForKey:@"SocialSecurityAuthenticationggreenVC"];
//
//    if (!show) {
//        [AuthenticationGuideView showGuideViewWithimgHidden:YES Block:^(BOOL isBack) {
//            [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"SocialSecurityAuthenticationggreenVC"];
//            [[NSUserDefaults standardUserDefaults]synchronize];
//            if (isBack) {
//
//
//                [self.navigationController popViewControllerAnimated:YES];
//            }
//        }];
//    }else {
//        [self.navigationController popViewControllerAnimated:YES];
//    }
    
    
    
}


#pragma mark - event response
- (void)btnClick:(UIButton *)sender
{
    
    NSLog(@"%ld",sender.tag);
    if (sender == self.commitBtn) {
        
//        TestViewController *vc = [Storyboard instantiateViewControllerWithIdentifier:@"TestViewController"];
//           vc.title = @"查看图片";
//           vc.bestImage = self.images[0];
//           vc.liveMouth = self.images[1];
//        [self.navigationController pushViewController:vc animated:YES];
//
//        return;
        [self uploadData];
//        if (self.state == GreenRenZheng) {
//            [self uploadData];
//        }else if(self.state == GreenCaiJi){
//
//            [self uploadData2];
//        }
        
    }else {
        if (self.imagesArray.count == 0 && sender == self.cardView.btn_right) {
            [self alertShow:@"请先拍摄第一张照片"];
            return;
        }
        _selectIndex = sender.tag;

        [self getPhoto];
    }
  
   
}

- (void)kehuchatClick:(UIButton *)sender
{
//    UIStoryboard *sb  =[UIStoryboard storyboardWithName:@"Main" bundle:nil];
//    WebViewController *tc=[sb instantiateViewControllerWithIdentifier:@"webViewController"];
//
//    tc.URL = [NSURL URLWithString:@"http://kefu.weface.com.cn/index.html"];
//
//    tc.title = @"人工客服";
//
//    [self.navigationController pushViewController:tc animated:YES];
}
#pragma mark 处理自定义视图操作事件
//- (void)handleCustomActionEnvent:(ZJAnimationPopView *)popView
//{
//
//}
#pragma mark - 绿色通道上传图片
- (NSDictionary *)requestParams
{
    NSDictionary *params = nil;
    if (self.telphone == nil) {
        params = @{
            @"addressRelation":_addressRelation,
            @"nowAddress":_nowAddress,
            @"fromModel":@4,
            @"identityNumber":_identityNumber,
            @"indentityFacePhoto":@"",
            @"lal":_aLocation,
            @"libid":_libid,
            @"recordPhotos":_images,
            @"verifytype":_verifytype
        };
    }else{
        params = @{
        @"addressRelation":_addressRelation,
        @"nowAddress":_nowAddress,
        @"fromModel":@4,
        @"identityNumber":_identityNumber,
        @"indentityFacePhoto":@"",
        @"lal":_aLocation,
        @"libid":_libid,
        @"recordPhotos":_images,
        @"verifytype":_verifytype,
        @"telphone":_telphone
        };
    }
    return params;
}

//认证
- (void)uploadData
{
    if (self.imagesArray.count < 2) {
        [self alertShow:@"要传2张自拍照片"];
        return;
    }
    [self uploadDataRequest];
    
}

- (void)uploadDataRequest
{
    if ([self.type isEqualToString:@"butie"]) {
        [self butieUploadDataRequest];
    }else{
        [self youfuUploadDataRequest];
    }
}

- (void)youfuUploadDataRequest
{
    WeakSelf
    NSString *urlString = [NSString stringWithFormat:@"%@%@",[NetworkConfig ipYouFuVerityConfig],DistrictAuthenticationUploadImageRequest];
    NSLog(@"民政认证绿色通道url%@",urlString);
    NSLog(@"prams:%@",[self requestParams]);
    NSString *token = [UserDefault objectForKey:@"token"];
    
    [SVProgressHUD show];
    [NetworkRequest Post3:urlString Params:[self requestParams] Token:token Success:^(id  _Nonnull responseObject) {
        [SVProgressHUD dismiss];
        NSLog(@"responseObject:%@",responseObject);
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSString *state = [NSString stringWithFormat:@"%@",responseObject[@"state"]];
            if ([state isEqualToString:@"200"]) {
                if ([responseObject[@"result"] isKindOfClass:[NSDictionary class]]) {
                    NSString *code =  [NSString stringWithFormat:@"%@",responseObject[@"result"][@"code"]];
                    if ([code isEqualToString:@"200100"]) {
                        
                        AuthenticationFinishController *vc = [Storyboard instantiateViewControllerWithIdentifier:@"AuthenticationFinishController"];
                        vc.title = @"认证完成";
                        vc.imagesArray = self->_images;
                        if ([responseObject[@"result"][@"data"] isKindOfClass:[NSDictionary class]]){
                            vc.defaultPhoto = responseObject[@"result"][@"data"][@"defaultPhoto"];
                        }
                        vc.model = self.model;
                        vc.location = self->_nowAddress;
                        vc.phone = self.tf_telphone;
                        vc.recordId = [NSString stringWithFormat:@"%@",responseObject[@"result"][@"data"][@"recordId"]];
                        vc.type = self.type;
                        

                        [weakSelf.navigationController pushViewController:vc animated:YES];
                        
                    }else if ([code isEqualToString:@"200001"]){
                        [self alertShow:@"照片不合格"];
                    }else if ([code isEqualToString:@"200002"]){
                        [self alertShow:@"身份证号或id错误"];
                    }else if ([code isEqualToString:@"200200"]){
                        AuthenticationFailedController *vc = [Storyboard instantiateViewControllerWithIdentifier:@"AuthenticationFailedController"];
                                               vc.title = @"认证完成";
                        vc.type = self.type;
                        [weakSelf.navigationController pushViewController:vc animated:YES];
                    }else if ([code isEqualToString:@"200300"]){
                        NSDictionary *data = responseObject[@"result"][@"data"];
                         if ([data isKindOfClass:[NSDictionary class]])
                         {
                                                   
                                if ([data[@"idcardPic"] isKindOfClass:[NSNumber class]])
                                {
                                                 
                                        NSNumber *idcardPic = (NSNumber *)data[@"idcardPic"];
                                        NSString *recordId = [NSString stringWithFormat:@"%@",data[@"recordId"]];
                                                                                                    
                                        if (idcardPic.boolValue)
                                        {
                                            if (![Tools isBlankString:recordId]) {
                                                 CommitIDInfoController *vc = [Storyboard instantiateViewControllerWithIdentifier:@"CommitIDInfoController"];
                                                 vc.title = @"证件信息";
                                                 vc.hidesBottomBarWhenPushed = YES;
                                                vc.recordId = recordId;
                                                vc.type = self.type;
                                                [self.navigationController pushViewController:vc animated:YES];
                                            }
                                            
                                        }else{
                                            
                                            [self alertShow:@"认证审核中,请耐心等待审核结果"];
                                            AuthenticationReviewController *vc = [Storyboard instantiateViewControllerWithIdentifier:@"AuthenticationReviewController"];
                                            vc.title = @"认证完成";
                                            [self.navigationController pushViewController:vc animated:YES];
                                        }
                                                   
                                }
                         
                          }
                    }else if ([code isEqualToString:@"400001"]){
                        [self alertShow:@"终端参数异常"];
                    }else if ([code isEqualToString:@"400002"]){
                        [self alertShow:@"绿色通道至少需要2张图片"];
                    }else if ([code isEqualToString:@"400004"]){
                        [self alertShow:@"参数不是base64照片"];
                    }else{
                        [self alertShow:@"认证失败"];
                    }
                
                    
                }else{
                    [self alertShow:@"服务器异常"];
                }
            }else if ([state isEqualToString:@"700"]){
                [weakSelf reloadData];
            }else{
                [self alertShow:@"请求失败"];
            }
            
        }else{
            [self alertShow:@"服务器异常"];
        }
        
        
    } Failure:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
        NSLog(@"error:%@",error);
        [self alertShow:@"网络异常"];
    }];
}

- (void)butieUploadDataRequest
{
    WeakSelf
    NSString *urlString = [NSString stringWithFormat:@"%@%@",[NetworkConfig ipYouFuVerityConfig],DistrictAuthenticationUploadImageRequest];
    NSLog(@"民政认证绿色通道url%@",urlString);
    NSLog(@"prams:%@",[self requestParams]);
    NSString *token = [UserDefault objectForKey:@"token"];
    
    [SVProgressHUD show];
    [NetworkRequest Post7:urlString Params:[self requestParams] Token:token Success:^(id  _Nonnull responseObject) {
        [SVProgressHUD dismiss];
        NSLog(@"responseObject:%@",responseObject);
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            NSString *state = [NSString stringWithFormat:@"%@",responseObject[@"state"]];
            if ([state isEqualToString:@"200"]) {
                if ([responseObject[@"result"] isKindOfClass:[NSDictionary class]]) {
                    NSString *code =  [NSString stringWithFormat:@"%@",responseObject[@"result"][@"code"]];
                    if ([code isEqualToString:@"200100"]) {
                        
//                        AuthenticationFinishController *vc = [Storyboard instantiateViewControllerWithIdentifier:@"AuthenticationFinishController"];
                        AuthenticationButieFinishVC *vc = [[AuthenticationButieFinishVC alloc]init];
                        vc.title = @"认证完成";
                        vc.imagesArray = self->_images;
                        if ([responseObject[@"result"][@"data"] isKindOfClass:[NSDictionary class]]){
                            vc.defaultPhoto = responseObject[@"result"][@"data"][@"defaultPhoto"];
                        }
                        vc.model = self.model;
                        vc.location = self->_nowAddress;
                        vc.phone = self.tf_telphone;
                        vc.recordId = [NSString stringWithFormat:@"%@",responseObject[@"result"][@"data"][@"recordId"]];
                        vc.type = self.type;

                        [weakSelf.navigationController pushViewController:vc animated:YES];
                        
                    }else if ([code isEqualToString:@"200001"]){
                        [self alertShow:@"照片不合格"];
                    }else if ([code isEqualToString:@"200002"]){
                        [self alertShow:@"身份证号或id错误"];
                    }else if ([code isEqualToString:@"200200"]){
                        AuthenticationFailedController *vc = [Storyboard instantiateViewControllerWithIdentifier:@"AuthenticationFailedController"];
                                               vc.title = @"认证完成";
                        vc.type = self.type;
                        [weakSelf.navigationController pushViewController:vc animated:YES];
                    }else if ([code isEqualToString:@"200300"]){
                        NSDictionary *data = responseObject[@"result"][@"data"];
                         if ([data isKindOfClass:[NSDictionary class]])
                         {
                                                   
                                if ([data[@"idcardPic"] isKindOfClass:[NSNumber class]])
                                {
                                                 
                                        NSNumber *idcardPic = (NSNumber *)data[@"idcardPic"];
                                        NSString *recordId = [NSString stringWithFormat:@"%@",data[@"recordId"]];
                                                                                                    
                                        if (idcardPic.boolValue)
                                        {
                                            if (![Tools isBlankString:recordId]) {
                                                 CommitIDInfoController *vc = [Storyboard instantiateViewControllerWithIdentifier:@"CommitIDInfoController"];
                                                 vc.title = @"证件信息";
                                                 vc.hidesBottomBarWhenPushed = YES;
                                                vc.recordId = recordId;
                                                vc.type = self.type;
                                                [self.navigationController pushViewController:vc animated:YES];
                                            }
                                            
                                        }else{
                                            
                                            [self alertShow:@"认证审核中,请耐心等待审核结果"];
                                            AuthenticationReviewController *vc = [Storyboard instantiateViewControllerWithIdentifier:@"AuthenticationReviewController"];
                                            vc.title = @"认证完成";
                                            [self.navigationController pushViewController:vc animated:YES];
                                        }
                                                   
                                }
                         
                          }
                    }else if ([code isEqualToString:@"400001"]){
                        [self alertShow:@"终端参数异常"];
                    }else if ([code isEqualToString:@"400002"]){
                        [self alertShow:@"绿色通道至少需要2张图片"];
                    }else if ([code isEqualToString:@"400004"]){
                        [self alertShow:@"参数不是base64照片"];
                    }else{
                        [self alertShow:@"认证失败"];
                    }
                
                    
                }else{
                    [self alertShow:@"服务器异常"];
                }
            }else if ([state isEqualToString:@"700"]){
                [weakSelf reloadData];
            }else{
                [self alertShow:@"请求失败"];
            }
            
        }else{
            [self alertShow:@"服务器异常"];
        }
        
        
    } Failure:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
        NSLog(@"error:%@",error);
        [self alertShow:@"网络异常"];
    }];
}


- (void)reloadData
{
    WeakSelf
    [NetworkTokenRequest getTokenSuccess:^(BOOL success) {
        if (success) {
            [weakSelf uploadDataRequest];
        }else{
            [self alertShow:@"请求失败"];
        }
    }];
}
//采集
- (void)uploadData2
{
    
   /*
    
   NSString *path =[IPDIZHI stringByAppendingString:@"/socialsecurity/kankan/greenChannelInsert"];
    [SVProgressHUD shows];
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < self.imagesArray.count; i++) {
        NSString * str = [self changImageBase64:self.imagesArray[i]];
        [array addObject:str];
    }
    [CityNetManager getSuccessSenderSocialMessageInsertInfo:path getName:self.social.nText privateStringIdentityNumber:self.social.idText andProvinceName:self.social.province.text andCityName:self.social.downTown.text andCountryName:self.social.area.text andTownName:self.social.village.text andvillageName:self.social.villageCommittee.text andTelephone:self.social.phoneNumber.text andCompanyName:self.social.Company.text andFlag:2 andVerifyType:self.social.ice andUimage:array ocrIMage:self.social.ocrBase64 ocrIMage_:self.social.ocrBase64_ completionHandle:^(verifyAdressModel * model, NSError * error) {
        [SVProgressHUD doneShow];
        [SVProgressHUD dismiss];
        if(model.code == 0)
        {
            NSString *str =nil;
            if ([getPeopleID getpeopleLimited]  ==YES) {
                
                str =[NSString stringWithFormat:@"%@,采集成功",self.social.nameText.text];
            }else{
                
                str =[NSString stringWithFormat:@"%@,采集成功,请等待5个工作日审核身份，通过后，进行社保身份认证，认证通过后可正常领取养老金",self.social.nameText.text];
            }
            
            if ([model.bak isEqualToString:@"0"]) {
                str = nil;
                str =@"仅识别出一张人脸,按返回重新采集";
                UIAlertController *alertCon =[UIAlertController alertControllerWithTitle:str message:nil preferredStyle:UIAlertControllerStyleAlert];
                [alertCon addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                }]];
                
                [self presentViewController:alertCon animated:YES completion:nil];
                return;
            }else if ([model.bak isEqualToString:@"1"]){
                str = nil;
                str =@"仅识别出一张人脸,按返回重新采集";
                UIAlertController *alertCon =[UIAlertController alertControllerWithTitle:str message:nil preferredStyle:UIAlertControllerStyleAlert];
                [alertCon addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                }]];
                
                [self presentViewController:alertCon animated:YES completion:nil];
            }else if ([model.bak isEqualToString:@"2"]){
                    __weak typeof(self) weakSelf = self;
                [SuccessBaseView showSuccessAlertViewWithTitle:@"恭喜采集成功" name:self.social.nameText.text address:[NSString stringWithFormat:@"%@%@%@",self.social.province.text,self.social.downTown.text,self.social.area.text] type:GreenCaiJi block:^(NSInteger tag) {
                    NSLog(@"点击第%ld个按钮",tag);
                    if (tag == 202) {
                        //2017年11月23日注释了
                        weakSelf.social.ocrBase64=nil;
                        weakSelf.social.ocrBase64_ =nil;
                        weakSelf.social.phoneNumber.text =@"";
                        [weakSelf.navigationController popToRootViewControllerAnimated:YES];
                    }else if (tag == 200){
                        [weakSelf shareWebPageToPlatformType:UMSocialPlatformType_WechatSession];
                    }else if (tag == 201){
                        [weakSelf shareWebPageToPlatformType:UMSocialPlatformType_WechatTimeLine];
                    }
                }];
               
                
                
            }
            
           
        }else
        {
            
            codeSelected *code = [codeSelected MustTakeReturn:model.code];
            [code returnCodeMethod];
            return;
        }
    }];
    */
}

- (void)alertShow:(NSString *)msg
{
    if (@available(iOS 9.0,*)) {
        
        UIAlertController * alertV = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertV addAction:confirm];
        
        [self presentViewController:alertV animated:YES completion:nil];
        
    }else{
        UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alertV show];
    }
}

- (void)getPhoto
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请选择获取图片操作" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//    UIAlertAction *photos = [UIAlertAction actionWithTitle:@"从相册中" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        self.imagePicker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
//        [self presentViewController:self.imagePicker animated:YES completion:nil];
//        
//    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    UIAlertAction *makePhotos = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
        [self presentViewController: self.imagePicker animated:YES completion:nil];
     
    }];
    //[alert addAction:photos];
    [alert addAction:makePhotos];
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
}



#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController: (UIImagePickerController *)picker
didFinishPickingMediaWithInfo: (NSDictionary *)info
{
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    UIImage *newImg = [image scaleToWidth:480];
    NSData *data = UIImageJPEGRepresentation(newImg, 0.85);
    NSString *imageStr = [SecurityUtil encodeBase64Data:data];
    NSLog(@"selectImg=%@",image);
    if (self.imagesArray.count == 2) {
        if (_selectIndex == 200) {
            _lastImage = image;
            [self.imagesArray replaceObjectAtIndex:0 withObject:image];
            [self.images replaceObjectAtIndex:0 withObject:imageStr];
        }else if (_selectIndex == 201){
            [self.imagesArray replaceObjectAtIndex:1 withObject:image];
            [self.images replaceObjectAtIndex:1 withObject:imageStr];
        }
    }else if (self.imagesArray.count == 1){
        if (_selectIndex == 200) {
            _lastImage = image;
            [self.imagesArray replaceObjectAtIndex:0 withObject:image];
            [self.images replaceObjectAtIndex:0 withObject:imageStr];
        }else if (_selectIndex == 201){
            [self.imagesArray addObject:image];
            [self.images addObject: imageStr];
        }
    }else if (self.imagesArray.count == 0){
        if (_selectIndex == 200) {
            _lastImage = image;
            [self.imagesArray addObject:image];
            [self.images addObject:imageStr];
        }
    }
    
    
    if (_selectIndex == 200) {
        [self.cardView.btn_left setBackgroundImage:image forState:UIControlStateNormal];
    }else{
        [self.cardView.btn_right setBackgroundImage:image forState:UIControlStateNormal];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 用户选择取消
- (void) imagePickerControllerDidCancel: (UIImagePickerController *)picker
{
   [self dismissViewControllerAnimated:YES completion:nil];
   
}


#pragma mark - 分享
////网页分享
//- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
//{
//    //创建分享消息对象
//    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//
//    //创建网页内容对象
//    NSString* thumbURL =  UMS_THUMB_IMAGE;
//    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:UMS_Title descr:UMS_Text thumImage:thumbURL];
//    //设置网页地址
//    shareObject.webpageUrl = UMS_WebLink;
//
//    //分享消息对象设置分享内容对象
//    messageObject.shareObject = shareObject;
//
//    //调用分享接口
//    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
//        if (error) {
//            UMSocialLogInfo(@"************Share fail with error %@*********",error);
//        }else{
//            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
//                UMSocialShareResponse *resp = data;
//                //分享结果消息
//                UMSocialLogInfo(@"response message is %@",resp.message);
//                //第三方原始返回的数据
//                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
//
//            }else{
//                UMSocialLogInfo(@"response data is %@",data);
//            }
//        }
//        [self alertWithError:error];
//    }];
//}


- (void)alertWithError:(NSError *)error
{
    NSString *result = nil;
    if (!error) {
        result = [NSString stringWithFormat:@"分享成功"];
    }
    else{
        NSMutableString *str = [NSMutableString string];
        if (error.userInfo) {
            for (NSString *key in error.userInfo) {
                [str appendFormat:@"%@ = %@\n", key, error.userInfo[key]];
            }
        }
        if (error) {
            result = [NSString stringWithFormat:@"Share fail with error code: %d\n%@",(int)error.code, str];
            result = [NSString stringWithFormat:@"分享失败"];
        }
        else{
            result = [NSString stringWithFormat:@"Share fail"];
        }
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                    message:result
                                                   delegate:nil
                                          cancelButtonTitle:NSLocalizedString(@"确定", @"确定")
                                          otherButtonTitles:nil];
    [alert show];
}


#pragma mark - private methods
-(NSString *)changeBase64:(UIImage*)image
{
//    CGSize imagesize = image.size;
//    NSLog(@"%lf",image.size.height);
//    NSLog(@"%lf",image.size.width);
//    imagesize.height =50;
//    imagesize.width =50;
//    //对图片大小进行压缩--
//
//    //    UIImage *image1=   [SecondSocialViewController imageCompressFitSizeScale:image targetSize:CGSizeMake(image.size.width/4, image.size.height/4)];
//
//    UIImage *image1=    [UIImage imageByScalingAndCroppingForSize:CGSizeMake(483, 644) and:image];
//    //照片是对的完全没有问题,如果想加快速度必须让戴海峰配合的去弄,否则毫无办法,因为把他放在网上转base64是可以转出来的,如果想提速只要调用上面的这个方法即可.否则毫无办法,上面两个方法都是可以的
//    //这里的0.3决定了照片认证的精确值
//   // image1 =   [SecondSocialViewController image:image1 rotation:UIImageOrientationLeft];
//   // NSData *str2=UIImageJPEGRepresentation(image1, 0.4);
//
//
//    NSString *str3=[EnercryTool encryptBase64:str2];
//
//
//    return str3;
    
    return nil;
    
}


#pragma mark - getters and setters
- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - cl_kNavigationBarHeight - 45 * WIDTHRADIU)];
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.contentSize = CGSizeMake(ScreenWidth, ScreenHeight - cl_kNavigationBarHeight - 45 * WIDTHRADIU);
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        [_scrollView addSubview:self.topView];
        [_scrollView addSubview:self.cardView];
        [_scrollView addSubview:self.lb_title];
        [_scrollView addSubview:self.lb_remind];
        [_scrollView addSubview:self.lb_five];
        [_scrollView addSubview:self.btn_CustomerService];
        [self setupUI];
    }
    return _scrollView;
}

- (void)setupUI
{
    [self.lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cardView.mas_bottom).offset(35 * WIDTHRADIU);
        make.left.offset(20 * WIDTHRADIU);
        make.height.offset(16 * WIDTHRADIU);
    }];
    [self.lb_remind mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lb_title.mas_bottom).offset(18 * WIDTHRADIU);
        make.left.equalTo(self.lb_title);
        make.right.equalTo( self.cardView);
    }];
    [self.lb_five mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lb_remind.mas_bottom).offset(10 * WIDTHRADIU);
        make.left.equalTo(self.lb_remind);
        make.height.offset(14 * WIDTHRADIU);
    }];
    [self.btn_CustomerService mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.lb_five);
        make.left.equalTo(self.lb_five.mas_right).offset(10);
    }];
}
- (UIView *)topView
{
    if (!_topView) {
        _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, 125 * WIDTHRADIU)];
        _topView.backgroundColor = K_MainColor;
    }
    return _topView;
}

- (UIButton *)commitBtn
{
    if (!_commitBtn) {
        _commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _commitBtn.backgroundColor = [UIColor colorWithHex:0x11a480];
        [_commitBtn setTitle:@"确认提交" forState:UIControlStateNormal];
        _commitBtn.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
        [_commitBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _commitBtn;
}

- (GreenChannelImageView *)cardView
{
    if (!_cardView) {
        _cardView = [[GreenChannelImageView alloc]initWithFrame:CGRectMake(10 * WIDTHRADIU, 20 * WIDTHRADIU, ScreenWidth - 20 * WIDTHRADIU, 237 * WIDTHRADIU)];
        _cardView.backgroundColor = [UIColor whiteColor];
        _cardView.layer.cornerRadius = 10 * WIDTHRADIU;
        _cardView.layer.shadowColor = K_RGB(23, 123, 205).CGColor;
        _cardView.layer.shadowOffset = CGSizeMake(0, 0);
        _cardView.layer.shadowOpacity = 0.25;
        _cardView.layer.shadowRadius = 2.0;
        [_cardView.btn_left addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_cardView.btn_right addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cardView;
}

- (UILabel *)lb_title
{
    if (!_lb_title) {
        _lb_title = [[UILabel alloc]init];
        _lb_title.text = @"绿色通道:";
        _lb_title.font = [UIFont systemFontOfSize:16 * WIDTHRADIU];
        _lb_title.textColor = [UIColor colorWithHex:0x808080];
        _lb_title.numberOfLines = 0;
    }
    return _lb_title;
}

- (UILabel *)lb_remind
{
    if (!_lb_remind) {
        _lb_remind = [[UILabel alloc]init];
        NSString *text1 = @"1、生病人群（如：卧床不起、重大疾病）等其他特殊人群;\n";
        NSString *text2 = @"2、多次视频认证不通过或不方便使用视频认证用户;\n";
        NSString *text3 = @"3、正视摄像头，注意光线和角度，确保人脸清晰度;\n";
        NSString *text4 = @"4、拍摄两张正面照片;";
        
        NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:10 * WIDTHRADIU];
        
        NSMutableAttributedString *attributedString1 = [[NSMutableAttributedString alloc] initWithString:text1 attributes:@{NSParagraphStyleAttributeName: paragraphStyle}];
        NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString:text2 attributes:@{NSParagraphStyleAttributeName: paragraphStyle}];
        NSMutableAttributedString *attributedString3 = [[NSMutableAttributedString alloc] initWithString:text3 attributes:@{NSParagraphStyleAttributeName: paragraphStyle}];
         NSMutableAttributedString *attributedString4 = [[NSMutableAttributedString alloc] initWithString:text4 attributes:@{NSParagraphStyleAttributeName: paragraphStyle}];
        [attributedString1 appendAttributedString:attributedString2];
        [attributedString1 appendAttributedString:attributedString3];
        [attributedString1 appendAttributedString:attributedString4];
        
        NSRange range = [[[[text1 stringByAppendingString:text2] stringByAppendingString:text3] stringByAppendingString:text4] rangeOfString:[[[text1 stringByAppendingString:text2] stringByAppendingString:text3] stringByAppendingString:text4]];
        [attributedString1 addAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:0x999999],
                                           NSFontAttributeName:[UIFont systemFontOfSize:14 * WIDTHRADIU]} range:range];
        _lb_remind.attributedText = attributedString1;
        _lb_remind.numberOfLines = 0;
    }
    return _lb_remind;
}
- (UILabel *)lb_five
{
    if (!_lb_five) {
        _lb_five = [[UILabel alloc]init];
        _lb_five.text = @"5、有疑问请联系人工客服。";
        _lb_five.font = [UIFont systemFontOfSize:14 * WIDTHRADIU
                         ];
        _lb_five.textColor = [UIColor colorWithHex:0x999999];
    }
    return _lb_five;
}

- (UIButton *)btn_CustomerService
{
    if (!_btn_CustomerService) {
        _btn_CustomerService = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btn_CustomerService setImage:[UIImage imageNamed:@"Greenchannel_customerserviceicon_normal"] forState:UIControlStateNormal];
        [_btn_CustomerService setTitle:@"转人工咨询" forState:UIControlStateNormal];
        _btn_CustomerService.titleLabel.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
        [_btn_CustomerService setTitleColor:K_MainColor forState:UIControlStateNormal];
        [_btn_CustomerService layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleLeft imageTitleSpace:4 * WIDTHRADIU];
        _btn_CustomerService.hidden = YES;
        [_btn_CustomerService addTarget:self action:@selector(kehuchatClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn_CustomerService;
}

- (UIImagePickerController *)imagePicker
{
    if (!_imagePicker) {
        _imagePicker = [[UIImagePickerController alloc]init];
        _imagePicker.delegate = self;
        _imagePicker.allowsEditing = YES;
    }
    
    return _imagePicker;
}

- (NSMutableArray *)imagesArray
{
    if (!_imagesArray) {
        _imagesArray = [[NSMutableArray alloc]initWithCapacity:2];
        
    }
    return _imagesArray;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
