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

#import "ClientShoppingCarViewController.h"
#import "ShopcarModel.h"
#import "ProductDetailsViewController.h"
#import "ClientDetailsTableViewCell.h"

@interface ClientShoppingCarViewController ()<UITableViewDelegate,UITableViewDataSource>


/**
 *  购物车数据源
 */
@property (nonatomic,strong) NSMutableArray *shopResponseArray;

/**
 *  购物袋请求
 */
@property (nonatomic,strong) ShopCartFilter *shoppingCarModel;

@end

@implementation ClientShoppingCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

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

#pragma mark -获取购物车商品
- (void)getShoppingCardataCompleted:(void(^)(ShopCartResponse *result))completed
{
    WS(weakSelf);
    [HTTP networkRequestWithURL:SERVERREQUESTURL(SHOPPINGBAG)  withRequestType:ZERO withParameter:self.shoppingCarModel withReturnValueBlock:^(id returnValue) {
        if (RESULT(returnValue)) {
            ShopCartResponse *shopcar = [[ShopCartResponse alloc]initWithDictionary:RESPONSE(returnValue) error:nil];
            completed(shopcar);
            for (TOShopcartEntity *objc in shopcar.shopcart) {
                ShopcarModel *model = [[ShopcarModel alloc] initWithDictionary:[objc toDictionary] error:nil];
                [weakSelf.shopResponseArray addObject:model];
            }
            [weakSelf.tableView reloadData];
            
        }else
        {
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    }withFailureBlock:^(NSError *error) {
        [weakSelf endRefresh:EndRefreshDefault];
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}


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


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

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProductDetailsViewController *productDetails = [ProductDetailsViewController viewControllerWithStoryBoardType:STORYBOARD_TYPE_MAIN];
    ShopcarModel *model = [self.shopResponseArray objectAtIndex_opple:indexPath.row];
    productDetails.goodsID = model.goods.fid;
    [self pushViewController:productDetails animated:YES];
}

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

- (ShopCartFilter *)shoppingCarModel
{
    if (!_shoppingCarModel) {
        _shoppingCarModel = [[ShopCartFilter alloc]init];
        DataPage *Newpage = [[DataPage alloc]init];
        Newpage.page = ONE;
        Newpage.rows = KROWS;
        _shoppingCarModel.dp = Newpage;
        _shoppingCarModel.consumerId = self.model.consumer.fid;
    }
    return _shoppingCarModel;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
