//
//  InviterViewController.m
//  Lighting
//
//  Created by 曹云霄 on 2017/7/25.
//  Copyright © 2017年 上海勾芒科技有限公司. All rights reserved.
//

#import "InviterViewController.h"
#import "InviterTableViewCell.h"
#import "CustomVDesignerEntity.h"

@interface InviterViewController ()


/**
 邀请人
 */
@property (nonatomic,strong) NSMutableArray *inviterArray;

/**
 搜索显示邀请人
 */
@property (nonatomic,strong) NSArray *displayArray;

/**
 选中邀请人
 */
@property (nonatomic,strong) NSIndexPath *indexPath;

@end

@implementation InviterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

#pragma mark -下拉刷新
- (void)loadWebDataSource
{
    WS(weakSelf);
    [self.inviterArray removeAllObjects];
    self.displayArray = @[];
    [self getInviterListcompleted:^(DesignerPageResult *result) {
        if (weakSelf.pullPageIndex >= result.totalpages) {
            [weakSelf endRefresh:EndRefreshNotData];
        }else {
            [weakSelf endRefresh:EndRefreshDefault];
        }
    }];
}

#pragma mark -请求数据
- (void)getInviterListcompleted:(void(^)(DesignerPageResult *result))completed
{
    WS(weakSelf);
    NSString *urlString = [NSString stringWithFormat:SERVERREQUESTURL(GETINVITER),[Shoppersmanager manager].shoppers.employee.currentDepart.fid];
    [HTTP networkRequestWithURL:urlString  withRequestType:ONE withParameter:nil withReturnValueBlock:^(id returnValue) {
        if (RESULT(returnValue)) {
            DesignerPageResult *customerResult = [[DesignerPageResult alloc]initWithDictionary:RESPONSE(returnValue) error:nil];
            completed(customerResult);
            for (VDesignerEntity *objc in customerResult.results) {
                CustomVDesignerEntity *entity = [[CustomVDesignerEntity alloc] initWithDictionary:[objc toDictionary] error:nil];
                [weakSelf.inviterArray addObject:entity];
            }
            weakSelf.displayArray = [NSMutableArray arrayWithArray:weakSelf.inviterArray];
            [weakSelf.tableView reloadData];
        }
        else
        {
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    }withFailureBlock:^(NSError *error) {
        [weakSelf endRefresh:EndRefreshDefault];
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

#pragma mark -取消
- (IBAction)cancelButtonItemClickAction:(UIBarButtonItem *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -确认
- (IBAction)sureButtonItemClickAction:(UIBarButtonItem *)sender {
    if (!self.indexPath) {
        return;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
    if (self.selectedBlock) {
        self.selectedBlock(self.displayArray[self.indexPath.row]);
    }
}

#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.displayArray = [NSArray arrayWithArray:self.inviterArray];
    [self.tableView reloadData];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"realName contains [cd] %@ OR userName contains [cd] %@", searchText, searchText];
    self.displayArray = [self.inviterArray filteredArrayUsingPredicate:pred];
    if (searchText.length < 1) {
        self.displayArray = [NSArray arrayWithArray:self.inviterArray];
    }
    [self.tableView reloadData];
}


#pragma mark -<UITableVieDelegate,UITableVieDataSource>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InviterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InviterTableViewCell" forIndexPath:indexPath];
    cell.entity = self.displayArray[indexPath.row];
    return cell;
}

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

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

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    for (InviterTableViewCell *cell in tableView.visibleCells) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    for (CustomVDesignerEntity *entity in self.inviterArray) {
        entity.selectedState = NO;
    }
    CustomVDesignerEntity *entity = self.inviterArray[indexPath.row];
    InviterTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    entity.selectedState = YES;
    self.indexPath = indexPath;
}


#pragma mark -lazy
- (NSMutableArray *)inviterArray
{
    if (!_inviterArray) {
        _inviterArray = [NSMutableArray array];
    }
    return _inviterArray;
}






@end
