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

#import "WithdrawalViewController.h"
#import "WithdrawalTableViewCell.h"
#import "WithdrawalTableViewController.h"

@interface WithdrawalViewController ()<UITableViewDelegate,UITableViewDataSource>

/**
 *  上传参数
 */
@property (nonatomic,strong) RsApplyBillRequest *model;

/**
 *  数据源
 */
@property (nonatomic,strong) NSMutableArray *resultArray;

/**
 *  总页数
 */
@property (nonatomic,assign) NSInteger totalPage;

@end

@implementation WithdrawalViewController


#pragma mark - lazy

- (RsApplyBillRequest *)model
{
    if (!_model) {
        _model = [[RsApplyBillRequest alloc]init];
        DataPage *page = [[DataPage alloc]init];
        page.page = ONE;
        page.rows = KROWS;
        _model.page = page;
        _model.employeeId = [Shoppersmanager manager].shoppers.employee.fid;
    }
    return _model;
}

- (NSMutableArray *)resultArray
{
    if (!_resultArray) {
        _resultArray = [NSMutableArray array];
    }
    return _resultArray;
}


#pragma mark -获取数据

- (void)loadWebDataSource
{
    WS(weakSelf);
    if (self.pullPageIndex == ONE) {
        [self.resultArray removeAllObjects];
    }
    self.model.page.page = self.pullPageIndex;
    [self getDatasActionCompleted:^(RsApplyBillResponse *result) {
        if (weakSelf.pullPageIndex >= result.totalpages) {
            [weakSelf endRefresh:EndRefreshNotData];
        }else {
            [weakSelf endRefresh:EndRefreshDefault];
        }
    }];
}


#pragma mark - 获取提现进度

- (void)getDatasActionCompleted:(void(^)(RsApplyBillResponse *result))completed
{
    WS(weakSelf);
    [HTTP networkRequestWithURL:SERVERREQUESTURL(WITHDRAWALPROGRESS)  withRequestType:ZERO withParameter:self.model withReturnValueBlock:^(id returnValue) {
        if (RESULT(returnValue)) {
            RsApplyBillResponse *result = [[RsApplyBillResponse alloc]initWithDictionary:RESPONSE(returnValue) error:nil];
            completed(result);
            for (TOApplyBillEntity *entity in result.list) {
                [weakSelf.resultArray addObject:entity];
            }
            [weakSelf.tableView reloadData];
        }else
        {
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    }withFailureBlock:^(NSError *error) {
        [weakSelf endRefresh:EndRefreshDefault];
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

#pragma mark - <UITableViewDataSource,UITableViewDelegate>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WithdrawalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WithdrawalTableViewCell" forIndexPath:indexPath];
    TOApplyBillEntity *entity = self.resultArray[indexPath.row];
    cell.entity = entity;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.resultArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WithdrawalTableViewController *paymentsDetailsVC = [WithdrawalTableViewController viewControllerWithStoryBoardType:STORYBOARD_TYPE_MAIN];
    paymentsDetailsVC.model = self.resultArray[indexPath.row];
    [self pushViewController:paymentsDetailsVC animated:YES];
}

#pragma mark - 释放
- (void)dealloc
{
    [Notification removeObserver:self];
}


@end
