//
//  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"
#import "WebViewController.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 isEqualToString:@"待支付"]) {
        SeeLogistics *viewBottom = [SeeLogistics viewWithNibName:@"SeeLogistics"];
        [viewBottom.btn addTarget:self action:@selector(actionPay) forControlEvents:UIControlEventTouchUpInside];
        viewBottom.frame = CGRectMake(0, kHeight - 64 - 65, kWidth,  64 + 65);
        [self.view addSubview:viewBottom];
    }
    // Do any additional setup after loading the view.
}

- (void)actionPay {
    WebViewController *payVC = [[WebViewController alloc] init];
    UserInfo *user = [UserInfo shareInstance];
    NSString *url = [NSString stringWithFormat:@"%@Wap/orderDetail/id/%@/shop_id/%@/mobile/%@.html",user.appConfig.webShopBaseUrl,self.model.billNumber,self.model.shop.uuid,user.reseller.mobilephone];
    payVC.URL = url;
    payVC.title = @"付款";
    [self.navigationController pushViewController:payVC animated:YES];
}

- (void)viewDidLayoutSubviews {
    
    self.tableView.frame = CGRectMake(0, 0, kWidth, kHeight);
    //底部查看物流按钮
    if ([self.orderType isEqualToString:@"待支付"]) {
       self.tableView.frame = CGRectMake(0, 0, kWidth, kHeight - 64);
    }
    
    [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
