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

#import "OrderDetailViewController.h"
#import "NSString+Category.h"
#import "OrderDetailTableViewCell.h"
#import "UITableView+Category.h"
#import "SeeLogistics.h"
#import "OrderDetailBottomView.h"
#import "CommodityDetailViewController.h"
#import "CommodityListModel/CommotityListModel.h"

#define kCellId @"OrderDetailTableViewCell.h"
@interface OrderDetailViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) OrderDetailBottomView *bottomView;
@property (nonatomic, strong) OrderDetailTopView *atopView;


@end

@implementation OrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kCustomGreenColor;
    self.navigationItem.title = @"订单详情";
    
    //tableView
    self.tableView = [UITableView plainTableViewWithTarget:self cellNibName:@"OrderDetailTableViewCell" cellId:kCellId];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = kBacroundColor;
    
    [self.view addSubview:self.tableView];//topView
    
    
    OrderDetailTopView *topView = [OrderDetailTopView viewWithNibName:@"OrderDetailTopView"];
    self.atopView = topView;
    self.atopView.labelExpressTime.text = self.model.lastModifyInfo.time;
    self.atopView.labelExpress.text = self.model.shipType;
    self.atopView.orderType = [self.model.state getOrderStateString];;
    [self.atopView viewWithModel:self.model];
    [self.tableView addSubview:topView];
    
    [self.atopView updateByOrderType];
    
    self.bottomView = [OrderDetailBottomView viewWithNibName:@"OrderDetailBottomView"];
    [self.bottomView viewWithModel:self.model];
    
    self.tableView.tableFooterView = self.bottomView;
    
    //底部查看物流按钮
//    if (self.orderType == ORDERTOPTYPE_TRANSPORTED) {
//        SeeLogistics *viewBottom = [SeeLogistics viewWithNibName:@"SeeLogistics"];
//        viewBottom.frame = CGRectMake(0, kHeight - 64 - 65, kWidth,  64 + 65);
//        [self.view addSubview:viewBottom];
//    }
    

    
    // Do any additional setup after loading the view.
}

- (void)viewDidLayoutSubviews {
    
    self.tableView.frame = CGRectMake(0, 0, kWidth, kHeight);
    
    [self.atopView updateHeight];
    self.atopView.frame = CGRectMake(0, -self.atopView.height, kWidth, self.atopView.height);

    [self.bottomView updateHeight];
    self.bottomView.width = kWidth;
    
    self.tableView.contentInset = UIEdgeInsetsMake(self.atopView.height, 0, self.bottomView.height - 250, 0);
    
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

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

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

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

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    JavenGoodsDetails *details = self.model.goodsDetails[indexPath.row];
    
    CommodityDetailViewController *detailVC = [[CommodityDetailViewController alloc] init];
    
    CommotityListModel *model = [[CommotityListModel alloc] init];
    model.uuid = details.goods.uuid;
    model.name = details.goods.name;
    
    OrderDetailTableViewCell *cell = (OrderDetailTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    detailVC.image = cell.imgCommdity.image;
    detailVC.model = model;
    detailVC.isShowNavigationBar = NO;
    [self.navigationController pushViewController:detailVC animated:YES];
}




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

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