//
//  SLUserCenterViewController.m
//  SLAiELTS
//
//  Created by Gusont on 2023/3/9.
//

#import "SLUserCenterViewController.h"
#import "SLBaseTableViewCell.h"
#import "SLModifyNicknameVc.h"
#import "SLChangeMobileVc.h"
#import "SLImagePickerAndUpload.h"

@interface SLUserCenterViewController ()<UITableViewDelegate, UITableViewDataSource>

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

@property (nonatomic, strong) NSMutableArray<NSArray<SLPageModelDetail *> *> *dataModels;

@property (nonatomic, strong) SLImagePickerAndUpload *imageUpload;
@end

@implementation SLUserCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"个人中心";
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 100;
    [self.tableView registerNibCellWithReuseIdentifierClass:SLBaseTableViewCell.class];
    self.tableView.backgroundColor = [UIColor colorFormString:@"#F2F3F7"];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear: animated];
    _dataModels = nil;
    [self.tableView reloadData];
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    SLBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SLBaseTableViewCell"];
    SLPageModelDetail *detail = [[self.dataModels objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    [cell confignCell:detail];
    return cell;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataModels objectAtIndex:section].count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataModels.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SLPageModelDetail *detail = [[self.dataModels objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    if ([detail.title isEqualToString:@"头像"]) {
        [self.imageUpload willShowActionSheet];
    }
    if ([detail.title isEqualToString:@"昵称"]) {
        SLModifyNicknameVc *vc = [SLModifyNicknameVc loadViewControllewWithNib];
        [self navPushViewController:vc animated:YES];
    }
    if ([detail.title isEqualToString:@"手机号"]) {
        SLChangeMobileVc *vc = [SLChangeMobileVc loadViewControllewWithNib];
        [self navPushViewController:vc animated:YES];
    }
    if ([detail.title isEqualToString:@"退出登录"]) {
        [SLCustomizeAlert showAletrWithTitle:@"退出确认" message:@"是否确认退出登录？" sureBtnTitle:@"确定" cancelBtnTitle:@"取消" sureBtnAction:^{
            [[SLGlobalInfo SharedInstance] userLogout];
            AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
            [appDelegate setLoginVcWithRootWindow];
        } cancelBtnAction:^{
            
        }];
    }
    if ([detail.title isEqualToString:@"用户服务协议"]) {
        NSString *urlStr = @"http://115.238.47.234:8446/index.html";
        SLWebViewController *vc = [SLWebViewController loadViewControllewWithNib];
        vc.webUrlStr = urlStr;
        vc.title = detail.title;
        [self navPushViewController:vc animated:YES];
    }
    if ([detail.title isEqualToString:@"免责声明"]) {
        NSString *urlStr = @"http://115.238.47.234:8446/mzsm.html";
        SLWebViewController *vc = [SLWebViewController loadViewControllewWithNib];
        vc.webUrlStr = urlStr;
        vc.title = detail.title;
        [self navPushViewController:vc animated:YES];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 68.f;
    }
    return 50.f;
}

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

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 20)];
    view.backgroundColor = [UIColor colorFormString:@"#F2F3F7"];
    return view;
}

- (NSMutableArray<NSArray<SLPageModelDetail *> *> *)dataModels {
    if (!_dataModels) {
        SLUserModel *user = [SLGlobalInfo SharedInstance].loginInfo.user;
        NSArray *arr = @[
        @[[SLPageModelDetail initWithTitle:@"头像" subTitle:@"" bgImage:@""],
          [SLPageModelDetail initWithTitle:@"昵称" subTitle:user.userName bgImage:@""],
          [SLPageModelDetail initWithTitle:@"手机号" subTitle:user.phone bgImage:@""],
          [SLPageModelDetail initWithTitle:@"DLid" subTitle:user.userName bgImage:@""],
          [SLPageModelDetail initWithTitle:@"性别" subTitle:@"" bgImage:@""],
          [SLPageModelDetail initWithTitle:@"地区" subTitle:@"" bgImage:@""],
          [SLPageModelDetail initWithTitle:@"星座" subTitle:@"" bgImage:@""],],
          @[[SLPageModelDetail initWithTitle:@"用户服务协议" subTitle:@"" bgImage:@""],
            [SLPageModelDetail initWithTitle:@"免责声明" subTitle:@"" bgImage:@""]],
          @[[SLPageModelDetail initWithTitle:@"退出登录" subTitle:@"" bgImage:@""],]];
        _dataModels = [NSMutableArray arrayWithArray:arr];
    }
    return _dataModels;
}

- (SLImagePickerAndUpload *)imageUpload {
    if (!_imageUpload) {
        _imageUpload = [[SLImagePickerAndUpload alloc] init];
        WS(weakSelf);
        _imageUpload.uploadBlock = ^(NSString *imageUrl) {
            weakSelf.dataModels = nil;
            [weakSelf.tableView reloadData];
        };
    }
    return _imageUpload;
}

@end
