//
//  GasStationinvoiceRecordVC.m
//  Open
//
//  Created by qinxuyang on 2021/11/30.
//  Copyright © 2021 QXY. All rights reserved.
//

#import "GasStationinvoiceRecordVC.h"
#import "IoninvoiceModel.h"
#import "InvoiceRecordCell.h"
#import "InvoiceRecordWebVC.h"
#import "GasStationinvoiceVC.h"
@interface GasStationinvoiceRecordVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) EmptyView *emptyView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) int pageIndex;
@end

@implementation GasStationinvoiceRecordVC

- (NSMutableArray *)dataArr{
    if (_dataArr == nil) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self configNav];
    
    [self.view addSubview:self.tableView];

    [self.tableView.mj_header beginRefreshing];
    
    [self emptyViewInit];
}
- (void)configNav
{
   UIView*nav = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, cl_kNavigationBarHeight)];
    nav.backgroundColor = [UIColor colorWithHex:0x086ed7];
    [self.view addSubview:nav];
    
    UIButton*backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setTitle:@"返回" forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backBtn layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleLeft imageTitleSpace:4];
    [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [nav addSubview:backBtn];
    
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.top.offset(cl_kStatusBarHeight);
        make.width.offset(80);
        make.height.offset(44);
    }];
    
    UILabel*titleLabel = [[UILabel alloc]init];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"开票记录";
    titleLabel.font = [UIFont boldSystemFontOfSize:17];
    [nav addSubview:titleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.offset(cl_kStatusBarHeight);
        make.width.offset(80);
        make.height.offset(44);
    }];
    
//    UIButton *orderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    orderBtn.frame = CGRectMake(ScreenWidth - 80, cl_kStatusBarHeight, 80, 44);
//    [orderBtn setTitle:@"开发记录" forState:UIControlStateNormal];
//    [orderBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    orderBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
//    [orderBtn addTarget:self action:@selector(GasStationinvoiceRecordVC) forControlEvents:UIControlEventTouchUpInside];
//    [nav addSubview:orderBtn];
}
- (void)emptyViewInit
{
    _emptyView = [[[NSBundle mainBundle]loadNibNamed:@"EmptyView" owner:nil options:nil] lastObject];
    _emptyView.frame = CGRectMake((ScreenWidth - 152)/2, (ScreenHeight - 160)/2 - cl_kNavigationBarHeight + 44 , 152, 160);
    [self.view addSubview:_emptyView];
}
- (void)backClick{
    [self.navigationController popViewControllerAnimated:YES];
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, cl_kNavigationBarHeight, ScreenWidth, ScreenHeight - cl_kNavigationBarHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;//推荐该方法
        __weak typeof(self) weakself = self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakself refreshHeader];
        }];
//        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//            [weakself upDown];
//        }];
        [_tableView registerNib:[UINib nibWithNibName:@"InvoiceRecordCell" bundle:nil] forCellReuseIdentifier:@"InvoiceRecordCell"];
    }
    return _tableView;
}
- (void)refreshHeader{
    _pageIndex = 1;
    [self requestList];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    InvoiceRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InvoiceRecordCell"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"InvoiceRecordCell" owner:self options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    IoninvoiceModel *model = self.dataArr[indexPath.row];
    if (model.invoiceTitleType == 0) {
        cell.invoiceTitleType.text = @"公司";
    }else{
        cell.invoiceTitleType.text = @"个人";
    }
    cell.invoiceTotalPriceTax.text = [NSString stringWithFormat:@"%@",model.invoiceTotalPriceTax];
    cell.invoiceDate.text = [NSString stringWithFormat:@"%@",model.invoiceDate];
    cell.detailBtn.tag = indexPath.row;
    [cell.detailBtn addTarget:self action:@selector(detailBtn:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 170;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    IoninvoiceModel *model = self.dataArr[indexPath.row];
    InvoiceRecordWebVC *VC = [[InvoiceRecordWebVC alloc]init];
    VC.url = model.imgUrl;
    [self.navigationController pushViewController:VC animated:YES];
}
#pragma mark - 重新开票
- (void)detailBtn:(UIButton *)button{
    IoninvoiceModel *model = self.dataArr[button.tag];
    GasStationinvoiceVC *VC = [[GasStationinvoiceVC alloc]init];
    VC.model = model;
    VC.isNoEditor = YES;
    [self.navigationController pushViewController:VC animated:YES];
}
#pragma mark - 发票信息
- (void)invoiceInfo:(UIButton *)button{
//    IoninvoiceModel *model = self.dataArr[button.tag];
    InvoiceRecordWebVC *VC = [[InvoiceRecordWebVC alloc]init];
    VC.url = @"";
    [self.navigationController pushViewController:VC animated:YES];
}
#pragma mark - 发票记录
- (void)requestList{
    NSString *url = [NSString stringWithFormat:@"%@tuanyou//invoice/finished",[NetworkConfig ipGasStationConfig]];
    NSMutableDictionary *parms  = [NSMutableDictionary dictionary];
    [parms setValue:[getPeopleID PeopleIphoneNmber] forKey:@"phone"];
    [parms setValue:@(100) forKey:@"pageSize"];
    [parms setValue:@(_pageIndex) forKey:@"pageIndex"];
    NSString *GasToken =  [UserDefault valueForKey:@"GasToken"];
    if ([UserDefault valueForKey:@"GasToken"] && GasToken.length > 0) {
        [parms setValue:GasToken forKey:@"token"];
    }
    NSLog(@"筛选参数=====%@",parms);
    typeof(self) wSelf = self;
    [NetworkRequest PostGasStation:url Params:parms Success:^(id _Nonnull objc) {
        [wSelf.tableView.mj_header endRefreshing];
        [wSelf.tableView.mj_footer endRefreshing];
        if ([objc isKindOfClass:[NSDictionary class]]) {
            NSString *state = [NSString stringWithFormat:@"%@",objc[@"state"]];
            if ([state isEqualToString:@"200"]) {
                if (wSelf.pageIndex == 1) {
                    [wSelf.dataArr removeAllObjects];
                }
                NSString *str = [NSString stringWithFormat:@"%@",objc[@"result"]];
                if ([str isEqualToString:@"<null>"] || [str isEqualToString:@""]) {
                    if (wSelf.dataArr.count > 0) {
                        wSelf.emptyView.hidden = YES;
                    }else{
                        wSelf.emptyView.hidden = NO;
                    }
                    [wSelf.tableView reloadData];
                    return;
                }
                NSArray *arr = objc[@"result"];
                if (arr.count > 0) {
                    NSArray *resultArr = [IoninvoiceModel mj_objectArrayWithKeyValuesArray:objc[@"result"]];
                    for (IoninvoiceModel *model in resultArr) {
                        [wSelf.dataArr addObject:model];
                    }
                }
                if (wSelf.dataArr.count > 0) {
                    wSelf.emptyView.hidden = YES;
                }else{
                    wSelf.emptyView.hidden = NO;
                }
                [wSelf.tableView reloadData];
            }
        }
     
    } Failure:^(NSError *error) {
        NSLog(@"error:%@",error);
        [wSelf.tableView.mj_header endRefreshing];
        [wSelf.tableView.mj_footer endRefreshing];
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end
