//
//  NoPaymentTableViewController.m
//  ALand
//
//  Created by Z on 16/3/30.
//  Copyright © 2016年 Z. All rights reserved.
//

#import "NoPaymentTableViewController.h"
#import "OrderTableViewCell.h"
#import "OrderDetailViewController.h"
#import "OrderExpressTableViewCell.h"
#import "JavenOrderModel.h"
#import "JavenGoodsDetails.h"
#import "MJRefresh.h"
#import "OrderDetailTableViewCell.h"
#import "OrderListHeaderView.h"
#import "OrderListFooterView.h"

#define kExpressCell @"OrderExpressTableViewCell.h"
#define kCellId @"OrderTableViewCell.h"
#define kPageSize 10
#define kHeaderID @"headerId"
#define kFooterID @"footerId"
@interface NoPaymentTableViewController ()
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray *arrDatas;
@end

@implementation NoPaymentTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isFirstLoaded = YES;
    [self.tableView registerNib:[UINib nibWithNibName:@"OrderDetailTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kCellId];
    [self.tableView registerNib:[UINib nibWithNibName:@"OrderListHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:kHeaderID];
    [self.tableView registerNib:[UINib nibWithNibName:@"OrderListFooterView" bundle:nil] forHeaderFooterViewReuseIdentifier:kFooterID];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
    WS(weakSelf)
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 0;
        [MBProgressHUD showHUDAddedTo:kWindow animated:YES];
        [weakSelf.arrDatas removeAllObjects];
        [weakSelf setUpData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page += 1;
        [weakSelf setUpData];
    }];

}
- (NSMutableArray *)arrDatas {
    if (!_arrDatas) {
        _arrDatas = [NSMutableArray array];
    }
    return _arrDatas;
}

- (void)setUpData {
    self.isFirstLoaded = NO;
    WS(weakSelf)
    [[HTTPCilent shareCilent] POST:@"order/query" parameters:[self getParams] success:^(NSURLSessionDataTask *task, id responseObject) {
        [MBProgressHUD hideHUDForView:kWindow];
        [weakSelf.tableView.mj_header endRefreshing];

        for (NSDictionary *dic in responseObject[@"queryResult"]) {
            JavenOrderModel *model = [JavenOrderModel modelObjectWithDictionary:dic];
            [weakSelf.arrDatas addObject:model];
        }

        //判断后面是否还有数据
        NSArray *array = responseObject[@"queryResult"];
        if ([array count] < kPageSize) {
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [weakSelf.tableView.mj_footer endRefreshing];
        }
        
        if (weakSelf.arrDatas.count > 0) {
            [weakSelf.tableView reloadData];
        }

    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
    
}


- (NSDictionary *)getParams {
    
    
     id orderState = [self.orderTag getOrderState];
    
    if ([self.orderTag isEqualToString:@"全部订单"]) {
        orderState = [NSNull null];
    }
    
    NSDictionary *myDictionary = @{@"defintion" : @{@"conditions" : @[@{@"operation" : @"domain equals",
                                                                        @"parameters" : @[kDomain]},
                                                                      @{@"operation" : @"reseller uuid equals",
                                                                        @"parameters" : @[[UserInfo shareInstance].uuid]},
                                                                      @{@"operation" : @"order state equals",
                                                                        @"parameters" : @[orderState]}],
                                                    @"orders" : @[@{@"field" : @"createInfo.time",
                                                                    @"direction" : @"desc"}],
                                                    @"pageSize" : @(kPageSize),
                                                    @"page" : @(self.page),
                                                    @"probePages" : @0},
                                   @"fetchParts" : @[@"string"]};
    return myDictionary;
}


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


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.arrDatas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self.arrDatas[section] goodsDetails] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 80;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    OrderListHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kHeaderID];
    [header setUpViewWithModel:self.arrDatas[section] state:self.orderTag];
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    OrderListFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kFooterID];
    [footer setUpViewWithModel:self.arrDatas[section]];
    return footer;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    OrderDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId forIndexPath:indexPath];
    
    [cell cellWithModel:[self.arrDatas[indexPath.section] goodsDetails][indexPath.row]];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderDetailViewController *orderDetailVC = [[OrderDetailViewController alloc] init];
    
    orderDetailVC.isShowNavigationBar = YES;
    orderDetailVC.model = self.arrDatas[indexPath.section];
    [self.navigationController pushViewController:orderDetailVC animated:YES];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

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