//
//  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"
#define kExpressCell @"OrderExpressTableViewCell.h"
#define kCellId @"OrderTableViewCell.h"
#define kPageSize 4

@interface NoPaymentTableViewController ()
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray *arrDatas;
@end

@implementation NoPaymentTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"OrderTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kCellId];
    [self.tableView registerNib:[UINib nibWithNibName:@"OrderExpressTableViewCell" bundle:nil] forCellReuseIdentifier:kExpressCell];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
    WS(weakSelf)
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 0;
        [weakSelf.arrDatas removeAllObjects];
        [weakSelf setUpData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page += 1;
        [weakSelf setUpData];
    }];
    [self setUpData];

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

- (void)setUpData {
    WS(weakSelf)
    [[HTTPCilent shareCilent] POST:@"order/query" parameters:[self getParams] success:^(NSURLSessionDataTask *task, id responseObject) {
        for (NSDictionary *dic in responseObject[@"queryResult"]) {
            JavenOrderModel *model = [JavenOrderModel modelObjectWithDictionary:dic];
            [weakSelf.arrDatas addObject:model];
        }
        
        [weakSelf.tableView reloadData];
        if (weakSelf.arrDatas.count  < kPageSize) {
            weakSelf.tableView.mj_footer.hidden = YES;
        }
        [weakSelf.tableView.mj_header endRefreshing];
        NSArray *array = responseObject[@"queryResult"];
        if ([array count] < kPageSize) {
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [weakSelf.tableView.mj_footer endRefreshing];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
    
}


- (NSDictionary *)getParams {
    NSString *orderState;
    
    if ([self.orderTag isEqualToString:@"待支付"]) {
        orderState = @"toBePaid";
    }else if([self.orderTag isEqualToString:@"待发货"]){
        orderState = @"toBeShipped";
    }else if ([self.orderTag isEqualToString:@"已发货"]){
        orderState = @"shipped";
    }else if ([self.orderTag isEqualToString:@"已完成"]){
        orderState = @"paid";
    }else if ([self.orderTag isEqualToString:@"已取消"]){
        orderState = @"cancelled";
    }
    
    NSDictionary *myDictionary = @{@"defintion" : @{@"conditions" : @[@{@"operation" : @"domain equals",
                                                                        @"parameter" : @{@"operation" : @"string"},
                                                                        @"parameters" : @[[UserInfo shareInstance].domain]},
                                                                      @{@"operation" : @"reseller uuid equals",
                                                                        @"parameter" : @{@"operation" : @"string"},
                                                                        @"parameters" : @[[UserInfo shareInstance].uuid]},
                                                                      @{@"operation" : @"order state equals",
                                                                        @"parameter" : @{@"operation" : @"string"},
                                                                        @"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 {
    
//    if ([self.orderTag isEqualToString:@"待支付"]) {
//    }else if([self.orderTag isEqualToString:@"待发货"]){
//    }else if ([self.orderTag isEqualToString:@"已发货"]){
//        return 170 + 120 * [self.arrDatas[indexPath.row] goodsDetails].count;
//    }else if ([self.orderTag isEqualToString:@"已完成"]){
//        return 170 + 120 * [self.arrDatas[indexPath.row] goodsDetails].count;
//    }else if ([self.orderTag isEqualToString:@"已取消"]){
//    }
    
    return 115 + 120 * [self.arrDatas[indexPath.row] goodsDetails].count;
}


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

#pragma mark - Table view data source

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

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if ([self.orderTag isEqualToString:@"已发货"] || [self.orderTag isEqualToString:@"已完成"]) {
//        OrderExpressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kExpressCell forIndexPath:indexPath];
//        
//        [cell cellWithModel:self.arrDatas[indexPath.row]];
//        [cell setState:_orderTag];
//        cell.checkExpress.hidden = NO;
//        return cell;
//
//    }else{
    
        OrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId forIndexPath:indexPath];
        
        [cell cellWithModel:self.arrDatas[indexPath.row]];
        [cell setState:_orderTag];
        return cell;
//    }
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderDetailViewController *orderDetailVC = [[OrderDetailViewController alloc] init];
    if ([self.orderTag isEqualToString:@"代支付"]) {
        orderDetailVC.orderType = ORDERTOPTYPE_NOPAYMENT;
    }else if([self.orderTag isEqualToString:@"待发货"]){
        orderDetailVC.orderType = ORDERTOPTYPE_WAITFORDISPATCH;
    }else if ([self.orderTag isEqualToString:@"已发货"]){
        orderDetailVC.orderType = ORDERTOPTYPE_TRANSPORTED;
    }else if ([self.orderTag isEqualToString:@"已完成"]){
        orderDetailVC.orderType = ORDERTOPTYPE_COMMPLETE;
    }else if ([self.orderTag isEqualToString:@"已取消"]){
        orderDetailVC.orderType = ORDERTOPTYPE_CANCELED;
    }
    
    orderDetailVC.isShowNavigationBar = YES;
    orderDetailVC.model = self.arrDatas[indexPath.row];
    [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
