//
//  PayHistoryController.m
//  Open
//
//  Created by 雷俊博 on 2020/11/25.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "PayHistoryController.h"
#import "PayHistoryCell.h"

#import "PayHistoryModel.h"
#import "EmptyView.h"

@interface PayHistoryController ()<UITableViewDelegate,UITableViewDataSource>
{
    EmptyView* emptyView;
}
@property (nonatomic, strong)UITableView* tableView;

@property (nonatomic, strong)NSArray* dataArray;

@end

@implementation PayHistoryController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [GTCountSDK trackCountEvent:@"home_shebaojiaofei_orderList" withArgs:@{}];//个推统计
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.tableView];
    [self emptyViewInit];
    
    [self loadData];
}

- (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);
    emptyView.hidden = NO;
    [self.view addSubview:emptyView];
}

- (void)loadData {
//    WeakSelf
    NSString *urlString = [NSString stringWithFormat:@"%@%@",[NetworkConfig ipMySocialInsuranceConfig],BuyedOrderListRequest];
    NSDictionary*params = @{
        @"key":[PayUserInfoManager sharedManager].model.key ?[PayUserInfoManager sharedManager].model.key:@""
    };
    NSLog(@"获取已购社保列表url:%@",urlString);
    NSLog(@"params:%@",params);
    
    [KKLoadingAnimationView show];
    [NetworkRequest Post:urlString Params:params Success:^(id _Nonnull objc) {
        [KKLoadingAnimationView dismiss];
        NSLog(@"objc:%@",objc);
        NSString*code = [NSString stringWithFormat:@"%@",objc[@"code"]];
        NSString*msg = [NSString stringWithFormat:@"%@",objc[@"msg"]];
        if ([code isEqualToString:@"200"]) {
            self.dataArray = [PayHistoryModel mj_objectArrayWithKeyValuesArray:objc[@"data"]];
            
            [self.tableView reloadData];
        }else{
            [self alertShow:msg];
        }
        
        self->emptyView.hidden = self.dataArray.count != 0;
        
    } Failure:^(NSError* error) {
        [KKLoadingAnimationView dismiss];
        NSLog(@"error:%@",error);
        [self alertShow:@"网络异常"];
        
    }];
}

#pragma mark - 提示
- (void)alertShow:(NSString *)msg
{
    if (@available(iOS 9.0,*)) {
        
        UIAlertController * alertV = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertV addAction:confirm];
        
        [self presentViewController:alertV animated:YES completion:nil];
        
    }else{
        UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alertV show];
    }
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PayHistoryModel* model = self.dataArray[indexPath.section];
    
    PayHistoryCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.contentView.layer.cornerRadius = 10;
    cell.contentView.layer.masksToBounds = YES;
    
    cell.lb_name.text = model.title;
    
    if ([model.status isEqualToString:@"1"]) {
        cell.lb_largeState.text = @"待付款";
        cell.lb_smallState.text = @"待付款";
        cell.lb_largeState.textColor = [UIColor redColor];
        cell.lb_smallState.textColor = [UIColor redColor];
    }else if ([model.status isEqualToString:@"2"]) {
        
        cell.lb_largeState.text = @"付款成功";
        cell.lb_smallState.text = @"付款成功";
        cell.lb_largeState.textColor = kRGBColor(24, 133, 37);
        cell.lb_smallState.textColor = kRGBColor(24, 133, 37);
        
    }else if ([model.status isEqualToString:@"3"]) {
        
        cell.lb_largeState.text = @"正在申报中";
        cell.lb_smallState.text = @"正在申报中";
        cell.lb_largeState.textColor = kRGBColor(24, 133, 37);
        cell.lb_smallState.textColor = kRGBColor(24, 133, 37);
        
    }else if ([model.status isEqualToString:@"4"]) {
        
        cell.lb_largeState.text = @"参保成功";
        cell.lb_smallState.text = @"参保成功";
        
    }else if ([model.status isEqualToString:@"5"]) {
        
        cell.lb_largeState.text = @"不成功";
        cell.lb_smallState.text = @"不成功";
        cell.lb_largeState.textColor = [UIColor redColor];
        cell.lb_smallState.textColor = [UIColor redColor];
        
    }else if ([model.status isEqualToString:@"6"]) {
        
        cell.lb_largeState.text = @"申请停保";
        cell.lb_smallState.text = @"申请停保";
        cell.lb_largeState.textColor = kRGBColor(24, 133, 37);
        cell.lb_smallState.textColor = kRGBColor(24, 133, 37);
        
    }else if ([model.status isEqualToString:@"8"]) {
        
        cell.lb_largeState.text = @"停保处理中";
        cell.lb_smallState.text = @"停保处理中";
        cell.lb_largeState.textColor = kRGBColor(24, 133, 37);
        cell.lb_smallState.textColor = kRGBColor(24, 133, 37);
        
    }else if ([model.status isEqualToString:@"10"]) {
        
        cell.lb_largeState.text = @"已停保";
        cell.lb_smallState.text = @"已停保";
        cell.lb_largeState.textColor = kRGBColor(30, 196, 237);
        cell.lb_smallState.textColor = kRGBColor(30, 196, 237);
        
    }else if ([model.status isEqualToString:@"11"]) {
        
        cell.lb_largeState.text = @"已全部退款";
        cell.lb_smallState.text = @"已全部退款";
        cell.lb_largeState.textColor = kRGBColor(24, 133, 37);
        cell.lb_smallState.textColor = kRGBColor(24, 133, 37);
        
    }else if ([model.status isEqualToString:@"15"]) {
        cell.lb_largeState.text = @"用户申请退款";
        cell.lb_smallState.text = @"用户申请退款";
        cell.lb_largeState.textColor = kRGBColor(24, 133, 37);
        cell.lb_smallState.textColor = kRGBColor(24, 133, 37);
    }
    
    
    cell.lb_uName.text = model.u_name;
    cell.lb_uId.text = model.id_card;
    cell.lb_uMonth.text = [NSString stringWithFormat:@"%@年%@月",[model.start_month substringToIndex:4],[model.start_month substringFromIndex:4]];
    cell.lb_uCity.text = model.city_name;
    cell.lb_uOrder.text = model.buy_no;
    
    cell.lb_amount.text = [NSString stringWithFormat:@"合计金额:¥%@",model.total_fee];
    
    return cell;
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor colorWithHex:0xf4f4f4];
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor colorWithHex:0xf4f4f4];
    return view;
}

#pragma mark - getters and setters
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"PayHistoryCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    }
    return _tableView;
}

/*
#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
