//
//  GroupListController.m
//  AIIM
//
//  Created by gan on 2025/4/6.
//

#import <Foundation/Foundation.h>
#import "GroupListController.h"
#import "GroupNetApi.h"
#import "GroupListCell.h"
#import "GroupController.h"
#import "NewGroupController.h"

@interface GroupListController()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *titleLb;


@end

@implementation GroupListController

-(void)viewDidLoad{
    [super viewDidLoad];
    _titleLb.text = NSLocalizedString(@"TabBarItem3", @"");
    [self initSubView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self getGrouplistData];
}

-(void)initSubView{
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    [_tableView registerClass:[GroupListCell class] forCellReuseIdentifier:@"group"]; // 使用 Storyboard 时不需要这行代码，除非你混合使用 Storyboard 和代码配置 Cell。
}

-(void)getGrouplistData{
    [GroupNetApi getGroupList:^(int code, NSDictionary * res) {
        NSLog(@"getGrouplistData res:%@",res);
        NSNumber *codeD = res[@"code"];
        if([codeD intValue]==401){
            [[NSNotificationCenter defaultCenter] postNotificationName: nkonLogoutSucc object:nil];
            return;
        }
        self.grouplist = res[@"data"];
        [self.tableView reloadData];
    } fail:^(NSError * _Nonnull error) {
        NSLog(@"error");
    }];
    
}



- (IBAction)addNewGroup:(id)sender {
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    NewGroupController *groupctr = [board instantiateViewControllerWithIdentifier:@"NewGroupController"];
    UINavigationController *uiNavC = [[UINavigationController alloc] initWithRootViewController:groupctr];
    uiNavC.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController :uiNavC animated:YES completion:nil];
    
}


#pragma mark UITableViewDelegate


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    NSLog(@"numberOfRowsInSection:%lu",(unsigned long)self.grouplist.count);
    return self.grouplist.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *data=self.grouplist[indexPath.row];
//    NSLog(@"cellForRowAtIndexPath:%@",data);
    if(data){
        
        GroupListCell *cell =[tableView dequeueReusableCellWithIdentifier:@"group" forIndexPath:indexPath];
        if(cell==nil){
            cell=[[GroupListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"group"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        [cell fillWithData:data cellType:0];
        return cell;
    }
    return nil;
}

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

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"didSelectRowAtIndexPath:%@",indexPath);
    
    NSDictionary *data=self.grouplist[indexPath.row];
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    GroupController *groupctr = [board instantiateViewControllerWithIdentifier:@"GroupController"];
    UINavigationController *uiNavC = [[UINavigationController alloc] initWithRootViewController:groupctr];
    uiNavC.modalPresentationStyle = UIModalPresentationFullScreen;
    groupctr.groupMsg = data;
    groupctr.groupId = data[@"id"];
    [self presentViewController :uiNavC animated:YES completion:nil];
}
#pragma mark statusBar
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
@end
