//
//  SLModifyNicknameVc.m
//  SLAiELTS
//
//  Created by Gusont on 2023/3/10.
//

#import "SLModifyNicknameVc.h"
#import "SLChangeMobileVc.h"

@interface SLModifyNicknameVc ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *nickNameTextField;
@property (weak, nonatomic) IBOutlet UIButton *clearBtn;

@end

@implementation SLModifyNicknameVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"修改昵称";
    YMBarButtonItem *rightBarItem = [[YMBarButtonItem alloc] initWithTitle:@"保存  " target:self action:@selector(preserveBtnClick)];
    rightBarItem.titleInsets = UIEdgeInsetsMake(0, 0, 0, 20);
    self.navigationBar.rightBarButtonItem = rightBarItem;
    self.nickNameTextField.delegate = self;
    self.clearBtn.hidden = YES;
}

- (IBAction)clearBtnClick:(id)sender {
    self.nickNameTextField.text = @"";
    self.clearBtn.hidden = YES;
}

- (void)preserveBtnClick {
    [self.view endEditing:YES];
    if (!self.nickNameTextField.text.length) {
        [ZFToast ShowWithMessage:@"请输入昵称"];
        return;
    }
    [[SLHttpCenter SharedInstance] getWithUrl:@"/api/User/UpdateName" parameter:@{@"name" : self.nickNameTextField.text} success:^(id responseObject) {
        [ZFToast ShowWithMessage:@"保存成功"];
        SLLoginInfo *newLoginModel = [SLGlobalInfo SharedInstance].loginInfo;
        newLoginModel.user.userName = self.nickNameTextField.text;
        [SLGlobalInfo SharedInstance].loginInfo = newLoginModel;
    } failure:^(SPRequestError *error) {
        
    }];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    self.clearBtn.hidden = !toBeString.length;
    return YES;
}


@end
