//
//  OrderRecordViewController.m
//  Lighting
//
//  Created by 曹云霄 on 2016/10/25.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "OrderRecordViewController.h"
#import "OrderdetailsViewController.h"
#import "OrderTableViewCell.h"

@interface OrderRecordViewController ()<UITableViewDelegate,UITableViewDataSource>


/**
 *  订单记录数据
 */
@property (nonatomic,strong) NSMutableArray *orderRecordArray;

/**
 *  请求订单记录
 */
@property (nonatomic,strong) OrderFilter *orderRecordModel;

@end

@implementation OrderRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

#pragma mark -获取数据
- (void)loadWebDataSource
{
    WS(weakSelf);
    if (self.pullPageIndex == ONE) {
        [self.orderRecordArray removeAllObjects];
    }
    self.orderRecordModel.dp.page = self.pullPageIndex;
    [self getGuideAllcustomerOrderCompleted:^(OrderResponse *result) {
        if (weakSelf.pullPageIndex > result.totalpages) {
            [weakSelf endRefresh:EndRefreshNotData];
        }else {
            [weakSelf endRefresh:EndRefreshDefault];
        }
    }];
}

#pragma mark -获取客户订单数据
- (void)getGuideAllcustomerOrderCompleted:(void(^)(OrderResponse *result))completed
{
    WS(weakSelf);
    [HTTP networkRequestWithURL:SERVERREQUESTURL(QUERYORDER)  withRequestType:ZERO withParameter:self.orderRecordModel withReturnValueBlock:^(id returnValue) {
        if (RESULT(returnValue)) {
            OrderResponse *allorder = [[OrderResponse alloc]initWithDictionary:RESPONSE(returnValue) error:nil];
            completed(allorder);
            for (OrderBill *model in allorder.orderBillList) {
                [weakSelf.orderRecordArray addObject:model];
            }
            [weakSelf.tableView reloadData];
        }else
        {
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    }withFailureBlock:^(NSError *error) {
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ordercell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = [self.orderRecordArray objectAtIndex_opple:indexPath.row];
    return  cell;
}

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

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderdetailsViewController *orderdetails = [OrderdetailsViewController viewControllerWithStoryBoardType:STORYBOARD_TYPE_MAIN];
    TOOrderEntity *model = (TOOrderEntity *)[[self.orderRecordArray objectAtIndex_opple:indexPath.row] order];
    orderdetails.orderCode = model.orderNumber;
    if ([model.orderState isEqualToString:COMPLETED] || [model.orderState isEqualToString:PAID] || [model.orderState isEqualToString:SHIPPED]) {
        orderdetails.isShowShareButton = YES;
        orderdetails.isShowPayButton = NO;
        orderdetails.isShowHeaderView = YES;
        orderdetails.isUserInteractionEnabled = NO;
        
    } else if ([model.orderState isEqualToString:UNPAID]) {//未付款
        orderdetails.isShowPayButton = YES;
        orderdetails.isShowShareButton = YES;
        orderdetails.isShowHeaderView = YES;
        orderdetails.isUserInteractionEnabled = YES;
        
    }else{
        orderdetails.isShowHeaderView = NO;
    }
    //撤销、支付回调
    WS(weakSelf);
    [orderdetails setDelecteAndPayButtonBlock:^(NSInteger cellindex, NSString *orderSate) {
        OrderBill *model = [weakSelf.orderRecordArray objectAtIndex_opple:cellindex];
        model.order.orderState = orderSate;
        NSIndexPath *indexapath = [NSIndexPath indexPathForRow:cellindex inSection:0];
        [weakSelf.tableView reloadRowsAtIndexPaths:@[indexapath] withRowAnimation:UITableViewRowAnimationNone];
    }];
    [self pushViewController:orderdetails animated:YES];
}

#pragma mark - lazy
- (NSMutableArray *)orderRecordArray
{
    if (_orderRecordArray == nil) {
        
        _orderRecordArray = [NSMutableArray array];
    }
    return _orderRecordArray;
}

- (OrderFilter *)orderRecordModel
{
    if (!_orderRecordModel) {
        _orderRecordModel = [[OrderFilter alloc]init];
        DataPage *Newpage = [[DataPage alloc]init];
        Newpage.page = 1;
        Newpage.rows = KROWS;
        _orderRecordModel.dp = Newpage;
        _orderRecordModel.consumerIdEquals = self.model.consumer.fid;
    }
    return _orderRecordModel;
}

@end
