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

#import "RebateViewController.h"
#import "PaymentsViewController.h"
#import "WithdrawalViewController.h"

@interface RebateViewController ()

/**
 *  头像
 */
@property (weak, nonatomic) IBOutlet UIImageView *headerImageView;

/**
 *  账户金额
 */
@property (weak, nonatomic) IBOutlet UILabel *currentAmountLabel;

/**
 *  昨日收益
 */
@property (weak, nonatomic) IBOutlet UILabel *yesterdayAmountLabel;

/**
 *  历史收益
 */
@property (weak, nonatomic) IBOutlet UILabel *historyAmountLabel;

/**
 *  提现
 */
@property (weak, nonatomic) IBOutlet UIButton *withdrawalButton;

/**
 *  收支明细，提现进度
 */
@property (weak, nonatomic) IBOutlet UIView *contentBackgroundView;

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

/**
 *  指示条
 */
@property (weak, nonatomic) IBOutlet UIView *instructionsLinesView;

/**
 *  收支明细
 */
@property (weak, nonatomic) IBOutlet UIButton *paymentsButton;

/**
 * 提现进度
 */
@property (weak, nonatomic) IBOutlet UIButton *withdrawalProgressButton;


@end

@implementation RebateViewController

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

#pragma mark - UI
- (void)uiConfigAction
{
    [self setupInstructionsViewOrigin:self.paymentsButton];
    [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:[Shoppersmanager manager].Shoppers.employee.picture] placeholderImage:ReplaceImage];
}

#pragma mark - 设置收支明细、提现进度
- (void)setupChildViewController
{
    PaymentsViewController *payments = [[self getStoryboardWithName] instantiateViewControllerWithIdentifier:@"PaymentsViewController"];
    [self addChildViewController:payments];
    WithdrawalViewController *withdrawal = [[self getStoryboardWithName] instantiateViewControllerWithIdentifier:@"WithdrawalViewController"];
    [self addChildViewController:withdrawal];
    payments.view.frame = CGRectMake(0, 40, self.contentBackgroundView.mj_w, self.contentBackgroundView.mj_h-40);
    withdrawal.view.frame = CGRectMake(0, 40, self.contentBackgroundView.mj_w, self.contentBackgroundView.mj_h-40);
    [self.contentBackgroundView addSubview:payments.view];
    self.currentVC = payments;
}


#pragma mark - 切换收支明细、提现进度
- (void)switchPaymentsVCAndWithdrawalVC:(BaseViewController *)newViewController
{
    [self transitionFromViewController:self.currentVC toViewController:newViewController duration:0.5 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished) {
        if (finished) {
            [self.contentBackgroundView addSubview:newViewController.view];
            self.currentVC = newViewController;
        }
    }];
}

#pragma mark - 切换ContentController
- (IBAction)paymentsButtonAndWithdrawalButtonClick:(UIButton *)sender {
    
    if ([sender isEqual:self.paymentsButton]) {
        sender.selected = YES;
        self.withdrawalButton.selected = NO;
    }else
    {
        sender.selected = YES;
        self.paymentsButton.selected = NO;
    }
    [self setupInstructionsViewOrigin:sender];
    [self switchPaymentsVCAndWithdrawalVC:self.childViewControllers[sender.tag-100]];
}

#pragma mark - 设置指示线的位置
- (void)setupInstructionsViewOrigin:(UIButton *)sender
{
    [UIView animateWithDuration:0.5 delay:0.1f usingSpringWithDamping:0.5f initialSpringVelocity:0.5f options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.instructionsLinesView.frame = CGRectMake(sender.mj_origin.x+(sender.mj_w-self.instructionsLinesView.mj_w)/2, self.instructionsLinesView.mj_origin.y, self.instructionsLinesView.mj_w, 2);
    } completion:nil];
}



@end
