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

#import "GasStationPayListVC.h"
#import "GasStationPayCell.h"
#import "GasOrderModel.h"
#import "GasStationPayDetailsVC.h"
#import "GasStationinvoiceListVC.h"
@interface GasStationPayListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) int pageIndex;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) EmptyView *emptyView;
@end

@implementation GasStationPayListVC
- (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(GasStationinvoiceList) forControlEvents:UIControlEventTouchUpInside];
    [nav addSubview:orderBtn];
}
#pragma mark - 开发票
- (void)GasStationinvoiceList{
    GasStationinvoiceListVC *VC = [[GasStationinvoiceListVC alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
}
- (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:@"GasStationPayCell" bundle:nil] forCellReuseIdentifier:@"GasStationPayCell"];
    }
    return _tableView;
}
- (void)refreshHeader{
    _pageIndex = 1;
    [self requestList];
}
- (void)upDown{
    _pageIndex++;
    
    [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 {
    GasStationPayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GasStationPayCell"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"GasStationPayCell" owner:self options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    GasOrderModel *model = self.dataArr[indexPath.row];
    cell.orderStatusName.text = model.orderStatusName;
    
    cell.orderTimeLabel.text = model.orderTime;
    [cell.gasLogoSmall sd_setImageWithURL:[NSURL URLWithString:model.gasLogoSmall] placeholderImage:nil];
    cell.gasName.text = model.gasName;
    cell.gasAddress.text = model.gasAddress;
    cell.gasMessage.text = [NSString stringWithFormat:@"加油信息：%@ (%@L) %@号枪",model.oilNo,model.litre,model.gunNo];
    
    NSString *price = [NSString stringWithFormat:@"总额¥%@，优惠¥%@，实付：¥%@",model.amountGun,model.amountDiscounts,model.amountPay];
    NSString *amountPay = model.amountPay;
    NSMutableAttributedString *priceFont = [[NSMutableAttributedString alloc] initWithString:price];
    [priceFont addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20 weight:UIFontWeightMedium] range:NSMakeRange(price.length - amountPay.length, amountPay.length)];
    [priceFont addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(price.length - amountPay.length - 4, amountPay.length + 4)];
    cell.price.attributedText = priceFont;
    
    [cell.OrderDetailBtn setTitle:@"查看详情" forState:UIControlStateNormal];
    cell.OrderDetailBtn.tag = indexPath.row;
    [cell.OrderDetailBtn addTarget:self action:@selector(OrderDetailBtn:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 220;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    GasOrderModel *model = self.dataArr[indexPath.row];
    GasStationPayDetailsVC *VC = [[GasStationPayDetailsVC alloc]init];
    VC.Model = model;
    [self.navigationController pushViewController:VC animated:YES];
}
#pragma mark - 详情
- (void)OrderDetailBtn:(UIButton *)button{
    GasOrderModel *model = self.dataArr[button.tag];
    GasStationPayDetailsVC *VC = [[GasStationPayDetailsVC alloc]init];
    VC.Model = model;
    [self.navigationController pushViewController:VC animated:YES];
}
#pragma mark - 油站列表
- (void)requestList{
    NSString *url = [NSString stringWithFormat:@"%@tuanyou/order/info",[NetworkConfig ipGasStationConfig]];
    NSMutableDictionary *parms  = [NSMutableDictionary dictionary];
    [parms setValue:[getPeopleID PeopleIphoneNmber] forKey:@"phone"];
    [parms setValue:@(10) forKey:@"pageSize"];
    [parms setValue:@(_pageIndex) forKey:@"pageIndex"];
    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 = [GasOrderModel mj_objectArrayWithKeyValuesArray:objc[@"result"]];
                    for (GasOrderModel *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
