//
//  RDSHomeListVC.m
//  Temperature
//
//  Created by RD on 2022/12/3.
//

#import "RDSHomeListVC.h"
#import "RDSHomeListCell.h"
#import "RDSHomeModel.h"
#import "RDSAddHomeVC.h"
#import "RDSHomeInfoVC.h"
#import <MJRefresh/MJRefresh.h>

static NSString * const RDSHomeListCellID = @"RDSHomeListCell";
@interface RDSHomeListVC ()<UITableViewDelegate , UITableViewDataSource>
@property (nonatomic, weak) UITableView *tableView;
@end

@implementation RDSHomeListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"家庭管理";
    
    [self p_navItem];
    [self p_setupTableView];
    
}

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

- (void)p_navItem{
    UIBarButtonItem *item = [RDSBarButtonItem barButtonItemWithTitle:@"新建" color:kTextColor target:self action:@selector(p_onAddItem)];
    [self.navigationItem setRightBarButtonItem:item];
}

- (void)p_onAddItem{
    
    RDSAddHomeVC *vc = [[RDSAddHomeVC alloc] init];
    [self pushViewController:vc animated:YES];
}

- (void)p_getHomeList{
    
    RDS_WEAKSELF(weakSelf)
    [RDSHomeModel rds_getHomeListIsAdmin:YES finished:^(NSError * _Nonnull error) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView reloadData];
    }];
}

- (void)p_setupTableView {
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, SCREEN_HEIGHT-20-60)];
    [self.view addSubview:tableView];
    tableView.dataSource = self;
    tableView.delegate = self;
    
    tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView = tableView;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"RDSHomeListCell" bundle:nil] forCellReuseIdentifier:RDSHomeListCellID];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    tableView.rowHeight = 100*kSCALE_WIDTH_X;
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(p_getHomeList)];
    header.automaticallyChangeAlpha = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    self.tableView.mj_header = header;
}

#pragma mark - Table view data source

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RDSHomeListCell *cell = [tableView dequeueReusableCellWithIdentifier:RDSHomeListCellID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = TheDataManager.homesAdmin[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    RDSHomeInfoVC *vc = [[RDSHomeInfoVC alloc] init];
    vc.home = TheDataManager.homesAdmin[indexPath.row];
    [self pushViewController:vc animated:YES];
}


- (void)dealloc{
    DDLog(@"dealloc");
    
}
@end
