//
//  SLAddFriendVc.m
//  SLAiELTS
//
//  Created by Gusont on 2023/4/18.
//

#import "SLAddFriendVc.h"
#import "LeftImageDesignTxtxField.h"
#import "AddressBookCell.h"

@interface SLAddFriendVc ()<UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet LeftImageDesignTxtxField *textField;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topViewConst;

@property (nonatomic, strong) NSMutableArray<SLUserModel *> *userModels;
@end

@implementation SLAddFriendVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.tableView registerNibCellWithReuseIdentifierClass:AddressBookCell.class];
    self.topViewConst.constant = kNavBarHeight;
    self.navigationBar.separatorHeight = 0.0f;
    self.title = @"添加朋友";
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    [self.userModels removeAllObjects];
    if (textField.text.length) {
        [self requetsSearchUser:textField.text];
    }else {
        [self.tableView reloadData];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField endEditing:YES];
    return YES;
}

- (void)requetsSearchUser:(NSString *)user {
    [[SLHttpCenter SharedInstance] getWithUrl:@"/api/Contacts/SearchUser" parameter:@{@"userName" : user} success:^(id responseObject) {
        NSArray *arr = [responseObject objectForKey:@"data"];
        NSArray<SLUserModel *> *userModels = [SLUserModel mj_objectArrayWithKeyValuesArray:arr];
        [self.userModels addObjectsFromArray:userModels];
        [self.tableView reloadData];
    } failure:^(SPRequestError *error) {
        
    }];
}

- (NSMutableArray<SLUserModel *> *)userModels {
    if (!_userModels) {
        _userModels = [NSMutableArray new];
    }
    return _userModels;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    AddressBookCell *cell = (AddressBookCell *)[tableView dequeueReusableCellWithIdentifier:@"AddressBookCell"];
    SLUserModel *userModel = [self.userModels objectAtIndex:indexPath.row];
    [cell confignCellWith:userModel];
    return cell;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.userModels.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SLUserModel *userModel = [self.userModels objectAtIndex:indexPath.row];
    NSString *msg = [NSString stringWithFormat:@"是否添加用户：%@ 为好友", userModel.userName];
    WS(weakSelf);
    [SLCustomizeAlert showAletrWithTitle:@"提示" message:msg sureBtnTitle:@"确定" cancelBtnTitle:@"取消" sureBtnAction:^{
        [[SLHttpCenter SharedInstance] getWithUrl:@"/api/Contacts/AddContacts" parameter:@{@"userId" : userModel.userId} success:^(id responseObject) {
            [ZFToast ShowWithMessage:@"添加成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            });
        } failure:^(SPRequestError *error) {

        }];
    } cancelBtnAction:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 72.0f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 12.01;
}

@end
