// // PBRolePersonViewController.m // IphoneBIMe // // Created by zjf on 2021/1/6. // Copyright © 2021 ProBIM. All rights reserved. // #import "PBRolePersonViewController.h" #import "PBPersonVCTableViewCell.h" #import "PBMultiSelectPersonCell.h" #import "PBProjectModel.h" #import "PBUserModel.h" #import "PBUserCollectionViewCell.h" #import "PBSelectedPersonController.h" #import "PBAddExamineViewController.h" #import "PBCheckViewController.h" static NSString *const cellID = @"cellID"; static NSString *const MScellID = @"MScellID"; static NSString *collectionCell = @"collectionCell"; @interface PBRolePersonViewController () @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *dataList; @property (nonatomic, weak) UIView *bottomV; @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, weak) UIButton *determineBtn; @property (nonatomic, weak) UIButton *arrowBtn; @end @implementation PBRolePersonViewController - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; if (!self.isMultiSelect) { return; } NSMutableArray *list = [[NSMutableArray alloc] init]; for (NSDictionary *obj in self.dataList) { NSMutableArray *arrM = [[NSMutableArray alloc] init]; for (PBUserModel *model in [obj valueForKey:@"Users"]) { for (PBUserModel *selectModel in self.selectList) { if ([model.UserId isEqualToString:selectModel.UserId]) { model.isSelect = YES; break; }else{ model.isSelect = NO; } } [arrM addObject:model]; } NSDictionary *dict = @{@"FirstLetter":[obj valueForKey:@"FirstLetter"], @"Users":arrM.copy}; [list addObject:dict]; } self.dataList = list; [self.tableView reloadData]; if (self.selectList.count > 0) { self.bottomV.hidden = NO; self.arrowBtn.hidden = NO; self.collectionView.hidden = NO; self.determineBtn.hidden = NO; [self.determineBtn setTitle:[NSString stringWithFormat:@"确定(%zd)",self.selectList.count] forState:UIControlStateNormal]; [self.collectionView reloadData]; }else { self.bottomV.hidden = YES; self.arrowBtn.hidden = YES; self.collectionView.hidden = YES; self.determineBtn.hidden = YES; } } - (void)viewDidLoad { [super viewDidLoad]; [self setupNav]; [self setupUI]; [self loadData]; } - (void)setupNav { self.title = @"请指定人员"; UIBarButtonItem *backNavItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Doc_preview_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backItemAction)]; self.navigationItem.leftBarButtonItem = backNavItem; } - (void)backItemAction { [self.navigationController popViewControllerAnimated:YES]; } - (void)setupUI { self.view.backgroundColor = PBColor(244, 245, 246); self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; self.tableView.backgroundColor = PBColor(244, 245, 246); self.tableView.dataSource = self; self.tableView.delegate = self; self.tableView.bounces = NO; self.tableView.rowHeight = 56; self.tableView.sectionIndexColor = PBColor(97, 111, 125); if (@available(iOS 15.0, *)) { self.tableView.sectionHeaderTopPadding = 0; }; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self.tableView registerClass:[PBPersonVCTableViewCell class] forCellReuseIdentifier:cellID]; [self.tableView registerClass:[PBMultiSelectPersonCell class] forCellReuseIdentifier:MScellID]; self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 56, 0); [self.view addSubview:self.tableView]; CGFloat bottomH = 0.f; if (IS_IPHONE_X) { bottomH = 0.f + IPHONE_X_BOTTOM; } [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view); make.left.right.equalTo(self.view); make.bottom.equalTo(self.view).offset(-(bottomH)); }]; // self.tableView.tableHeaderView = [self setupTableHearderView]; UIView *bottomV = [[UIView alloc] init]; bottomV.backgroundColor = [UIColor whiteColor]; [self.view addSubview:bottomV]; [bottomV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.bottom.right.equalTo(self.tableView); make.height.equalTo(@54); }]; UIButton *arrowBtn = [UIButton z_bgImageButton:[UIImage imageNamed:@"arrow_top"]]; [arrowBtn addTarget:self action:@selector(arrowTopActon) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:arrowBtn]; [arrowBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(bottomV).offset(15); make.left.equalTo(bottomV).offset(12); make.size.mas_equalTo(CGSizeMake(24, 24)); }]; UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout.alloc init]; layout.itemSize = CGSizeMake(34, 34); layout.minimumLineSpacing = 10; layout.minimumInteritemSpacing = 0; layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; self.collectionView = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout]; [self.collectionView registerClass:[PBUserCollectionViewCell class] forCellWithReuseIdentifier:collectionCell]; self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.bounces = NO; self.collectionView.showsHorizontalScrollIndicator = NO; self.collectionView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.collectionView]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(bottomV).mas_offset(10); make.left.equalTo(bottomV).offset(46); make.right.equalTo(bottomV).offset(-120); make.height.equalTo(@34); }]; UIButton *determineBtn = [UIButton z_textButton:[NSString stringWithFormat:@"确定(%zd)",self.selectList.count] boldFontSize:14 normalColor:[UIColor whiteColor]]; [determineBtn addTarget:self action:@selector(determineBtnAction) forControlEvents:UIControlEventTouchUpInside]; determineBtn.backgroundColor = PBColor(0, 122, 255); [self.view addSubview:determineBtn]; [determineBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(bottomV).offset(9); make.right.equalTo(bottomV).offset(-20); make.size.mas_equalTo(CGSizeMake(89, 34)); }]; [determineBtn circleViewWithRadius:6]; self.arrowBtn = arrowBtn; self.bottomV = bottomV; self.determineBtn = determineBtn; if (self.selectList.count > 0 && self.isMultiSelect) { self.bottomV.hidden = NO; self.arrowBtn.hidden = NO; self.collectionView.hidden = NO; self.determineBtn.hidden = NO; [self.collectionView reloadData]; }else { self.bottomV.hidden = YES; self.arrowBtn.hidden = YES; self.collectionView.hidden = YES; self.determineBtn.hidden = YES; } } - (void)arrowTopActon { PBSelectedPersonController *selectPersonVC = [[PBSelectedPersonController alloc] init]; selectPersonVC.selectList = self.selectList; [self.navigationController pushViewController:selectPersonVC animated:YES]; } - (void)determineBtnAction { [PBNoteCenter postNotificationName:PBNoteCenterUpdatePerson object:self.selectList.copy]; [self backAction]; } - (UIView *)setupTableHearderView { UIView *bgV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 52)]; bgV.backgroundColor = PBColor(244, 245, 246); UIButton *searchBtn = [UIButton z_bgImageButton:[UIImage imageNamed:@"Examine_search_background"]]; [searchBtn addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside]; [bgV addSubview:searchBtn]; [searchBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(bgV).offset(8); make.left.equalTo(bgV).offset(10); make.right.equalTo(bgV).offset(-10); make.height.equalTo(@36); }]; return bgV; } - (void)searchAction { } - (void)loadData { [YJProgressHUD showProgress:@"" inView:self.view]; [[PBNetworkTools sharedTools] GetProjectUserSortByLetterRoleId:self.roleID andOrganizeID:self.projectModel.organizeid andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { [YJProgressHUD showMessage:@"加载成员信息失败" inView:self.view]; return; } NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; NSLog(@"%@",str); PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str]; if (networkModel.Ret == 1) { [YJProgressHUD hide]; NSArray *arr = [networkModel.Data valueForKey:@"List"]; for (NSDictionary *obj in arr) { NSMutableArray *arrM = [[NSMutableArray alloc] init]; for (NSDictionary *user in [obj valueForKey:@"Users"]) { PBUserModel *model = [PBUserModel yy_modelWithDictionary:user]; for (PBUserModel *selectModel in self.selectList) { if ([model.UserId isEqualToString:selectModel.UserId]) { model.isSelect = YES; break; }else { model.isSelect = NO; } } [arrM addObject:model]; } NSDictionary *dict = @{@"FirstLetter":[obj valueForKey:@"FirstLetter"], @"Users":arrM.copy}; [self.dataList addObject:dict]; } [self.tableView reloadData]; }else { [YJProgressHUD showMessage:networkModel.Msg inView:self.view]; } }]; } #pragma mark - UITableViewDelegate, UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.dataList.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSDictionary *dict = self.dataList[section]; NSArray *arr = [dict valueForKey:@"Users"]; return arr.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (self.isMultiSelect) { PBMultiSelectPersonCell *cell = [tableView dequeueReusableCellWithIdentifier:MScellID forIndexPath:indexPath]; cell.selectionStyle = UITableViewCellSelectionStyleNone; NSDictionary *dict = self.dataList[indexPath.section]; NSArray *arr = [dict valueForKey:@"Users"]; cell.userModel = arr[indexPath.row]; return cell; }else { PBPersonVCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; cell.selectionStyle = UITableViewCellSelectionStyleNone; NSDictionary *dict = self.dataList[indexPath.section]; NSArray *arr = [dict valueForKey:@"Users"]; cell.userModel = arr[indexPath.row]; return cell; } } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 16; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { //最后一个 return 0.01; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { return nil; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *bgView = [[UIView alloc]init]; bgView.backgroundColor = PBColor(244, 245, 246); NSDictionary *dict = self.dataList[section]; UILabel *nameL = [UILabel z_labelWithText:[dict valueForKey:@"FirstLetter"] Color:PBColor(97, 111, 125) isBold:YES Font:12]; [bgView addSubview:nameL]; [nameL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.bottom.equalTo(bgView); make.left.equalTo(bgView).offset(20); }]; return bgView; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *dict = self.dataList[indexPath.section]; NSArray *arr = [dict valueForKey:@"Users"]; PBUserModel *userModel = arr[indexPath.row]; if (self.isMultiSelect) { if (userModel.isSelect) { userModel.isSelect = NO; for (PBUserModel *model in self.selectList) { if ([model.UserId isEqualToString:userModel.UserId]) { [self.selectList removeObject:model]; break; } } }else { userModel.isSelect = YES; [self.selectList addObject:userModel]; } if (self.selectList.count > 0) { self.bottomV.hidden = NO; self.arrowBtn.hidden = NO; self.collectionView.hidden = NO; self.determineBtn.hidden = NO; [self.determineBtn setTitle:[NSString stringWithFormat:@"确定(%zd)",self.selectList.count] forState:UIControlStateNormal]; }else { self.bottomV.hidden = YES; self.arrowBtn.hidden = YES; self.collectionView.hidden = YES; self.determineBtn.hidden = YES; } [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; [self.collectionView reloadData]; }else { [PBNoteCenter postNotificationName:PBNoteCenterUpdatePerson object:@[userModel]]; [self backAction]; } } - (void)backAction{ UINavigationController *navVC = self.navigationController; NSMutableArray *viewControllers = [[NSMutableArray alloc] init]; for (UIViewController *vc in [navVC viewControllers]) { [viewControllers addObject:vc]; if ([vc isKindOfClass:[PBAddExamineViewController class]]) { break; } if ([vc isKindOfClass:[PBCheckViewController class]]) { break; } } [navVC setViewControllers:viewControllers animated:YES]; } #pragma mark 右侧索引 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { NSMutableArray *arrM = [[NSMutableArray alloc] init]; for (NSDictionary *dict in self.dataList) { [arrM addObject:[dict valueForKey:@"FirstLetter"]]; }; return arrM.copy; } #pragma mark - UICollectionViewDelegate, UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.selectList.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ PBUserCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionCell forIndexPath:indexPath]; cell.userModel = self.selectList[indexPath.item]; return cell; } - (void)setProjectModel:(PBProjectModel *)projectModel { _projectModel = projectModel; } - (void)setRoleID:(NSString *)roleID { _roleID = roleID; } - (void)setIsMultiSelect:(BOOL)isMultiSelect { _isMultiSelect = isMultiSelect; } - (NSMutableArray *)dataList { if (_dataList == nil) { _dataList = [[NSMutableArray alloc] init]; } return _dataList; } - (void)setSelectList:(NSMutableArray *)selectList { _selectList = selectList; } /* #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