// // PBEnterpriseCodeController.m // IphoneBIMe // // Created by zjf on 2020/6/12. // Copyright © 2020 ProBIM. All rights reserved. // #import "PBEnterpriseCodeController.h" #import "PBCodeTableViewCell.h" #import "PBInputCodeTableViewCell.h" static NSString *const CellID = @"CellID"; static NSString *const InputCellID = @"InputCellID"; @interface PBEnterpriseCodeController () @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *dataList; @property (nonatomic, weak) UILabel *codeTieleL; @property (nonatomic, weak) UIView *codeBgV; @property (nonatomic, strong) NSMutableDictionary *inputCellDict; @property (nonatomic, assign) NSInteger currentSelectIndex; @end @implementation PBEnterpriseCodeController - (void)viewDidLoad { [super viewDidLoad]; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.f) { self.edgesForExtendedLayout = UIRectEdgeNone; } NSArray *arr = [NSString getApiArrData]; self.dataList = arr.mutableCopy; [self setupUI]; } - (void)setupUI { self.view.backgroundColor = PromptColor; self.title = @"企业服务"; UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(cancelAction)]; self.navigationItem.leftBarButtonItem = leftItem; UIView *topView = [self creatHeadView]; self.tableView.backgroundColor = PromptColor; self.tableView.rowHeight = 166; [self.tableView registerClass:[PBCodeTableViewCell class] forCellReuseIdentifier:CellID]; [self.tableView registerClass:[PBInputCodeTableViewCell class] forCellReuseIdentifier:InputCellID]; // self.tableView.delegate = self; // self.tableView.dataSource = self; self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(topView.mas_bottom); make.bottom.equalTo(self.view); make.left.equalTo(self.view).offset(20); make.right.equalTo(self.view).offset(-20); }]; // self.tableView = [[UITableView alloc] init]; // self.tableView.backgroundColor = PromptColor; // self.tableView.rowHeight = 166; // [self.tableView registerClass:[PBCodeTableViewCell class] forCellReuseIdentifier:CellID]; // [self.tableView registerClass:[PBInputCodeTableViewCell class] forCellReuseIdentifier:InputCellID]; // self.tableView.delegate = self; // self.tableView.dataSource = self; // self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; // self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // [self.view addSubview:self.tableView]; // [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { // make.top.equalTo(topView.mas_bottom); // make.left.right.bottom.equalTo(self.view); // }]; // self.tableView.contentInset = UIEdgeInsetsMake(6, 0, 0, 0); } - (void)cancelAction { [self dismissViewControllerAnimated:YES completion:nil]; } #pragma mark - UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataList.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == (self.dataList.count - 1)) { PBInputCodeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:InputCellID forIndexPath:indexPath]; cell.dict = self.inputCellDict; cell.GetEnterpriseBlock = ^(NSString * _Nonnull code) { [self getEnterpriseWithCode:code]; }; cell.SolveErr = ^{ [self.inputCellDict setValue:@"" forKey:@"code"]; [self.inputCellDict setValue:@"" forKey:@"err"]; }; return cell; }else { PBCodeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID forIndexPath:indexPath]; cell.dict = self.dataList[indexPath.row + 1]; return cell; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == (self.dataList.count - 1)) { return; } if (self.UpdataCodeBlock) { self.UpdataCodeBlock(indexPath.row + 1); } NSArray *arr = [NSString getApiArrData]; self.dataList = arr.mutableCopy; [self setHeaderViewCheck]; [self.tableView reloadData]; } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == self.dataList.count - 1) { return NO; } NSDictionary *dict = self.dataList[indexPath.row + 1]; if ([[dict valueForKey:@"Check"] isEqualToString:@"1"]) { return NO; } return YES; } - ( UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath { UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) { [self.dataList removeObjectAtIndex:indexPath.row + 1]; completionHandler (YES); [self.tableView reloadData]; if (self.DeleteCodeBlock) { self.DeleteCodeBlock(indexPath.row + 1); } }]; deleteRowAction.image = [UIImage imageNamed:@"code_delete"]; deleteRowAction.backgroundColor = PromptColor; UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]]; return config; } - (void)selectDefault:(UITapGestureRecognizer *)tap { if (self.UpdataCodeBlock) { self.UpdataCodeBlock(0); } NSArray *arr = [NSString getApiArrData]; self.dataList = arr.mutableCopy; [self setHeaderViewCheck]; [self.tableView reloadData]; } - (void)getEnterpriseWithCode:(NSString *)code { if ([code isEqualToString:@""]) { return; } [YJProgressHUD showProgress:@"" inView:self.view]; [[PBNetworkTools sharedTools] RequestGetUrlsByCodeWithCode:code andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { NSLog(@"%@",error); [YJProgressHUD showMessage:@"设置企业编码失败" inView:self.view]; [self codeErrWith:code]; return; } NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str]; if (networkModel.Ret == 1) { [YJProgressHUD hide]; NSDictionary *dict = networkModel.Data; NSMutableDictionary *dictM = dict.mutableCopy; [dictM setObject:code forKey:@"Code"]; [dictM setObject:@"1" forKey:@"Check"]; if (self.SetCodeBlock) { self.SetCodeBlock(code, dictM.copy); } NSArray *arr = [NSString getApiArrData]; self.dataList = arr.mutableCopy; [self setHeaderViewCheck]; [self.tableView reloadData]; }else { [YJProgressHUD showMessage:@"输入企业编码有误" inView:self.view]; [self codeErrWith:code]; } }]; } - (void)codeErrWith:(NSString *)errCode { [self.inputCellDict setValue:errCode forKey:@"code"]; [self.inputCellDict setValue:@"err" forKey:@"err"]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(self.dataList.count - 1) inSection:0]; [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } - (UIView *)creatHeadView { UIView *bgV = [[UIView alloc] init]; bgV.backgroundColor = PromptColor; [self.view addSubview:bgV]; [bgV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.right.equalTo(self.view); make.height.equalTo(@232); }]; UIView *codeBgV = [[UIView alloc] init]; codeBgV.backgroundColor = [UIColor whiteColor]; [bgV addSubview:codeBgV]; [codeBgV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(bgV).offset(20); make.left.equalTo(bgV).offset(20); make.right.equalTo(bgV).offset(-20); make.bottom.equalTo(bgV).offset(-60); }]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectDefault:)]; codeBgV.userInteractionEnabled = YES; [codeBgV addGestureRecognizer:tap]; [codeBgV circleViewWithRadius:6]; UIImageView *logoImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo_unknown"]]; [bgV addSubview:logoImageV]; [logoImageV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(codeBgV).offset(26); make.centerX.equalTo(codeBgV); make.size.mas_equalTo(CGSizeMake(36, 36)); }]; UIImageView *checkIamgeV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"code_check"]]; [bgV addSubview:checkIamgeV]; [checkIamgeV mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(codeBgV); make.right.equalTo(codeBgV).offset(-20); make.size.mas_equalTo(CGSizeMake(24, 24)); }]; UILabel *codeTieleL = [UILabel z_labelWithText:@"管理平台" Color:TitleColor isBold:YES Font:TitleFontSize]; codeTieleL.textAlignment = NSTextAlignmentCenter; [bgV addSubview:codeTieleL]; [codeTieleL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(logoImageV.mas_bottom).offset(26); make.left.equalTo(codeBgV).offset(20); make.right.equalTo(codeBgV).offset(-20); make.height.equalTo(@18); }]; UILabel *theirOwnL = [UILabel z_labelWithText:@"自有企业服务" Color:TitleColor isBold:YES Font:13]; theirOwnL.textAlignment = NSTextAlignmentCenter; [bgV addSubview:theirOwnL]; [theirOwnL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(codeBgV.mas_bottom).offset(30); make.size.mas_equalTo(CGSizeMake(84, 16)); make.centerX.equalTo(codeBgV); }]; UIView *leftLin = [[UIView alloc] init]; leftLin.backgroundColor = PBColor(233, 235, 237); [bgV addSubview:leftLin]; [leftLin mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(theirOwnL); make.left.equalTo(codeBgV); make.right.equalTo(theirOwnL.mas_left).offset(-15); make.height.equalTo(@1); }]; UIView *rightLin = [[UIView alloc] init]; rightLin.backgroundColor = PBColor(233, 235, 237); [bgV addSubview:rightLin]; [rightLin mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(theirOwnL); make.left.equalTo(theirOwnL.mas_right).offset(15); make.right.equalTo(codeBgV); make.height.equalTo(@1); }]; self.codeBgV = codeBgV; self.codeTieleL = codeTieleL; [self setHeaderViewCheck]; return bgV; } - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; UITableViewController *tvc = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain]; [self addChildViewController:tvc]; _tableView = tvc.tableView; _tableView.delegate = self; _tableView.dataSource = self; if (@available(iOS 15.0, *)) { _tableView.sectionHeaderTopPadding = 0; } [self.view addSubview:_tableView]; } return _tableView; } - (void)setHeaderViewCheck { if (self.dataList.count > 0) { NSDictionary *dict = self.dataList[0]; NSString *check = [dict valueForKey:@"Check"]; if ([check isEqualToString:@"1"]) { self.codeBgV.backgroundColor = PBColor(0,122, 255); self.codeTieleL.textColor = [UIColor whiteColor]; }else { self.codeBgV.backgroundColor = [UIColor whiteColor]; self.codeTieleL.textColor = PBColor(40, 58, 79); } } } - (NSMutableDictionary *)inputCellDict { if (_inputCellDict == nil) { _inputCellDict = [[NSMutableDictionary alloc] init]; } return _inputCellDict; } /* #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