//
//  ChangePasswordController.m
//  AIIM
//
//  Created by qitewei on 2025/4/29.
//

#import "ChangePasswordController.h"
#import "UIViewController+General.h"

#import "Masonry.h"
#import "config.h"
#import "UserNetApi.h"

@interface ChangePasswordController ()<UITextFieldDelegate>
@property (nonatomic, strong) UIStackView * titleStackView;
@property (nonatomic, strong) UILabel * oldPasswordLbl;
@property (nonatomic, strong) UILabel * changePasswordLbl;
@property (nonatomic, strong) UILabel * confirmPasswordLbl;
@property (nonatomic, strong) UILabel * oldPwdAlertLbl;
@property (nonatomic, strong) UILabel * changePwdAlterLbl;
@property (nonatomic, strong) UILabel * confirmPwdAlertLbl;
@property (nonatomic, strong) UITextField * oldPasswordTextfield;
@property (nonatomic, strong) UITextField * changePasswordTextfield;
@property (nonatomic, strong) UITextField * confirmPasswordTextfield;
@property (nonatomic, strong) UIButton * saveBtn;
@end

@implementation ChangePasswordController

#pragma mark lifecircle
- (void)viewDidLoad {
    [super viewDidLoad];
    // 创建一个点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    // 将手势添加到主视图上
    [self.view addGestureRecognizer:tap];
    [self configUI];
}
-(void)dismissKeyboard{
    NSLog(@"dismissKeyboard");
    [self.view endEditing:YES];
}

#pragma mark UI
- (void)configUI{
    
    [self setNavigationTitle:NSLocalizedString(@"userCenter-xiugaimm", @"")];
    // 设置导航栏背景色
    [self setNavigationBarBackgroundColor:UIColor.clearColor];
    
    // 设置标题颜色和字体
    [self setNavigationTitleColor:[UIColor whiteColor] font:[UIFont boldSystemFontOfSize:16]];
    
    // 设置返回按钮
    [self setBackButtonTitle:@""];
    [self setBackButtonColor:[UIColor whiteColor]];

    UIImageView * bgImageView = [[UIImageView alloc] initWithImage:kImageMake(@"loginBG")];
    [self.view addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    
    [self.view addSubview:self.titleStackView];
    [self.titleStackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(180);
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(SCREEN_TOP+48);
        make.width.mas_equalTo(70);
    }];
    
    [self.view addSubview:self.oldPasswordTextfield];
    [self.oldPasswordTextfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleStackView.mas_right).offset(20);
        make.height.mas_equalTo(50);
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(self.oldPasswordLbl.mas_centerY);
    }];
    
    [self.view addSubview:self.oldPwdAlertLbl];
    [self.oldPwdAlertLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(12);
        make.left.mas_equalTo(self.oldPasswordTextfield.mas_left).offset(20);
        make.top.mas_equalTo(self.oldPasswordTextfield.mas_bottom).offset(6);
    }];
    
    [self.view addSubview:self.changePasswordTextfield];
    [self.changePasswordTextfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleStackView.mas_right).offset(20);
        make.height.mas_equalTo(50);
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(self.changePasswordLbl.mas_centerY);
    }];
    
    [self.view addSubview:self.changePwdAlterLbl];
    [self.changePwdAlterLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(12);
        make.left.mas_equalTo(self.changePasswordTextfield.mas_left).offset(20);
        make.top.mas_equalTo(self.changePasswordTextfield.mas_bottom).offset(6);
    }];
    
    [self.view addSubview:self.confirmPasswordTextfield];
    [self.confirmPasswordTextfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleStackView.mas_right).offset(20);
        make.height.mas_equalTo(50);
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(self.confirmPasswordLbl.mas_centerY);
    }];
    
    [self.view addSubview:self.confirmPwdAlertLbl];
    [self.confirmPwdAlertLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(12);
        make.left.mas_equalTo(self.confirmPasswordTextfield.mas_left).offset(20);
        make.top.mas_equalTo(self.confirmPasswordTextfield.mas_bottom).offset(6);
    }];
    
    [self.view addSubview:self.saveBtn];
    [self.saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(46);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(self.confirmPasswordTextfield.mas_bottom).offset(30);
    }];
}

#pragma mark event
- (void)saveButtonClicked{
    if (self.oldPasswordTextfield.text.length == 0) {
        self.oldPwdAlertLbl.hidden = NO;
        self.oldPasswordTextfield.layer.borderColor = UIColor.redColor.CGColor;
        return;
    }
    
    if (self.changePasswordTextfield.text.length == 0) {
        self.changePwdAlterLbl.hidden = NO;
        self.changePasswordTextfield.layer.borderColor = UIColor.redColor.CGColor;
        return;
    }
    
    if (self.confirmPasswordTextfield.text.length == 0) {
        self.confirmPwdAlertLbl.hidden = NO;
        self.confirmPasswordTextfield.layer.borderColor = UIColor.redColor.CGColor;
        return;
    }
    
    if (![self.confirmPasswordTextfield.text isEqualToString:self.changePasswordTextfield.text]) {
        self.confirmPwdAlertLbl.text = NSLocalizedString(@"PwdChange_passwordsDifferent_alter", @"");
        self.confirmPwdAlertLbl.hidden = NO;
        self.confirmPasswordTextfield.layer.borderColor = UIColor.redColor.CGColor;
        return;
    }
    
    NSDictionary * param = @{@"oldPassword":self.oldPasswordTextfield.text,
                             @"newPassword":self.confirmPasswordTextfield.text};
    
    [UserNetApi changePassword:param succ:^(int code, NSDictionary * _Nullable res) {
        NSLog(@"result:%@",res);
        if ([res jk_hasKey:@"msg"]) {
            [MBProgressHUD showWithText:[res objectForKey:@"msg"]];
        }
    } fail:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
//        [MBProgressHUD showWithText:@"修改失败"];
    }];
}

#pragma mark textfield delegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField == self.oldPasswordTextfield && self.oldPasswordTextfield.text.length != 0) {
        self.oldPwdAlertLbl.hidden = YES;
        self.oldPasswordTextfield.layer.borderColor = UIColor.whiteColor.CGColor;
    }
    
    if (textField == self.changePasswordTextfield && self.changePasswordTextfield.text.length != 0) {
        self.changePwdAlterLbl.hidden = YES;
        self.changePasswordTextfield.layer.borderColor = UIColor.whiteColor.CGColor;
    }
    
    if (textField == self.confirmPasswordTextfield && self.confirmPasswordTextfield.text.length != 0) {
        self.confirmPwdAlertLbl.hidden = YES;
        self.confirmPasswordTextfield.layer.borderColor = UIColor.whiteColor.CGColor;
    }
    
    if ([self.confirmPasswordTextfield.text isEqualToString:self.changePasswordTextfield.text]) {
        self.confirmPwdAlertLbl.hidden = YES;
        self.confirmPasswordTextfield.layer.borderColor = UIColor.whiteColor.CGColor;
    }
}

#pragma mark lazy
- (UIStackView *)titleStackView{
    if (!_titleStackView) {
        _titleStackView = [[UIStackView alloc] initWithArrangedSubviews:@[self.oldPasswordLbl,self.changePasswordLbl,self.confirmPasswordLbl]];
        _titleStackView.axis = UILayoutConstraintAxisVertical;
        _titleStackView.alignment = UIStackViewAlignmentFill;
        _titleStackView.distribution = UIStackViewDistributionEqualSpacing;
        _titleStackView.spacing = 64.f;
    }
    return _titleStackView;
}

- (UILabel *)oldPasswordLbl{
    if (!_oldPasswordLbl) {
        _oldPasswordLbl = [[UILabel alloc] init];
        _oldPasswordLbl.text = NSLocalizedString(@"PwdChange_oldPassword", @"");
        _oldPasswordLbl.textColor = UIColor.whiteColor;
        _oldPasswordLbl.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
    }
    return _oldPasswordLbl;
}

- (UILabel *)changePasswordLbl{
    if (!_changePasswordLbl) {
        _changePasswordLbl = [[UILabel alloc] init];
        _changePasswordLbl.text = NSLocalizedString(@"PwdChange_newPassword", @"");
        _changePasswordLbl.textColor = UIColor.whiteColor;
        _changePasswordLbl.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
    }
    return _changePasswordLbl;
}

- (UILabel *)confirmPasswordLbl{
    if (!_confirmPasswordLbl) {
        _confirmPasswordLbl = [[UILabel alloc] init];
        _confirmPasswordLbl.text = NSLocalizedString(@"PwdChange_confirmPassword", @"");
        _confirmPasswordLbl.textColor = UIColor.whiteColor;
        _confirmPasswordLbl.font = [UIFont systemFontOfSize:16 weight:UIFontWeightRegular];
    }
    return _confirmPasswordLbl;
}

- (UILabel *)oldPwdAlertLbl{
    if (!_oldPwdAlertLbl) {
        _oldPwdAlertLbl = [[UILabel alloc] init];
        _oldPwdAlertLbl.text = NSLocalizedString(@"PwdChange_oldPwd_alter", @"");
        _oldPwdAlertLbl.textColor = globalColor(GCTypeRedd);
        _oldPwdAlertLbl.font = SYSFONT(12);
        _oldPwdAlertLbl.hidden = YES;
    }
    return _oldPwdAlertLbl;
}

- (UILabel *)changePwdAlterLbl{
    if (!_changePwdAlterLbl) {
        _changePwdAlterLbl = [[UILabel alloc] init];
        _changePwdAlterLbl.text = NSLocalizedString(@"PwdChange_newPwd_alter", @"");
        _changePwdAlterLbl.textColor = globalColor(GCTypeRedd);
        _changePwdAlterLbl.font = SYSFONT(12);
        _changePwdAlterLbl.hidden = YES;
    }
    return _changePwdAlterLbl;
}

- (UILabel *)confirmPwdAlertLbl{
    if (!_confirmPwdAlertLbl) {
        _confirmPwdAlertLbl = [[UILabel alloc] init];
        _confirmPwdAlertLbl.text = NSLocalizedString(@"PwdChange_confirmPwd_alter", @"");
        _confirmPwdAlertLbl.textColor = globalColor(GCTypeRedd);
        _confirmPwdAlertLbl.font = SYSFONT(12);
        _confirmPwdAlertLbl.hidden = YES;
    }
    return _confirmPwdAlertLbl;
}

- (UITextField *)oldPasswordTextfield{
    if (!_oldPasswordTextfield) {
        _oldPasswordTextfield = [[UITextField alloc] init];
        _oldPasswordTextfield.textColor = UIColor.whiteColor;
        _oldPasswordTextfield.borderStyle = UITextBorderStyleNone;
        _oldPasswordTextfield.layer.borderWidth = 1.f;
        _oldPasswordTextfield.layer.borderColor = globalColor(GCTypeDark2).CGColor;
        _oldPasswordTextfield.layer.cornerRadius = 5.f;
        _oldPasswordTextfield.secureTextEntry = YES;
        _oldPasswordTextfield.delegate = self;
        _oldPasswordTextfield.leftViewMode = UITextFieldViewModeAlways;
        UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 50)];
        _oldPasswordTextfield.leftView = leftView;
    }
    return _oldPasswordTextfield;
}

- (UITextField *)changePasswordTextfield{
    if (!_changePasswordTextfield) {
        _changePasswordTextfield = [[UITextField alloc] init];
        _changePasswordTextfield.textColor = UIColor.whiteColor;
        _changePasswordTextfield.borderStyle = UITextBorderStyleNone;
        _changePasswordTextfield.layer.borderWidth = 1.f;
        _changePasswordTextfield.layer.borderColor = globalColor(GCTypeDark2).CGColor;
        _changePasswordTextfield.layer.cornerRadius = 5.f;
        _changePasswordTextfield.secureTextEntry = YES;
        _changePasswordTextfield.delegate = self;
        _changePasswordTextfield.leftViewMode = UITextFieldViewModeAlways;
        UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 50)];
        _changePasswordTextfield.leftView = leftView;
    }
    return _changePasswordTextfield;
}

- (UITextField *)confirmPasswordTextfield{
    if (!_confirmPasswordTextfield) {
        _confirmPasswordTextfield = [[UITextField alloc] init];
        _confirmPasswordTextfield.textColor = UIColor.whiteColor;
        _confirmPasswordTextfield.borderStyle = UITextBorderStyleNone;
        _confirmPasswordTextfield.layer.borderWidth = 1.f;
        _confirmPasswordTextfield.layer.borderColor = globalColor(GCTypeDark2).CGColor;
        _confirmPasswordTextfield.layer.cornerRadius = 5.f;
        _confirmPasswordTextfield.secureTextEntry = YES;
        _confirmPasswordTextfield.delegate = self;
        _confirmPasswordTextfield.leftViewMode = UITextFieldViewModeAlways;
        UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 50)];
        _confirmPasswordTextfield.leftView = leftView;
    }
    return _confirmPasswordTextfield;
}

- (UIButton *)saveBtn{
    if (!_saveBtn) {
        _saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_saveBtn setTitle:NSLocalizedString(@"EGroupCtr-baocun", @"") forState:UIControlStateNormal];
        [_saveBtn setBackgroundColor:globalColor(GCTypeGreen)];
        [_saveBtn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        _saveBtn.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        _saveBtn.layer.cornerRadius = 5.f;
        _saveBtn.layer.masksToBounds = YES;
        [_saveBtn addTarget:self action:@selector(saveButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}

#pragma mark statusBar
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

@end
