//
//  BaseChooseViewController.m
//  RealEstateManagement
//
//  Created by Javen on 16/9/7.
//  Copyright © 2016年 上海勾芒信息科技. All rights reserved.
//

#import "BaseChooseViewController.h"
#import "UIScrollView+EmptyDataSet.h"
@interface BaseChooseViewController ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@end

@implementation BaseChooseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.tableFooterView = [UIView new];
    self.navigationTitle.text = self.navTitle;
    self.page = 0;
    self.pageSize = 15;
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

/**
 *  启用分页加载（最好在ViewDidLoad里面调用）
 */
- (void)paggingMode {
    self.isPageMode = YES;
    WS(weakSelf);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 0;
        [weakSelf.arrData removeAllObjects];
        [weakSelf httpRequest];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.page++;
        [weakSelf httpRequest];
    }];
//    self.tableView.mj_footer.hidden = YES;
}

/**
 *  请求数据要重写这个方法，在这个方法里面请求数据，获取数据后调用showData方法，展示获取到的数据
 */
- (void)httpRequest {
    CLog(@"httpRequest 方法需要重写");
}
#pragma mark - searchbar delegate

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    searchBar.showsCancelButton = YES;
    return YES;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    searchBar.showsCancelButton = NO;
    searchBar.text = nil;
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    self.displayDataArr = self.arrData;
    self.tableView.mj_header.hidden = NO;
    self.tableView.mj_footer.hidden = NO;
    [self.tableView reloadData];
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    searchBar.showsCancelButton = NO;
    self.displayDataArr = self.arrData;
    [self.tableView reloadData];
    return YES;
}

/**
 *  如果不需要本地搜索，则重写这个方法
 */
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if (self.isPageMode) {
        return;
    }
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"zj_displayName contains [cd] %@", searchText];
    self.displayDataArr = [self.arrData filteredArrayUsingPredicate:pred];
    if (searchText.length < 1) {
        self.displayDataArr = self.arrData;
    }
    [self.tableView reloadData];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"chooseCell" forIndexPath:indexPath];
    BaseChooseModel *model = self.displayDataArr[indexPath.row];
    cell.textLabel.text = model.zj_displayName;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    id model = self.displayDataArr[indexPath.row];
    [self.navigationController popViewControllerAnimated:YES];
    self.blockChoose(model);
}

- (void)showData{
    self.displayDataArr = self.arrData;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    
    [self.tableView reloadData];
    
    //停止刷新的逻辑
    if (self.tableView.mj_header.isRefreshing) {
        //下拉刷新
        [self.tableView.mj_header endRefreshing];
        
    } else if (self.tableView.mj_footer.isRefreshing) {
        [self.tableView.mj_footer endRefreshing];
    }
    
    if (self.arrData.count < self.pageSize * (self.page + 1)) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } else {
        [self.tableView.mj_footer resetNoMoreData];
    }
    
    if (self.arrData.count > 0 && self.page == 0) {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    }
}

- (void)refresh {
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - empty state
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"list_nodata"];
}

- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: @"transform"];
    
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0)];
    
    animation.duration = 1;
    animation.cumulative = YES;
    animation.repeatCount = MAXFLOAT;
    
    return animation;
}

- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIColor whiteColor];
}

- (void)disableRefresh {
    self.tableView.mj_header.hidden = YES;
    self.tableView.mj_footer.hidden = YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)actionBac:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSMutableArray *)arrData {
    if (!_arrData) {
        _arrData = [NSMutableArray array];
    }
    return _arrData;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
