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

#import "PaymentsViewController.h"
#import "PaymentsTableViewCell.h"
#import "PaymentsDetailsTableViewController.h"


@interface PaymentsViewController ()<UITableViewDelegate,UITableViewDataSource>

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

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

@end

@implementation PaymentsViewController


#pragma mark - lazy
- (RsCommissionRequest *)model
{
    if (!_model) {
        _model = [[RsCommissionRequest alloc]init];
        DataPage *page = [[DataPage alloc]init];
        page.page = ONE;
        page.rows = KROWS;
        _model.page = page;
        _model.emploreeId = [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:^(RsCommissionResponse *result) {
        if (weakSelf.pullPageIndex >= result.totalpages) {
            [weakSelf endRefresh:EndRefreshNotData];
        }else {
            [weakSelf endRefresh:EndRefreshDefault];
        }
    }];
}


#pragma mark - 获取收支明细
- (void)getDatasActionCompleted:(void(^)(RsCommissionResponse *result))completed
{
    WS(weakSelf);
    [HTTP networkRequestWithURL:SERVERREQUESTURL(PAYMENTS)  withRequestType:ZERO withParameter:self.model withReturnValueBlock:^(id returnValue) {
        if (RESULT(returnValue)) {
            RsCommissionResponse *result = [[RsCommissionResponse alloc]initWithDictionary:RESPONSE(returnValue) error:nil];
            completed(result);
            for (ApplyHist *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
{
    PaymentsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PaymentsTableViewCell" forIndexPath:indexPath];
    ApplyHist *entity = self.resultArray[indexPath.row];
    cell.createTimeLabel.text = entity.createDate;
    cell.moneyTypeLabel.text = [[self class] separatePaymentsAndWithfrawal:entity.amount withType:entity.type];
    NSString *string = ([entity.amount floatValue] > 0)?[NSString stringWithFormat:@"+%.2f",[entity.amount floatValue]]:[NSString stringWithFormat:@"%.2f",[entity.amount floatValue]];
    cell.amountLabel.text = string;
    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];
    PaymentsDetailsTableViewController *paymentsDetailsVC = [PaymentsDetailsTableViewController viewControllerWithStoryBoardType:STORYBOARD_TYPE_MAIN];
    paymentsDetailsVC.model = self.resultArray[indexPath.row];
    [self pushViewController:paymentsDetailsVC animated:YES];
}

@end
