//
//  SLNearbyPeopleVc.m
//  SLAiELTS
//
//  Created by Gusont on 2023/5/10.
//

#import "SLNearbyPeopleVc.h"
#import "SLNearbyPeopleCell.h"
#import "SLLocationManager.h"
#import "SLFriensInfoVc.h"

@interface SLNearbyPeopleVc ()<UITableViewDelegate, UITableViewDataSource>

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

@property (nonatomic, strong)NSMutableArray *nearbyPeoples;

@property (nonatomic, strong)CLLocation *currentLocation;

@property (nonatomic, strong)NSString *selSex;
@end

@implementation SLNearbyPeopleVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"附近的人";
    self.selSex = @"";
    [self.tableView registerNibCellWithReuseIdentifierClass:SLNearbyPeopleCell.class];
    UIImage *creatImage =[UIImage imageNamed:@"icon_pyq_more_back"];
    YMBarButtonItem *rightBarItem =[[YMBarButtonItem alloc]initWithImage:creatImage style:YMBarButtonItemPlain target:self action:@selector(moreBtnClick)];
    rightBarItem.titleInsets = UIEdgeInsetsMake(0, 0, 0, 20);
    self.navigationBar.rightBarButtonItem = rightBarItem;
    WS(weakSelf);
    [[SLLocationManager shareInstance] getLocationComplete:^(CLLocation * _Nullable currentLocation) {
        NSLog(@"%f--%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
        if (currentLocation) {
            weakSelf.currentLocation = currentLocation;
            [weakSelf requestData];
        }else {
            [ZFToast ShowWithMessage:@"定位失败，请检查权限设置"];
        }
    }];
}

- (void)requestData {
    WS(weakSelf);
    [self.tableView tableviewRefresh:self.nearbyPeoples TableViewRefresh:^(NSInteger pageNo, TableListCount listCount) {
        NSDictionary *parameter = @{@"PageIndex": @(pageNo),
                                    @"PageSize": @(SLPageSize),
                                    @"province" : [SLLocationManager shareInstance].province ,
                                    @"sex" : self.selSex,
                                    @"city" : [SLLocationManager shareInstance].city ,
                                    @"lon" : @(self.currentLocation.coordinate.longitude) ,
                                    @"lat" : @(self.currentLocation.coordinate.latitude)
        };
        [[SLHttpCenter SharedInstance] getWithUrl:@"/api/Contacts/PeopleNearby" parameter:parameter success:^(id responseObject) {
            NSDictionary *itemDict = [responseObject objectForKey:@"data"];
            NSArray *datas = [itemDict objectForKey:@"items"];
            if (datas.count) {
                listCount([datas count]);
                NSArray *friendsModel = [SLFriendsModel mj_objectArrayWithKeyValuesArray:datas];
                [weakSelf.nearbyPeoples addObjectsFromArray:friendsModel];
                [weakSelf.tableView reloadData];
            }else {
                listCount(-1);
            }
        } failure:^(SPRequestError *error) {
            listCount(-1);
        }];
    }];
}

- (void)moreBtnClick {
    WS(weakSelf);
    [SLCustomizeAlert showBottomAletrWithTitles:@[@"全部", @"只看男生", @"只看女生"] cancelTitle:@"取消" buttonIndex:^(NSInteger index) {
        switch (index) {
            case 0:
                weakSelf.selSex = @"";
                [weakSelf.tableView.mj_header beginRefreshing];
                break;
            case 1:
                weakSelf.selSex = @"男";
                [weakSelf.tableView.mj_header beginRefreshing];
                break;
            case 2:
                weakSelf.selSex = @"女";
                [weakSelf.tableView.mj_header beginRefreshing];
                break;
            default:
                break;
        }
    }];
}

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SLNearbyPeopleCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(SLNearbyPeopleCell.class)];
    SLFriendsModel *model = [self.nearbyPeoples objectAtIndex:indexPath.row];
    [cell confignCellWith:model];
    return cell;
}

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SLFriendsModel *model = [self.nearbyPeoples objectAtIndex:indexPath.row];
    SLFriensInfoVc *vc = [SLFriensInfoVc loadViewControllewWithNib];
    vc.friendModel = model;
    [self navPushViewController:vc animated:YES];
}

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

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

- (NSMutableArray *)nearbyPeoples {
    if (!_nearbyPeoples) {
        _nearbyPeoples = [NSMutableArray array];
    }
    return _nearbyPeoples;
}

@end
