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

#import "CommodityDetailViewController.h"
#import "CommodityDetailBottomView.h"
#import "CommodityDetailTopView.h"
#import "CommodityMutipleSelectBottomView.h"
@interface CommodityDetailViewController ()<UIWebViewDelegate>
@property (nonatomic, strong) CommodityDetailBottomView *bottomView;
@property (nonatomic, strong) CommodityDetailTopView *topView;
@end

@implementation CommodityDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([[[UIDevice currentDevice] systemVersion] doubleValue] > 7.0) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    self.view.backgroundColor = kBacroundColor;
    
    [self setUpWebView];
    [self.view addSubview:self.topView];
    [self.view addSubview:self.bottomView];
    self.bottomView.userInteractionEnabled = NO;
    //添加点击事件
    kButtonAddAction(self.bottomView.buttonShop, addCommodity)
    kButtonAddAction(self.bottomView.buttonShare, shareAction)
    kButtonAddAction(self.topView.buttonBack, backAction)
    kButtonAddAction(self.topView.buttonHome, homeAction)
    

}

- (void)backAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)homeAction {
    CLog(@"home");
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)addCommodity {
    CLog(@"添加商品");
    UserInfo *info = [UserInfo shareInstance];
    
    NSDictionary *param = @{
                            @"operCtx": @{
                                    @"time": [[NSDate date] timeStampNumber],
                                    @"domain": info.domain,
                                    @"operator": @{
                                            @"id": info.uuid,
                                            @"fullName": info.name
                                            }
                                    },
                            @"shopUuid": info.shop.uuid,
                            @"goodsUuid":self.model.uuid
                            };
    
    WS(weakSelf)
    [[HTTPCilent shareCilent] POST:@"shop/addGoods" parameters:param success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject[@"code"] isEqualToNumber: @0]) {
            
            [weakSelf addSUccess];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}

- (void)addSUccess {
    //添加成功提示框
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view.window];
    [self.view.window addSubview:hud];
    hud.animationType = MBProgressHUDAnimationFade;
    hud.removeFromSuperViewOnHide = YES;
    hud.labelText = @"添加成功！";
    hud.mode = MBProgressHUDModeText;
    [hud show:YES];
    [hud hide:YES afterDelay:1];
}


- (void)setUpWebView {
    UserInfo *userInfo = [UserInfo shareInstance];
    NSString *urlStr = [NSString stringWithFormat:@"%@Wap/detail/shop_id/%@/id/%@/hastbar/1/.html", userInfo.webShopBaseUrl, userInfo.shop.uuid, self.model.uuid];
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 67)];
    NSURL *url = [NSURL URLWithString:urlStr];
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
    webView.delegate = self;
    [self.view addSubview:webView];
    webView.userInteractionEnabled = YES;
    
}

- (void)shareAction {
    UserInfo *userInfo = [UserInfo shareInstance];
    [[ShareInstance shareInstace] showWithTitle:@"Aland" content:self.model.name url:[NSString stringWithFormat:@"%@Wap/detail/shop_id/%@/id/%@/hastbar/0/.html", userInfo.webShopBaseUrl, userInfo.shop.uuid, self.model.uuid] image:self.image];
}


//隐藏navigationbar
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}


#pragma mark =========== webView delegate ===========
- (void)webViewDidStartLoad:(UIWebView *)webView {
    [MBProgressHUD showHUDAddedTo:webView animated:YES];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    self.bottomView.userInteractionEnabled = YES;
    [MBProgressHUD hideHUDForView:webView];
}

#pragma mark =========== lazy loading ===========
- (CommodityDetailBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [CommodityDetailBottomView viewWithNibName:@"CommodityDetailBottomView"];
        _bottomView.frame = CGRectMake(0, kHeight - 67, kWidth, 67);
  
    }
    return _bottomView;
}

- (CommodityDetailTopView *)topView {
    if (!_topView) {
        _topView = [CommodityDetailTopView viewWithNibName:@"CommodityDetailTopView"];
        _topView.frame = CGRectMake(0, 0, kWidth, 86);
    }
    return _topView;
}

- (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
