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

#import "RebateViewController.h"
#import "PaymentsViewController.h"
#import "WithdrawalViewController.h"
#import "RebateDetailsViewController.h"
#import "WithdrawalTableViewController.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 UILabel *freezeAmountLabel;

/**
 *  提现
 */
@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;

/**
 *  账户数据
 */
@property (nonatomic,strong) EarningsResponse *model;


@end

@implementation RebateViewController

- (void)viewDidAppear:(BOOL)animated
{
    [self getRebateDatasFromUser];
}

- (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];
}


#pragma mark - 查询返利信息
- (void)getRebateDatasFromUser
{
    WS(weakSelf);
    [self CreateMBProgressHUDLoding];
    NSString *URL = [NSString stringWithFormat:@"%@/%@",REBATE,[Shoppersmanager manager].Shoppers.employee.fid];
    [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:SERVERREQUESTURL(URL) WithRequestType:1 WithParameter:nil WithReturnValueBlock:^(id returnValue) {
        
        [weakSelf RemoveMBProgressHUDLoding];
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            weakSelf.model = [[EarningsResponse alloc]initWithDictionary:returnValue[@"data"] error:nil];
             weakSelf.currentAmountLabel.text = [NSString stringWithFormat:@"%.2f",[returnValue[@"data"][@"accountTotal"] floatValue]];
             weakSelf.yesterdayAmountLabel.text = [NSString stringWithFormat:@"%.2f",[returnValue[@"data"][@"yesterdayEarnings"] floatValue]];
             weakSelf.historyAmountLabel.text = [NSString stringWithFormat:@"%.2f",[returnValue[@"data"][@"historyEarning"] floatValue]];
            weakSelf.freezeAmountLabel.text = [NSString stringWithFormat:@"%.2f",[returnValue[@"data"][@"applytotal"] floatValue]];
        }else
        {
            [weakSelf SHOWPrompttext:returnValue[@"message"]];
        }
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        [weakSelf RemoveMBProgressHUDLoding];
        [weakSelf SHOWPrompttext:NETWORK];
    } WithFailureBlock:^(NSError *error) {
        [weakSelf RemoveMBProgressHUDLoding];
        [weakSelf SHOWPrompttext:error.localizedDescription];
    }];
}


#pragma mark - 提现
- (IBAction)withdrawalButtonClickAction:(UIButton *)sender {
    
    WS(weakSelf);
    RebateDetailsViewController *rebateDetails = [self.getStoryboardWithName instantiateViewControllerWithIdentifier:@"RebateDetailsViewController"];
    rebateDetails.rebateAmount = [self.model.accountTotal floatValue]-[self.model.applytotal floatValue];
    [rebateDetails setShowApplyDetails:^(TOApplyBillEntity *entity) {
        WithdrawalTableViewController *detailVC = [weakSelf.getStoryboardWithName instantiateViewControllerWithIdentifier:@"WithdrawalTableViewController"];
        detailVC.model = entity;
        [weakSelf.navigationController pushViewController:detailVC animated:YES];
    }];
    [self.navigationController pushViewController:rebateDetails animated:YES];
}




@end
