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

#import "RebateDetailsViewController.h"
#import "RebateSuccessTableViewController.h"

@interface RebateDetailsViewController ()

/**
 *  输入提现金额
 */
@property (weak, nonatomic) IBOutlet UITextField *inputRebateTextField;

/**
 *  本次申请的UUID
 */
@property (nonatomic,copy) NSString *applyUUID;


@end

@implementation RebateDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.inputRebateTextField.placeholder = [NSString stringWithFormat:@"当前可提现:%@",self.rebateAmount];
}

- (IBAction)rebateButtonClickAction:(UIButton *)sender {
    
    WS(weakSelf);
    if (![self.inputRebateTextField.text length]) {
        [self SHOWPrompttext:@"请输入提现金额"];return;
    }
    if ([self.inputRebateTextField.text floatValue] > [self.rebateAmount floatValue]) {
        [self SHOWPrompttext:@"当前账户余额不足"];return;
    }
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:[NSString stringWithFormat:@"请确认提现金额:%@元",self.inputRebateTextField.text] preferredStyle:UIAlertControllerStyleAlert];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf CreateMBProgressHUDLoding];
        [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:SERVERREQUESTURL(WITHDRAWAL) WithRequestType:0 WithParameter:@{@"amount":self.inputRebateTextField.text} WithReturnValueBlock:^(id returnValue) {

            [weakSelf RemoveMBProgressHUDLoding];
            if ([returnValue[@"code"] isEqualToNumber:@0]) {
                weakSelf.applyUUID = returnValue[@"data"];
                [weakSelf rebateApplySuccess];
            }else
            {
                [weakSelf SHOWPrompttext:returnValue[@"message"]];
            }
        } WithErrorCodeBlock:^(id errorCodeValue) {
            [weakSelf RemoveMBProgressHUDLoding];
            [weakSelf SHOWPrompttext:NETWORK];
        } WithFailureBlock:^(NSError *error) {
            [weakSelf RemoveMBProgressHUDLoding];
            [weakSelf SHOWPrompttext:error.localizedDescription];
        }];
    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }]];
    [self presentViewController:alertVC animated:YES completion:nil];
}

#pragma mark - 提现申请成功
- (void)rebateApplySuccess
{
    WS(weakSelf);
    RebateSuccessTableViewController *success = [self.getStoryboardWithName instantiateViewControllerWithIdentifier:@"RebateSuccessTableViewController"];
    [success setClickEvent:^(NSIndexPath *indexPath) {
        if (indexPath.row == 1) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }else if (indexPath.row == 0)
        {
            [weakSelf queryDetails];
        }
    }];
    success.preferredContentSize = CGSizeMake(315, 320);
    success.modalPresentationStyle = UIModalPresentationFormSheet;
    UIPopoverPresentationController *pop = success.popoverPresentationController;
    pop.permittedArrowDirections = UIPopoverArrowDirectionAny;
    pop.sourceView = success.view;
    [self presentViewController:success animated:YES completion:nil];
}

#pragma mark - 查询详情
- (void)queryDetails
{
    [self CreateMBProgressHUDLoding];
    WS(weakSelf);
    NSString *urlString = [NSString stringWithFormat:@"%@/%@",WITHDRAWALPROGRESSDETAILS,self.applyUUID];
    [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:SERVERREQUESTURL(urlString) WithRequestType:1 WithParameter:nil WithReturnValueBlock:^(id returnValue) {
        [weakSelf RemoveMBProgressHUDLoding];
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            TOApplyBillEntity *result = [[TOApplyBillEntity alloc]initWithDictionary:returnValue[@"data"] error:nil];
            [weakSelf.navigationController popViewControllerAnimated:NO];
            if (weakSelf.showApplyDetails) {
                weakSelf.showApplyDetails(result);
            }
        }else
        {
            [weakSelf SHOWPrompttext:returnValue[@"message"]];
        }
    } WithErrorCodeBlock:^(id errorCodeValue) {
        [weakSelf RemoveMBProgressHUDLoding];
        [weakSelf SHOWPrompttext:NETWORK];
    } WithFailureBlock:^(NSError *error) {
        [weakSelf RemoveMBProgressHUDLoding];
        [weakSelf SHOWPrompttext:error.localizedDescription];
    }];
}

@end