//
//  KKUserAuthSuccessViewController.m
//  Open
//
//  Created by 雷俊博 on 2021/1/21.
//  Copyright © 2021 黄江涛 . All rights reserved.
//

#import "KKUserAuthSuccessViewController.h"
#import "DDSBCardViewController.h"
#import "KKUserIdentifyViewController.h"
#import "DistrictAuthenticationPersonalInfoController.h"
#import "KKAuthSuccessCell.h"
#import "KKUserAuthViewController.h"
#import "KKUserImageViewController.h"

@interface KKUserAuthSuccessViewController ()<UIGestureRecognizerDelegate,KKUserImageDelegate>

@property(nonatomic,strong)UILabel *infoState;//个人信息状态
@property(nonatomic,strong)UILabel *photoState;//证件图片状态
@property(nonatomic,strong)UILabel *faceState;//人脸验证状态

@end

@implementation KKUserAuthSuccessViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    
    //禁止手势返回
    id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
    [self.view addGestureRecognizer:pan];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self configNav];
    
    [self setUpUI];
    
}

- (void)configNav{
   UIView*nav = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, cl_kNavigationBarHeight)];
    nav.backgroundColor = [UIColor colorWithHex:0x086ed7];
    [self.view addSubview:nav];
    
    UIButton*backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setTitle:@"返回" forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backBtn layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleLeft imageTitleSpace:4];
    [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [nav addSubview:backBtn];
    
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.top.offset(cl_kStatusBarHeight);
        make.width.offset(80);
        make.height.offset(44);
    }];
    
    UILabel*titleLabel = [[UILabel alloc]init];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = self.title;
    titleLabel.font = [UIFont boldSystemFontOfSize:17];
    [nav addSubview:titleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.offset(cl_kStatusBarHeight);
        make.width.offset(80);
        make.height.offset(44);
    }];
    
    UIButton *RightBtn = [[UIButton alloc]init];
    [RightBtn setTitle:@" 客服" forState:UIControlStateNormal];
    [RightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [RightBtn setImage:[UIImage imageNamed:@"客服"] forState:UIControlStateNormal];
    [RightBtn setImage:[UIImage imageNamed:@"客服"] forState:UIControlStateHighlighted];
    RightBtn.titleLabel.font = [UIFont systemFontOfSize:15*WIDTHRADIU];
    RightBtn.imageEdgeInsets = UIEdgeInsetsMake(7.5, 0, 7.5, 0);
    [RightBtn addTarget:self action:@selector(kefu) forControlEvents:UIControlEventTouchUpInside];
    [nav addSubview:RightBtn];
    [RightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-15);
        make.centerY.equalTo(titleLabel);
        make.width.offset(60);
        make.height.offset(30);
    }];
    
}

//客服
- (void)kefu{
    [[clickEvent instance] clickEventRooterVC:self Content:@"weChatService" menuName:@"weChatService"];
}

- (void)setUpUI {
    
    UIView *topGroup = [[UIView alloc]initWithFrame:CGRectMake(0, cl_kNavigationBarHeight, ScreenWidth-30*WIDTHRADIU, 130*WIDTHRADIU)];
    topGroup.layer.cornerRadius=7*WIDTHRADIU;
    topGroup.layer.masksToBounds = YES;
    topGroup.backgroundColor=[UIColor colorWithHex:0x086ed7];
    //[[SetGradient instance] setViewBackground:topGroup fromColor:@"#0e76d4" toColor:@"#2591f3" fromCGPoint:CGPointMake(0, 0) toCGPoint:CGPointMake(0, 1)];
    [self.view addSubview:topGroup];
    [topGroup mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.offset(cl_kNavigationBarHeight+20*WIDTHRADIU);
        make.width.offset(ScreenWidth-30*WIDTHRADIU);
        make.height.offset(130*WIDTHRADIU);
    }];
    
    UIImageView* headImage = [UIImageView new];
    headImage.contentMode = UIViewContentModeScaleAspectFill;
    headImage.layer.cornerRadius = 30 * WIDTHRADIU;
    headImage.layer.masksToBounds = YES;
    headImage.backgroundColor = [UIColor clearColor];
    [topGroup addSubview:headImage];
    [headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20 * WIDTHRADIU);
        make.top.offset(20*WIDTHRADIU);
        make.width.height.offset(60 * WIDTHRADIU);
    }];
    NSURL *photoUrl = [getPeopleID PeoplePhoto];
    if (photoUrl) {
        [headImage sd_setImageWithURL:photoUrl placeholderImage:[UIImage imageNamed:@"my_information_headportrait_default"]];
    }else{
        headImage.image = [UIImage imageNamed:@"my_information_headportrait_default"];
    }
    
    //姓名
    UILabel* realName = [UILabel new];
    if (self.nameTT) {
        NSString *name=self.nameTT;
        realName.text = name;
    }
    realName.textColor = [UIColor colorWithHex:0xFFFFFF];
    realName.font = [UIFont boldSystemFontOfSize:16 * WIDTHRADIU];
    [topGroup addSubview:realName];
    [realName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headImage.mas_right).offset(15 * WIDTHRADIU);
        make.top.equalTo(headImage);
    }];
    
    //证件号
    UILabel* realId = [UILabel new];
    if (self.numTT) {
        realId.text = self.numTT;
    }
    realId.textColor = [UIColor colorWithHex:0xFFFFFF];
    realId.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
    [topGroup addSubview:realId];
    [realId mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(realName.mas_bottom).offset(20*WIDTHRADIU);
        //make.bottom.equalTo(headImage.mas_bottom);
        make.left.equalTo(realName);
    }];
    
    UILabel* personTip = [UILabel new];
    personTip.text = @"个人隐私信息安全保障中";
    personTip.textColor = [UIColor colorWithHex:0xFFFFFF];
    personTip.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    [topGroup addSubview:personTip];
    [personTip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headImage.mas_bottom).offset(15 * WIDTHRADIU);
        make.left.offset(20*WIDTHRADIU);
    }];
    
    UIImageView* rightImage = [UIImageView new];
    rightImage.image=[UIImage imageNamed:@"real_name_preview_right"];
    //rightImage.contentMode = UIViewContentModeScaleAspectFill;
    rightImage.backgroundColor = [UIColor clearColor];
    rightImage.contentMode=UIViewContentModeRight;
    [topGroup addSubview:rightImage];
    [rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-10 * WIDTHRADIU);
        make.centerY.equalTo(topGroup);
        make.width.height.offset(40 * WIDTHRADIU);
    }];
    
    
    //个人信息
    KKAuthSuccessCell* firstCell = [self getCell];
    firstCell.lb_title.text = @"个人信息";
    firstCell.lb_des.text=@"完善身份信息,体验更多服务";
    firstCell.lb_content.text = @"未完善 >";
    self.infoState=firstCell.lb_content;
    [self.view addSubview:firstCell];
    [self.view layoutIfNeeded];
    firstCell.frame = CGRectMake(0, CGRectGetMaxY(topGroup.frame) + 43 * WIDTHRADIU, ScreenWidth, 55 * WIDTHRADIU);
    
    //证件照片
    KKAuthSuccessCell* secondCell = [self getCell];
    secondCell.lb_title.text = @"证件照片";
    secondCell.lb_des.text=@"上传证件照片,规范补贴规范";
    secondCell.lb_content.text = @"未上传 >";
    self.photoState=secondCell.lb_content;
    [self.view addSubview:secondCell];
    secondCell.frame = CGRectMake(0, CGRectGetMaxY(firstCell.frame), ScreenWidth, 55 * WIDTHRADIU);
    
    //人像照片
    KKAuthSuccessCell* thirdCell = [self getCell];
    thirdCell.lb_title.text = @"人像照片";
    thirdCell.lb_des.text=@"体验刷脸科技,享受便民服务";
    thirdCell.lb_content.text = @"未验证 >";
    self.faceState=thirdCell.lb_content;
    [self.view addSubview:thirdCell];
    thirdCell.frame = CGRectMake(0, CGRectGetMaxY(secondCell.frame), ScreenWidth, 55 * WIDTHRADIU);
    
    [self realNameInfoFlesh];
    UIButton*confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    confirmBtn.backgroundColor = K_MainColor;
    switch (self.type) {
        case KKUserAuthFromVCTypeDDSBCard:
            [confirmBtn setTitle:@"电子社保卡" forState:UIControlStateNormal];
            break;
        case KKUserAuthFromVCTypeSBAuth:
            [confirmBtn setTitle:@"社保认证" forState:UIControlStateNormal];
            break;
        case KKUserAuthFromVCTypeYouFu:
            [confirmBtn setTitle:@"优抚核查" forState:UIControlStateNormal];
            break;
        case KKUserAuthFromVCTypeBuTie:
            [confirmBtn setTitle:@"补贴认证" forState:UIControlStateNormal];
            break;
        case KKUserAuthFromVCTypeAccount:
            [confirmBtn setTitle:@"社保认证" forState:UIControlStateNormal];
            break;
            
        default:
            break;
    }
    
    [confirmBtn addTarget:self action:@selector(confirmBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.offset(45 * WIDTHRADIU);
    }];
}

//实名状态刷新
- (void)realNameInfoFlesh{
    //查询当前实名信息
    NSLog(@"查询当前实名信息");
    NSDictionary *params = @{
        @"tel":[getPeopleID PeopleIphoneNmber]
    };
    NSString *requset=[NSString stringWithFormat:@"%@%@",CSL_url,@"/auth/query/extra/info"];
    [NetworkRequest Post15:requset Params:params Success:^(id _Nonnull obj) {
        NSLog(@"实名信息:%@",obj);
        NSString *state=[NSString stringWithFormat:@"%@",obj[@"state"]];
        if([state isEqualToString:@"200"]){
            NSDictionary *result=obj[@"result"];
            NSString *address=result[@"address"];
            NSString *vaildPeriod=result[@"vaildPeriod"];
            NSString *professional=result[@"professional"];
            if(address==nil||vaildPeriod==nil||professional==nil||[address isEqual:[NSNull null]]||[vaildPeriod isEqual:[NSNull null]]||[professional isEqual:[NSNull null]]){
                //个人信息未完善
                self.infoState.text = @"未完善 >";
            }else{
                self.infoState.text = @"已完善 >";
                self.infoState.textColor=[UIColor colorWithHex:0x8c8c8c];
            }
            //个人信息点击;
            self.infoState.userInteractionEnabled=YES;
            UITapGestureRecognizer *tapText01 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(infoClick:)];
            [self.infoState addGestureRecognizer:tapText01];
            
            NSString *idCardPositive=result[@"idCardPositive"];
            NSString *idCardReverse=result[@"idCardReverse"];
            if(idCardPositive==nil||idCardReverse==nil||[idCardPositive isEqual:[NSNull null]]||[idCardReverse isEqual:[NSNull null]]){
                //证件照还没上传
                self.photoState.text = @"未上传 >";
            }else{
                self.photoState.text = @"已上传 >";
                self.photoState.textColor=[UIColor colorWithHex:0x8c8c8c];
            }
            //上传点击
            self.photoState.userInteractionEnabled=YES;
            UITapGestureRecognizer *tapText = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClick:)];
            [self.photoState addGestureRecognizer:tapText];
            
            self.faceState.text = @"已验证 >";
            self.faceState.textColor=[UIColor colorWithHex:0x8c8c8c];
        }
        
    } Failure:^(NSError *error) {
        NSLog(@"网络错误:%@",error);
    }];
    
    
}

//代理刷新回调;
- (void)KKUserCompleteUp{
    NSLog(@"刷新当前实名信息");
    [self realNameInfoFlesh];
}
//个人信息
-(void)infoClick:(UITapGestureRecognizer *)tap{
    NSLog(@"个人信息....");
    [[clickEvent instance] clickEventRooterVC:self Content:@"minePersonalInfo" menuName:@"minePersonalInfo"];
}

//上传图片
-(void)imageClick:(UITapGestureRecognizer *)tap{
    NSLog(@"上传图片....");
    KKUserImageViewController *vc=[[KKUserImageViewController alloc]init];
    vc.title=@"图片上传";
    vc.delegate=self;
    [self.navigationController pushViewController:vc animated:NO];
}


#pragma mark - event response
- (void)backClick {
    
    if (self.type == KKUserAuthFromVCTypeDDSBCard) {
        
        for (UIViewController* vc in self.navigationController.viewControllers) {
            if ([vc isKindOfClass:[DDSBCardViewController class]]) {
                [self.navigationController popToViewController:vc animated:YES];
                break;
            }
        }
    }else if (self.type == KKUserAuthFromVCTypeSBAuth) {
        
        
        for (UIViewController* vc in self.navigationController.viewControllers) {
            if ([vc isKindOfClass:[KKUserIdentifyViewController class]]) {
                [self.navigationController popToViewController:vc animated:YES];
                break;
            }
        }
        
        
        
    }else if (self.type == KKUserAuthFromVCTypeYouFu || self.type == KKUserAuthFromVCTypeBuTie) {
        
        for (UIViewController* vc in self.navigationController.viewControllers) {
            if ([vc isKindOfClass:[DistrictAuthenticationPersonalInfoController class]]) {
                [self.navigationController popToViewController:vc animated:YES];
                break;
            }
        }
    }else{
        
        if ([self.from isEqualToString:@"personInfo"]) {
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        
        
    }
}

- (void)confirmBtnClick:(UIButton *)sender {
    
    if (self.type == KKUserAuthFromVCTypeDDSBCard) {
        
        for (UIViewController* vc in self.navigationController.viewControllers) {
            if ([vc isKindOfClass:[DDSBCardViewController class]]) {
                [self.navigationController popToViewController:vc animated:YES];
                break;
            }
        }
    }else if (self.type == KKUserAuthFromVCTypeSBAuth) {
        
        
        for (UIViewController* vc in self.navigationController.viewControllers) {
            if ([vc isKindOfClass:[KKUserIdentifyViewController class]]) {
                [self.navigationController popToViewController:vc animated:YES];
                break;
            }
        }
        
        
        
    }else if (self.type == KKUserAuthFromVCTypeYouFu || self.type == KKUserAuthFromVCTypeBuTie) {
        
        for (UIViewController* vc in self.navigationController.viewControllers) {
            if ([vc isKindOfClass:[DistrictAuthenticationPersonalInfoController class]]) {
                [self.navigationController popToViewController:vc animated:YES];
                break;
            }
        }
    }else if (self.type == KKUserAuthFromVCTypeAccount) {
        
        
        [self kkJumpUserIdentifyViewController];
    }
    
}


- (void)editBtnClick:(UIButton *)sender {
    
    KKUserAuthViewController* vc = [KKUserAuthViewController new];
    vc.title = @"实名认证";
    vc.nameTT = self.nameTT;
    vc.numTT = self.numTT;
    vc.type = KKUserAuthFromVCTypeUpdate;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark - 社保认证跳转
- (void)kkJumpUserIdentifyViewController {
    KKUserIdentifyViewController*vc = [[KKUserIdentifyViewController alloc]init];
    vc.title = @"社保认证";
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 获取到cell
- (KKAuthSuccessCell *)getCell {
    KKAuthSuccessCell* cell = [[NSBundle mainBundle]loadNibNamed:@"KKAuthSuccessCell" owner:self options:nil].lastObject;
    
    return cell;
}

/*
#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
