//
//  ClientdetailsViewController.m
//  Lighting
//
//  Created by 曹云霄 on 16/5/3.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "ClientdetailsViewController.h"
#import "ClientShoppingCarViewController.h"
#import "OrderRecordViewController.h"

@interface ClientdetailsViewController ()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

/**
 *  当前控制器
 */
@property (nonatomic,strong) BaseViewController *currentVC;

/**
 *  购物袋
 */
@property (nonatomic,strong) ClientShoppingCarViewController *shoppingCar;

/**
 *  订单记录
 */
@property (nonatomic,strong) OrderRecordViewController *orderRecord;

@end

@implementation ClientdetailsViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self uiConfigAction];
    [self addChildViewController];
}


#pragma mark -UI
- (void)uiConfigAction
{
    //圆角
    [self.setupButton addCorner:kCornerRadius];
    [self.shoppingAndRecordBackview addCorner:kCornerRadius];
    [self.shoppingBagButton addCorner:kCornerRadius];
    [self.orderRecordButton addCorner:kCornerRadius];
    
    //默认选中购物袋
    self.shoppingBagButton.selected = YES;
    self.shoppingBagButton.backgroundColor = [UIColor whiteColor];
    
    //赋值
    [self.customerHeader sd_setImageWithURL:[NSURL URLWithString:self.model.consumer.picture] placeholderImage:ReplaceImage];
    self.customerName.text = self.model.consumer.name;
    self.customerPhoneNumber.text = self.model.consumer.mobile;
    self.companyName.text = self.model.consumer.company;
    self.customerLocation.text = [BaseViewController isMunicipality:self.model.consumer.province withCity:self.model.consumer.city withCountry:self.model.consumer.country withDetailAddress:self.model.consumer.address];
    self.setupButton.selected = self.model.selectedState;
    [self.setupButton setTitle:self.model.selectedState?@"退出当前客户":@"设为当前客户" forState:UIControlStateNormal];
}

#pragma mark - 添加子视图
- (void)addChildViewController
{
    self.shoppingCar = [ClientShoppingCarViewController viewControllerWithStoryBoardType:STORYBOARD_TYPE_MAIN];
    self.shoppingCar.model = self.model;
    [self addChildViewController:self.shoppingCar];
    self.currentVC = self.shoppingCar;
    [self.controllerBackgroundView addSubview:self.shoppingCar.view];
    self.orderRecord = [OrderRecordViewController viewControllerWithStoryBoardType:STORYBOARD_TYPE_MAIN];
    [self addChildViewController:self.orderRecord];
    self.orderRecord.model = self.model;
}

#pragma mark - 设置frame
- (void)viewDidLayoutSubviews
{
    self.currentVC.view.frame = CGRectMake(0, self.shoppingAndRecordBackview.bottom, self.controllerBackgroundView.width, self.controllerBackgroundView.height-self.shoppingAndRecordBackview.bottom);
    self.orderRecord.view.frame = CGRectMake(0, self.shoppingAndRecordBackview.bottom, self.controllerBackgroundView.width, self.controllerBackgroundView.height-self.shoppingAndRecordBackview.bottom);
}

#pragma mark - 切换子视图
- (void)switchChildView:(BaseViewController *)controller
{
    WS(weakSelf);
    [self transitionFromViewController:self.currentVC toViewController:controller duration:0.5 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished) {
        if (finished) {
            [weakSelf.controllerBackgroundView addSubview:controller.view];
            weakSelf.currentVC = controller;
        }
    }];
}

#pragma mark -设为当前的客户
- (IBAction)SetupcurrentUserButtonClick:(UIButton *)sender {
   
    if (self.currentUserBlock) {
        self.currentUserBlock(_cellindex,sender.currentTitle);
    }
     [sender setTitle:sender.selected?@"设为当前客户":@"退出当前客户" forState:UIControlStateNormal];
     sender.selected = !sender.selected;
}


#pragma mark -购物袋
- (IBAction)ShoppingbagButtonClick:(UIButton *)sender {
    
    sender.backgroundColor = [UIColor whiteColor];
    sender.selected = YES;
    self.orderRecordButton.selected = NO;
    self.orderRecordButton.backgroundColor = kMainBlueColor;
    [self switchChildView:self.shoppingCar];
}

#pragma mark -订单记录
- (IBAction)OrderrecordButtonClick:(UIButton *)sender {
    
    sender.backgroundColor = [UIColor whiteColor];
    sender.selected = YES;
    self.shoppingBagButton.selected = NO;
    self.shoppingBagButton.backgroundColor = kMainBlueColor;
    [self switchChildView:self.orderRecord];
}



@end
