//
//  ContactsViewController.m
//  TianLing_App
//
//  Created by 刘荣毅 on 2017/10/20.
//  Copyright © 2017年 Liu. All rights reserved.
//
/**
 *Class Name:  通讯录
 */
#import "ContactsViewController.h"
#import "UIButton+ImageTitleStyle.h"
#import "ContactsTreeDataModels.h"
#import "JX_ContactPartmentViewController.h"
#import "JX_ContactSeachViewController.h"
#import "JX_SeachResultViewController.h"
#import "JX_ContactsCell.h"

@interface ContactsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic ,strong)NSMutableArray *imgArr;
@property(nonatomic ,strong)NSMutableArray *titleArr;

@property(nonatomic ,strong)ContactsTreeBaseClass *dataBaseModel;
@property(nonatomic ,strong)ContactsTreeData *dataModel;
@property(nonatomic ,strong)ContactsTreeDatas *contactDataModel;


@property(nonatomic ,strong)UITableView *tableView;

@end

@implementation ContactsViewController

static NSString *cellIdentifier = @"contactsCell";

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    // 请求联系人数据
    [self getContactsNetworkingRequest];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [self loadTestData];
    
//    [self getContactsNetworkingRequest];
    // 布置视图
    [self initUI];
    
  
    
}

-(void)loadTestData{
    self.titleArr = [NSMutableArray arrayWithArray:@[@"后勤部",@"产品部",@"销售部"]];
    self.imgArr  = [NSMutableArray arrayWithArray:@[@"wenjiajia",@"wenjiajia",@"wenjiajia"]];
}


-(void)initUI{
  self.navigationItem.title = @"通讯录";
    self.view.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
    
    
    /**
     搜索UI
     */
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame =  CGRectMake(7, 72, IPHONE_WIDTH-14, 29);
    if (kDevice_Is_iPhoneX) {
        searchBtn.frame =  CGRectMake(7, 72+24, IPHONE_WIDTH-14, 29);
    }
    searchBtn.backgroundColor = [UIColor whiteColor];
    [searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
    [searchBtn setTitleColor:[UIColor colorWithHexString:@"#808080"] forState:UIControlStateNormal];
    searchBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 6);
    searchBtn.layer.cornerRadius = 2;
    searchBtn.layer.masksToBounds = YES;
    [searchBtn setFont:[UIFont systemFontOfSize:14]];
    [searchBtn addTarget:self action:@selector(DidTapSearchBtnHandle) forControlEvents:UIControlEventTouchUpInside];
    [searchBtn setImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal];
    [searchBtn setButtonImageTitleStyle:ButtonImageTitleStyleLeft padding:10];
    [self.view addSubview:searchBtn];
    
    
    /**
     常用联系人UI
     */
//    UIView *commonContactsV = [[UIView alloc]initWithFrame:CGRectMake(0, 110, IPHONE_WIDTH, 63)];
    UIView *commonContactsV = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(searchBtn.frame)+8, IPHONE_WIDTH, 63)];
    commonContactsV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:commonContactsV];
    
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 42, 42)];
//    imgView.backgroundColor = [UIColor yellowColor];
    imgView.layer.cornerRadius=5;
    imgView.layer.masksToBounds = YES;
    imgView.image = [UIImage imageNamed:@"changyonglianxiren"];
    [commonContactsV addSubview:imgView];
    
    UILabel *commonConL = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imgView.frame)+10, 20, 150, 22)];
    commonConL.text = @"常用联系人";
    commonConL.font = [UIFont systemFontOfSize:15];
    commonConL.textColor = [UIColor colorWithHexString:@"#353535"];
    [commonContactsV addSubview:commonConL];
    
    [commonContactsV addTapBlock:^(id obj) {
        DLog(@"常用联系人");
        JX_ContactPartmentViewController *contactVC = [[JX_ContactPartmentViewController alloc]init];
        [self.navigationController pushViewController:contactVC animated:YES];
        contactVC.navTitleStr = @"常用联系人";
        contactVC.isCommon = YES;
    }];
    // 线条
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(imgView.frame.origin.x, CGRectGetMaxY(imgView.frame)+10, commonContactsV.frame.size.width-imgView.frame.origin.x, 1)];
    lineView.backgroundColor = [UIColor colorWithHexString:@"DCDEE0"];
    [commonContactsV addSubview:lineView];
    
    /**
     联系人
     */
//    UIView *comview = [[UIView alloc]initWithFrame:CGRectMake(0, 167, IPHONE_WIDTH, 23)];
//    comview.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
//    [self.view addSubview:comview];
//    //    view.backgroundColor = [UIColor redColor];
//    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(12, 5, 50, 13)];
//    label.text = @"联系人";
//    label.font = [UIFont systemFontOfSize:13];
//    label.textColor = [UIColor colorWithHexString:@"#9A9A9A"];
//    //    label.backgroundColor = [UIColor whiteColor];
//    [comview addSubview:label];
//
    

    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(commonContactsV.frame), IPHONE_WIDTH, IPHONE_HEIGHT-CGRectGetMaxY(commonContactsV.frame)-49) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    self.tableView.rowHeight = 51;
//    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"JX_ContactsCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
    [self.view addSubview:self.tableView];

    
}



#pragma mark- delegate && datasoure
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 63;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataModel.datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    UITableViewCell *cell = [[UITableViewCell alloc]init];
    
    // 定义唯一标识
//    static NSString *CellIdentifier = @"Cell";
    
    JX_ContactsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//    if (!cell) {
//        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//    }
//    else {
//        //当页面拉动的时候 当cell存在并且最后一个存在 把它进行删除就出来一个独特的cell我们在进行数据配置即可避免
//        while ([cell.contentView.subviews lastObject] != nil) {
//            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
//        }
//    }
//    //  左侧图片
//    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 42, 42)];
//    imgView.contentMode = UIViewContentModeScaleAspectFit;
//    imgView.layer.cornerRadius=5;
//    imgView.layer.masksToBounds = YES;
//    imgView.image = [UIImage imageNamed:@"wenjiajia"];
//    // 标题
//    UILabel *commonConL = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imgView.frame)+10, 20, IPHONE_WIDTH - CGRectGetMaxX(imgView.frame)-20, 22)];
//    //    commonConL.text = self.titleArr[indexPath.row];
//    commonConL.font = [UIFont systemFontOfSize:15];
//    commonConL.textColor = [UIColor colorWithHexString:@"#353535"];
    ContactsTreeDatas *dataModels = self.dataModel.datas[indexPath.row];
    cell.titleLB.text = dataModels.name;
    // 线条
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(imgView.frame.origin.x, CGRectGetMaxY(imgView.frame)+10, IPHONE_WIDTH-10, 1)];
//    view.backgroundColor = [UIColor colorWithHexString:@"DCDEE0"];
//
//
//    [cell addSubview:imgView];
//    [cell addSubview:commonConL];
//    [cell addSubview:view];

    
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH,0.1)];
//    view.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
////    view.backgroundColor = [UIColor redColor];
//    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(12, 5, 50, 13)];
//    label.text = @"联系人";
//    label.font = [UIFont systemFontOfSize:13];
//    label.textColor = [UIColor colorWithHexString:@"#9A9A9A"];
////    label.backgroundColor = [UIColor whiteColor];
//
//
//    [view addSubview:label];

    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH,45)];
    view.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    view.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(view.frame.size.width/2-25, 5, IPHONE_WIDTH, 25)];
    label.text = [NSString stringWithFormat: @"共%.0f人",self.dataModel.count];
    label.font = [UIFont systemFontOfSize:13];
    label.backgroundColor = [UIColor whiteColor];
    label.textColor = [UIColor colorWithHexString:@"#9A9A9A"];
    [view addSubview:label];
    
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 40;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    self.contactDataModel = self.dataModel.datas[indexPath.row];
    JX_ContactPartmentViewController *contactVC = [[JX_ContactPartmentViewController alloc]init];
    [self.navigationController pushViewController:contactVC animated:YES];
    contactVC.navTitleStr = self.contactDataModel.name;
    contactVC.isCommon = NO;
    contactVC.parentId = self.contactDataModel.datasIdentifier;
    DLog(@"数据 === %@",self.contactDataModel.name);
}



-(void)DidTapSearchBtnHandle{
    DLog(@"点击搜索");
    
//    JX_ContactSeachViewController * testVC = [JX_ContactSeachViewController new];
//
//    self.definesPresentationContext = YES; //self is presenting view controller
//    testVC.view.backgroundColor = [UIColor colorWithHexString:@"#999999" alpha:0.4];
//    testVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
//
//    [self presentViewController:testVC animated:NO completion:nil];
    
        JX_SeachResultViewController * testVC = [JX_SeachResultViewController new];
    
        self.definesPresentationContext = YES; //self is presenting view controller
        testVC.view.backgroundColor = [UIColor colorWithHexString:@"#999999" alpha:0.4];
        testVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
    
        [self presentViewController:testVC animated:NO completion:nil];
    
}



#pragma mark- networking
-(void)getContactsNetworkingRequest{
    
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"access_token"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"access_token"];
    parDic[@"parentId"] = @"";
    
    [HttpClient getWithURL:GETCONTACTS_URL parameter:parDic success:^(id JSON) {
       
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:JSON options:0 error:nil];
        
        self.dataBaseModel = [ContactsTreeBaseClass modelObjectWithDictionary:dict];
        self.dataModel = self.dataBaseModel.data;
        
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
        
        DLog(@"联系人 === %@",dict);
    } failure:^(NSError *error) {
        
    }];
    
    
}




@end
