CNLiveMonthBillController.m 7.14 KB
//
//  CNLiveMonthBillController.m
//  CNLiveBalanceModule
//
//  Created by 153993236@qq.com on 11/12/2019.
//  Copyright (c) 2019 153993236@qq.com. All rights reserved.
//

#import "CNLiveMonthBillController.h"
#import "MJExtension.h"
#import <SDWebImage/SDWebImage.h>
#import "QMUIKit.h"
#import "CNLiveNetworking.h"
#import "CNLiveEnvironment.h"
#import "PGDatePickManager.h"

#import "CNUserInfoManager.h"
#import "CNLiveBaseKit.h"
#import "CNLiveCategory.h"
#import "CNLiveRefreshHeader.h"

#import "CNLiveServices.h"
#import "CNLiveBalanceRequest.h"

#import "CNLiveBalanceNavigationBar.h"
#import "CNLiveMonthBillView.h"
#import "CNLiveTradingRecordModel.h"
#import "CNLiveTradingRecordCell.h"

@interface CNLiveMonthBillController ()<UITableViewDelegate, UITableViewDataSource,CNLiveBalanceNavigationBarDelegate,CNLiveMonthBillViewDelegate>
@property (nonatomic, strong) CNLiveBalanceNavigationBar *navigationBar;
@property (nonatomic, strong) CNLiveMonthBillView *monthBillView;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *data;

@property (nonatomic, assign) NSInteger pageNo;

@property (nonatomic, copy) NSString *type;

@end

@implementation CNLiveMonthBillController
static const CGFloat height = 60;
- (instancetype)initWithDate:(NSString *)date startTime:(NSString *)startTime endTime:(NSString *)endTime{
    if(self = [super init]){
        self.date = date;
        self.startTime = startTime;
        self.endTime = endTime;
    }
    return self;
    
}
#pragma mark - 加载数据
- (void)loadData {
    [CNLiveBalanceRequest getTradingRecordWithType:self.type startTime:self.startTime endTime:self.endTime block:^(BOOL isSuccess, NSDictionary * _Nonnull data, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        if (error) {
            [self.data removeAllObjects];
            [self.tableView reloadData];
            [self showEmptyViewWithImage:[UIImage imageNamed:@"wufalianjie"] text:error.localizedDescription detailText:@"别紧张,试试看刷新页面~" buttonTitle:@"     重试     " buttonAction:@selector(loadData)];
            [self setCNAPIErrorEmptyView];
            self.emptyView.verticalOffset = -(NavigationContentTop+height);
            return;
        }
        
        if (isSuccess) {
            CNLiveTradingRecordModel *model = [CNLiveTradingRecordModel mj_objectWithKeyValues:data];
            if(self.pageNo == 1&&self.data.count > 0){
                [self.data removeAllObjects];
            }
            [self.data addObjectsFromArray:model.list];
            [self.tableView reloadData];
        }
    }];
    
}

#pragma mark - 生命周期
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    if (@available(iOS 11, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }

    self.type = @"2";
    self.pageNo = 1;
    [self createSubViews];
    [self loadData];
    
}

- (void)createSubViews {
    [self.view addSubview:self.navigationBar];
    [self.view addSubview:self.monthBillView];
    [self.view addSubview:self.tableView];

}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80.0;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CNLiveTradingRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:kCNLiveTradingRecordCell];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.data[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

#pragma mark - CNLiveMonthBillViewDelegate
- (void)monthBillView:(CNLiveMonthBillView *)view startTime:(NSString *)startTime endTime:(NSString *)endTime{
    self.startTime = startTime;
    self.endTime = endTime;
    [self loadData];
}
- (void)switchType:(NSString *)type{
    if(self.type != type){
        self.type = type;
        [self loadData];
    }
}

#pragma mark - 懒加载
- (NSMutableArray *)data {
    if (!_data) {
        _data = [[NSMutableArray alloc] init];
    }
    return _data;
}
- (CNLiveBalanceNavigationBar *)navigationBar {
    if (!_navigationBar) {
        _navigationBar = [[CNLiveBalanceNavigationBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, NavigationContentTop)];
        _navigationBar.backgroundColor = [UIColor whiteColor];
        _navigationBar.delegate = self;
        _navigationBar.title.text = @"月账单";
        _navigationBar.title.textColor = [UIColor blackColor];
        UIImage *image = [self getImageWithImageName:@"mine_back_black_b" bundleName:@"CNLiveBalanceModule" targetClass:[self class]];
        [_navigationBar.left setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        _navigationBar.right = nil;

    }
    return _navigationBar;
}
- (CNLiveMonthBillView *)monthBillView{
    if(!_monthBillView){
        _monthBillView = [[CNLiveMonthBillView alloc]initWithFrame:CGRectMake(0, NavigationContentTop, SCREEN_WIDTH, height)];
        _monthBillView.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0];
        _monthBillView.delegate = self;
        _monthBillView.date = self.date;
    }
    return _monthBillView;
}
- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavigationContentTop+height, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationContentTop-height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[CNLiveTradingRecordCell class] forCellReuseIdentifier:kCNLiveTradingRecordCell];
        _tableView.mj_header = [CNLiveRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    }
    return _tableView;
}
#pragma mark - 响应方法
- (void)goBack {
    if (self.presentingViewController != nil) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - CNLiveBalanceNavigationBarDelegate
- (void)navigationBar:(CNLiveBalanceNavigationBar *)navigationBar actionEvents:(NSString *)actionEvents{
    if ([actionEvents isEqualToString:@"1"]) {
        [self goBack];
    }else if ([actionEvents isEqualToString:@"2"]){
        
    }
}

@end