//
//  EmergencyContactPeople.m
//  Open
//
//  Created by 雷俊博 on 2020/3/31.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "EmergencyContactPeople.h"
#import "Constants.h"

@interface EmergencyContactPeople ()
{
    UITextField*_tf_name;
    UITextField*_tf_phone;
}

/** 弹窗主内容view */
@property (nonatomic,strong) UIView   *contentView;

@property (nonatomic, copy)void (^Callback)(NSInteger index,NSString*name,NSString* phone) ;

@end

@implementation EmergencyContactPeople

- (instancetype)initWithCustomWithCallback:(void (^)(NSInteger index,NSString*name,NSString* phone)  )Callback;
{
    self = [super init];
    if (self) {
        [self setupUI];
        self.Callback = Callback;
    }
    return self;
}

#pragma mark - UI搭建
/** UI搭建 */
- (void)setupUI
{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
    
    [self addGestureRecognizer:tap];
    
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc] init];
    self.contentView.bounds = CGRectMake(0, 0, 295*WIDTHRADIU, 352 * WIDTHRADIU);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 15 * WIDTHRADIU;
    
    
    
    //返回按钮
    UIButton* backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setTitle:@"返回" forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"insuredInfo_arrow"] forState:UIControlStateNormal];
    backBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [backBtn addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [backBtn layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleLeft imageTitleSpace:8];
    
    //头像图标
    UIImageView* iconImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"insuredInfo_icon"]];
    
    
    //姓名
    UIImageView* nameIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"insuredInfo_Name"]];
    
    //leftLine
    UIView* nameleftLine = [[UIView alloc]init];
    nameleftLine.backgroundColor = [UIColor colorWithHex:0xcccccc];
    //姓名输入框
    UITextField* tf_name = [[UITextField alloc]init];
    tf_name.borderStyle = UITextBorderStyleNone;
    tf_name.keyboardType = UIKeyboardTypeDefault;
    tf_name.placeholder = @"请输入紧急联系人姓名";
    
    if (@available(iOS 13.0,*)) {
             NSMutableAttributedString *placeholderFont = [[NSMutableAttributedString alloc] initWithString:@"请输入紧急联系人姓名" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16.0 * WIDTHRADIU],NSForegroundColorAttributeName : [UIColor colorWithHex:0xcccccc]}];
            tf_name.attributedPlaceholder = placeholderFont;
     }else{
          tf_name.placeholder = @"请输入紧急联系人姓名";
          [tf_name setValue:[UIFont systemFontOfSize:16.0 * WIDTHRADIU] forKeyPath:@"_placeholderLabel.font"];
         [tf_name setValue:[UIColor colorWithHex:0xcccccc] forKeyPath:@"_placeholderLabel.textColor"];
     }
    //bottomLine
    UIView* nameBottomLine = [[UIView alloc]init];
    nameBottomLine.backgroundColor = [UIColor colorWithHex:0xcccccc];
    
    //电话
    UIImageView* phoneIcon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"+86"]];
    //leftLine
    UIView* phoneleftLine = [[UIView alloc]init];
    phoneleftLine.backgroundColor = [UIColor colorWithHex:0xcccccc];
    //姓名输入框
    UITextField* tf_phone = [[UITextField alloc]init];
    tf_phone.borderStyle = UITextBorderStyleNone;
    tf_phone.keyboardType = UIKeyboardTypeNumberPad;
    tf_phone.placeholder = @"请输入紧急联系人手机号";
    
    if (@available(iOS 13.0,*)) {
             NSMutableAttributedString *placeholderFont = [[NSMutableAttributedString alloc] initWithString:@"请输入紧急联系人手机号" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16.0 * WIDTHRADIU],NSForegroundColorAttributeName : [UIColor colorWithHex:0xcccccc]}];
            tf_phone.attributedPlaceholder = placeholderFont;
     }else{
          tf_phone.placeholder = @"请输入紧急联系人手机号";
          [tf_phone setValue:[UIFont systemFontOfSize:16.0 * WIDTHRADIU] forKeyPath:@"_placeholderLabel.font"];
         [tf_phone setValue:[UIColor colorWithHex:0xcccccc] forKeyPath:@"_placeholderLabel.textColor"];
     }
    
    _tf_name = tf_name;
    _tf_phone = tf_phone;
    
    
    //bottomLine
    UIView* phoneBottomLine = [[UIView alloc]init];
    phoneBottomLine.backgroundColor = [UIColor colorWithHex:0xcccccc];
    
    
    //message
    UILabel* lb_message = [[UILabel alloc]init];
    lb_message.textAlignment = NSTextAlignmentCenter;
    lb_message.numberOfLines = 0;
    
    lb_message.attributedText = [self getAttributedWithString:@"为规范养老金发放秩序，需要您填写紧急联系人，方便我们作为备用方式联系您" WithLineSpace:8 * WIDTHRADIU kern:0.5 font:[UIFont systemFontOfSize:13 * WIDTHRADIU]];
    
    
    
    
    //确定按钮
    UIButton* btn_confirm = [UIButton buttonWithType:UIButtonTypeCustom];
    btn_confirm.backgroundColor = K_MainColor;
    [btn_confirm setTitle:@"确定" forState:UIControlStateNormal];
    [btn_confirm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn_confirm.titleLabel.font = [UIFont systemFontOfSize:16 * WIDTHRADIU];
    btn_confirm.layer.cornerRadius = 20 * WIDTHRADIU;
    [btn_confirm addTarget:self action:@selector(commitClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView* subContentView = [[UIView alloc]initWithFrame:CGRectMake(22.5 * WIDTHRADIU, 120 * WIDTHRADIU, 295*WIDTHRADIU - 45 * WIDTHRADIU, 40 * WIDTHRADIU)];
    
    
    UIView* subContentView2 = [[UIView alloc]initWithFrame:CGRectMake(22.5 * WIDTHRADIU, 180 * WIDTHRADIU, 295*WIDTHRADIU - 45 * WIDTHRADIU, 40 * WIDTHRADIU)];
    
    
    [self.contentView addSubview:backBtn];
    [self.contentView addSubview:iconImg];
    [self.contentView addSubview:subContentView];
    [self.contentView addSubview:subContentView2];
    [self.contentView addSubview:lb_message];
    [self.contentView addSubview:btn_confirm];

    [subContentView addSubview:nameIcon];
    [subContentView addSubview:nameleftLine];
    [subContentView addSubview:tf_name];
    [subContentView addSubview:nameBottomLine];
    
    
    
    
    [subContentView2 addSubview:phoneIcon];
    [subContentView2 addSubview:phoneleftLine];
    [subContentView2 addSubview:tf_phone];
    [subContentView2 addSubview:phoneBottomLine];

    
    
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16 * WIDTHRADIU);
        make.top.offset(12 * WIDTHRADIU);
    }];
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(30 * WIDTHRADIU);
        make.width.height.offset(70 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
    }];
    
    
    [nameIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(subContentView);
        make.left.offset(10 * WIDTHRADIU);
        make.width.offset(19 * WIDTHRADIU);
        make.height.offset(18 * WIDTHRADIU);
    }];

    [nameleftLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(nameIcon);
        make.left.offset(45 * WIDTHRADIU);
        make.height.offset(20 * WIDTHRADIU);
        make.width.offset(0.5);
    }];
    
    [tf_name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(subContentView);
        make.left.equalTo(nameleftLine.mas_right).offset(5);
        make.right.equalTo(subContentView);
        make.height.equalTo(subContentView);
        
    }];
    
    [nameBottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.bottom.offset(- 0.5);
        make.height.offset(0.5);
    }];
    
    
   
    
    [phoneIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(subContentView2);
        make.left.offset(6 * WIDTHRADIU);
        make.width.offset((int)phoneIcon.image.size.width * WIDTHRADIU);
        make.height.offset((int)phoneIcon.image.size.height * WIDTHRADIU);
    }];

    [phoneleftLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(phoneIcon);
        make.left.offset( 45 * WIDTHRADIU);
        make.height.offset(20 * WIDTHRADIU);
        make.width.offset(0.5);
    }];
    
    [tf_phone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(subContentView2);
        make.left.equalTo(phoneleftLine.mas_right).offset(5);
        make.right.equalTo(subContentView2);
        make.height.equalTo(subContentView2);
        
    }];
    
    [phoneBottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.bottom.offset(- 0.5);
        make.height.offset(0.5);
    }];
    
    [lb_message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(subContentView2.mas_bottom).offset(20 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
        make.left.right.equalTo(subContentView);
    }];
    
    [btn_confirm mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(subContentView);
        make.top.equalTo(lb_message.mas_bottom).offset(15 * WIDTHRADIU);
        make.height.offset(40 * WIDTHRADIU);
    }];
    
    
    
    
    
    
    
}

+ (void)showWithViewController:(UIViewController*)viewController Callback:(void (^)(NSInteger index,NSString*name,NSString* phone))Callback
{
    UIWindow* keyWindow = viewController.view.window;
    
    if (keyWindow == nil) {
        keyWindow = [UIApplication sharedApplication].keyWindow;
    }
    
    EmergencyContactPeople *alertView = [[EmergencyContactPeople alloc]initWithCustomWithCallback:Callback];
    
    [alertView show:keyWindow];
}

#pragma mark - event response
- (void)commitClick:(UIButton *)sender
{
    
    if (_tf_name.text.length == 0) {
        [Tools showMessage:@"请输入姓名"];
        return;
    }
    if (_tf_phone.text.length == 0) {
        [Tools showMessage:@"请输入手机号"];
        return;
    }
    
    if (![Tools isMobileNumber:_tf_phone.text]) {
        [Tools showMessage:@"请输入正确的手机号"];
        return;
    }
    
    if ([_tf_phone.text isEqualToString:[getPeopleID PeopleIphoneNmber]]) {
         [Tools showMessage:@"紧急联系电话不能和账户手机号一样"];
         return;
    }
    
    if ([_tf_phone.text isEqualToString:@"18888888888"]) {
        [Tools showMessage:@"请输入正确的手机号"];
        return;
    }
    
     self.Callback(300,_tf_name.text,_tf_phone.text);
    
    [self dismiss];
    
}
- (void)backClick:(UIButton *)sender
{
    self.Callback(200,nil,nil);
    
    [self dismiss];
}

- (void)tapClick:(UITapGestureRecognizer*)ges
{
    [self endEditing:YES];
}

#pragma mark - private methods
-(NSAttributedString *)getAttributedWithString:(NSString *)string WithLineSpace:(CGFloat)lineSpace kern:(CGFloat)kern font:(UIFont *)font{
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    //调整行间距
    paragraphStyle.lineSpacing = lineSpace;
    paragraphStyle.alignment =  NSTextAlignmentCenter;
    NSDictionary *attriDict = @{NSParagraphStyleAttributeName:paragraphStyle,NSKernAttributeName:@(kern),
                                NSFontAttributeName:font,NSForegroundColorAttributeName:[UIColor colorWithHex:0x808080]};
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:string attributes:attriDict];
    return attributedString;
}


#pragma mark - 显示或隐藏
/** 弹出此弹窗 */
- (void)show:(UIWindow *)window {
    // 出场动画
    self.alpha = 0;
    self.contentView.transform = CGAffineTransformScale(self.contentView.transform, 1.3, 1.3);
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 1;
        self.contentView.transform = CGAffineTransformIdentity;
    }];
    
    [window addSubview:self];
}

/** 移除此弹窗 */
- (void)dismiss {
    [self removeFromSuperview];
}





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

@end
