//
//  SaleInputListViewController.m
//  RealEstateManagement
//
//  Created by Javen on 2016/11/2.
//  Copyright © 2016年 上海勾芒信息科技. All rights reserved.
//

#import "SaleInputListViewController.h"
#import "SaleInputAddViewController.h"
#import "SaleInputListTableViewCell.h"
#import "HMSaleInputDetail.h"
#import "SaleInputDetailViewController.h"
#import "HMSaleInputQuery.h"
#import "JSDropDownMenu.h"
#import "SaleInputListViewModel.h"
#import "SaleInputHistoryViewController.h"
@interface SaleInputListViewController ()

@property (strong, nonatomic) SaleInputListViewModel *viewModel;

@end

@implementation SaleInputListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.startPage = 1;
    // Do any additional setup after loading the view.
    self.title = @"销售录入记录";
    [self refresh];
    self.tableView.rowHeight = 159;
    UIBarButtonItem *btnRight = [[UIBarButtonItem alloc] initWithTitle:@"历史" style:UIBarButtonItemStyleDone target:self action:@selector(actionGoHistory)];
    self.navigationItem.rightBarButtonItem = btnRight;
    self.btnAdd.hidden = !AppGlobal.permission.saleinput.newField;
    [self configDropMenu];
}

- (void)configDropMenu {
    JSDropDownMenu *menu = [[JSDropDownMenu alloc] initWithOrigin:CGPointMake(0, 0) andHeight:45];
    menu.indicatorColor = [UIColor colorWithRed:175.0f / 255.0f green:175.0f / 255.0f blue:175.0f / 255.0f alpha:1.0];
    menu.separatorColor = [UIColor colorWithRed:210.0f / 255.0f green:210.0f / 255.0f blue:210.0f / 255.0f alpha:1.0];
    menu.textColor = [UIColor colorWithRed:83.f / 255.0f green:83.f / 255.0f blue:83.f / 255.0f alpha:1.0f];
    menu.dataSource = self.viewModel;
    menu.delegate = self.viewModel;
    [self.view addSubview:menu];
    
    WS(weakSelf);
    self.viewModel.blockRequest = ^{
        weakSelf.page = weakSelf.startPage;
        [weakSelf httpRequest];
    };
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.barTintColor = kMainBlueColor;
}

- (void)httpRequest {
    self.viewModel.query.page = @(self.page);
    self.viewModel.query.pageSize = @(self.pageSize);
    WS(weakSelf);
    [MBProgressHUD j_loading];
    [ZJHttpManager POST:@"hdmall/salesinput/query" parameters:self.viewModel.query.toDictionary complete:^(id responseObject, NSError *error) {
        [MBProgressHUD j_hideLoadingView];
        if (kIsResponse) {
            if (weakSelf.page == 1) {
                [weakSelf.arrData removeAllObjects];
            }
            
            NSMutableArray *arr = [HMSaleInputDetail modelsFromArray:responseObject[@"data"][@"records"]];
            [weakSelf.arrData addObjectsFromArray:arr];
            
        }else{
            kFalseHttpTips;
            self.page--;
        }
        
        [weakSelf listTableViewReloadData];
    }];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SaleInputListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SaleInputListTableViewCell" forIndexPath:indexPath];
    [cell configCellWithArray:self.arrData indexPath:indexPath];
    return cell;
}

- (void)listDidSelect:(id)model {
    HMSaleInputDetail *listModel = model;
    SaleInputDetailViewController *detailVC = [SaleInputDetailViewController viewControllerWithStoryBoardType:STORYBOARD_TYPE_SALEINPUT];
    detailVC.listModel = listModel;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)actionAdd:(UIButton *)sender {
    SaleInputAddViewController *addVC = [SaleInputAddViewController viewControllerWithStoryBoardType:STORYBOARD_TYPE_SALEINPUT];
    WS(weakSelf);
    addVC.commplete = ^{
        weakSelf.page = weakSelf.startPage;
        [weakSelf httpRequest];
    };
    [self listPushCustomAnimate:addVC];
}

/**
 *  跳转查看历史界面
 */
- (void)actionGoHistory {
    SaleInputHistoryViewController *historyVC = [SaleInputHistoryViewController viewControllerWithStoryBoardType:(STORYBOARD_TYPE_SALEINPUT)];
    if (!AppGlobal.isMall) {
        historyVC.contract = [HMSaleInputDetail_contract modelWithDic:[AppGlobal getTenantContract].toDictionary];
    }
    [self.navigationController pushViewController:historyVC animated:YES];
}

ZJLazy(SaleInputListViewModel, viewModel);

@end
