// // XueShiCheckList.m // LNManager // // Created by EchoShacolee on 2017/4/9. // Copyright © 2017年 lee. All rights reserved. // #import "XueShiCheckList.h" #import "XueShiCheckCell.h" #import "MengBanView.h" #import "MJRefresh.h" #import "TeachLogForCheckVC.h" @interface XueShiCheckList () { MengBanView * _mengBanView; //请求参数 NSString * _kemu; NSString * _state; NSString *_stuName; NSInteger _currentPageNum;//记录页书 UISearchController *_searchController; //正在加载的状态 BOOL _IS_LOADING; //加载数据的类型 MyGetDataType _getDataType; NSMutableArray *_selectedIndexPathes;//记录选中的cell UIButton * _notifyBtn;//审核学时 UIButton *_allBtn; UIButton *_cancelBtn; BOOL _isEditing; } @end @implementation XueShiCheckList - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"学时审核"; if ([self.type isEqualToString:@"1"]) { [self changeBckBtn]; } //实例化 _kemu = @""; _state = @"待审核"; _stuName = @""; _currentPageNum = 0; _getDataType = MyGetDataTypeNomal; _IS_LOADING = NO; _selectedIndexPathes = [[NSMutableArray alloc] init]; _isEditing = NO; [self createSelectBtn]; [self setRefreshAction];//设置刷新 [self customRightBtn];//设置筛选 [self creatSearchVC];//添加搜索栏 [self getData]; __weak typeof(self) weakSelf = self; self.block = ^{ _currentPageNum = 0; [weakSelf getData]; }; } -(void)viewDidDisappear:(BOOL)animated{ [super viewDidDisappear:animated]; if (_allBtn) { [_allBtn removeFromSuperview]; } if (_cancelBtn) { [_cancelBtn removeFromSuperview]; } // if (_notifyBtn) { // [_notifyBtn removeFromSuperview]; // } //这个一定要写 要不然会在退出去下一个页面时候 搜索栏有一定时间的存留 [self.view endEditing:YES]; if (_searchController.active) { _searchController.active = NO; if (_searchController.searchBar && _searchController.searchBar.superview) { [_searchController.searchBar removeFromSuperview]; } } } -(void)changeBckBtn{ UIBarButtonItem* backBbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnAction)]; [backBbi setTintColor:COLOR_THEME]; [self.navigationItem setLeftBarButtonItem:backBbi]; } -(void)backBtnAction{ [self dismissViewControllerAnimated:YES completion:nil]; } -(void)createSelectBtn{ _notifyBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _notifyBtn.backgroundColor = COLOR_THEME; _notifyBtn.frame = CGRectMake(kSize.width-80, kSize.height-90-kNavOffSet, 60, 60); [_notifyBtn setTitle:@"审核\n学时" forState:UIControlStateNormal]; [_notifyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; _notifyBtn.titleLabel.numberOfLines = 0; _notifyBtn.layer.masksToBounds = YES; _notifyBtn.layer.cornerRadius = 30; [_notifyBtn addTarget:self action:@selector(notifiBtnClick:) forControlEvents:UIControlEventTouchUpInside]; _notifyBtn.hidden = YES; [self.view addSubview:_notifyBtn]; [self.view bringSubviewToFront:self.holderV]; } -(void)notifiBtnClick:(UIButton *)sender{ if (self.dataurce.count == 0) { return; } if (!_isEditing) { _isEditing = YES; [self.tableView setEditing:YES];//进入编辑模式 [self removeRefreshAction]; [sender setTitle:@"提交\n审核" forState:UIControlStateNormal]; CGRect frame = sender.frame;; _allBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _allBtn.backgroundColor = [UIColor lightGrayColor]; //_allBtn.backgroundColor = COLOR_THEME; _allBtn.frame = CGRectMake(frame.origin.x + 30, frame.origin.y, 0, 30); [_allBtn setTitle:@"全选" forState:UIControlStateNormal]; [_allBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_allBtn setTitleColor:COLOR_THEME forState:UIControlStateSelected]; [_allBtn addTarget:self action:@selector(allClick:) forControlEvents:UIControlEventTouchUpInside]; [KWINDOW addSubview:_allBtn]; _cancelBtn = [UIButton buttonWithType:UIButtonTypeSystem]; _cancelBtn.backgroundColor = [UIColor lightGrayColor]; _cancelBtn.frame = CGRectMake(frame.origin.x + 30, frame.origin.y + 30, 0, 30); [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal]; [_cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_cancelBtn addTarget:self action:@selector(cancelClick:) forControlEvents:UIControlEventTouchUpInside]; [KWINDOW addSubview:_cancelBtn]; [UIView animateWithDuration:.4 animations:^{ _allBtn.frame = CGRectMake(frame.origin.x-100, frame.origin.y - 5, 80, 30); _cancelBtn.frame = CGRectMake(frame.origin.x-100, frame.origin.y + 35, 80, 30); }]; return; } NSMutableArray * allId = [NSMutableArray new]; for (NSIndexPath *indexPath in _selectedIndexPathes) { [allId addObject:self.dataurce[indexPath.row][@"ID"]]; } if (allId.count > 0) { [self.tableView setEditing:NO]; [self setRefreshAction]; [sender setTitle:@"审核\n学时" forState:UIControlStateNormal]; [_allBtn removeFromSuperview]; [_cancelBtn removeFromSuperview]; _isEditing = NO; if (allId.count == 1) {//单条审核 [self shenheSingleXueshiWith:_selectedIndexPathes[0]]; }else{//多条审核 [self shenheXueshiWithIds:allId]; } [_selectedIndexPathes removeAllObjects]; }else{ [self showMsgByAlertVCWithString:@"请选择学时"]; } } -(void)allClick:(UIButton *)button{ button.selected = !button.selected; //获取表格视图内容的尺寸 CGSize size = self.tableView.contentSize; CGRect rect = CGRectMake(0, 0, size.width, size.height); //获取指定区域的cell的indexPath NSArray *indexPathes = [self.tableView indexPathsForRowsInRect:rect]; if (button.selected) { for (NSIndexPath *indexPath in indexPathes) { //使用代码方式选中一行 [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; } //更新选中数组 _selectedIndexPathes.array = indexPathes; } else { for (NSIndexPath *indexPath in indexPathes) { //使用代码方式取消选中一行 [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; } //清空选中cell的记录数组 [_selectedIndexPathes removeAllObjects]; } } - (void)cancelClick:(UIButton *)sender { [self.tableView setEditing:NO]; [self setRefreshAction]; [_notifyBtn setTitle:@"审核\n学时" forState:UIControlStateNormal]; [_allBtn removeFromSuperview]; [_cancelBtn removeFromSuperview]; _isEditing = NO; [_selectedIndexPathes removeAllObjects]; } -(void)shenheSingleXueshiWith:(NSIndexPath *)indexPath{ NSDictionary * dic = self.dataurce[indexPath.row]; NSString * message = [NSString stringWithFormat:@"姓名:%@ 训练时长:%@",dic[@"STUNAME"],dic[@"TIME"]]; UIAlertController *alertFind = [UIAlertController alertControllerWithTitle:@"审核学时" message:message preferredStyle:UIAlertControllerStyleAlert]; [alertFind addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"输入审核原因[二次审核不能为空]"; }]; [alertFind addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]]; [alertFind addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { UITextField *tf = alertFind.textFields[0]; [self trainTimesCheckWithDic:dic withReason:tf.text]; }]]; [self presentViewController:alertFind animated:true completion:nil]; } -(void)customRightBtn{ UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"筛选" style:UIBarButtonItemStylePlain target:self action:@selector(setMengbanView)]; item.tintColor = COLOR_THEME; self.navigationItem.rightBarButtonItem = item; } -(void)setMengbanView{ //如果是编辑状态 就不弹出筛选条件框 if (_isEditing) { [self showMsgByMBWithString:@"正在选择待审核学时,无法进行此操作!"]; return; } if (_mengBanView) { [_mengBanView showView]; return; } NSArray * btnarr1 = @[@"第一部分",@"第二部分",@"第三部分",@"第四部分"]; NSArray * btnarr2 = @[@"待审核",@"审核通过",@"审核不通过"]; __weak typeof(self) weakSelf = self; _mengBanView = [[MengBanView alloc]initWithTitileStr:nil buttonsArray:@[btnarr1,btnarr2] block:^(NSArray *array) { //array 是你已经选择好的内容 _kemu = array[0]; _state = array[1]; [weakSelf headerRefresh]; }]; [_mengBanView showView]; } - (void)creatSearchVC { _searchController = [[UISearchController alloc]initWithSearchResultsController:nil]; _searchController.delegate = self; _searchController.searchBar.delegate = self; _searchController.searchBar.placeholder = @"请输入学员姓名"; //设置UISearchController的显示属性,以下3个属性默认为YES //搜索时,背景变暗色 // _searchController.dimsBackgroundDuringPresentation = NO; // //搜索时,背景变模糊 // _searchController.obscuresBackgroundDuringPresentation = NO; // //隐藏导航栏 // _searchController.hidesNavigationBarDuringPresentation = NO; // [_searchController.searchBar sizeToFit]; _searchController.searchBar.frame = CGRectMake(_searchController.searchBar.frame.origin.x, _searchController.searchBar.frame.origin.y, _searchController.searchBar.frame.size.width, 44.0); self.tableView.tableHeaderView = _searchController.searchBar; } #pragma mark - 下拉刷新,上拉加载 - -(void)setRefreshAction{ // 下拉加载更多 __weak typeof(self) weakSelf = self; MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ [weakSelf headerRefresh]; }]; self.tableView.mj_header = header; MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ [weakSelf footerRefresh]; }]; self.tableView.mj_footer = footer; } -(void)headerRefresh{ //设置获取数据的方式 _getDataType=MyGetDataTypeHeaderRefresh; //加载数据 [self getData]; [self.tableView.mj_header endRefreshing]; } -(void)footerRefresh{ //设置获取数据的方式 _getDataType=MyGetDataTypeFooterRefresh; //加载数据 [self getData]; [self.tableView.mj_footer endRefreshing]; } -(void)removeRefreshAction{ self.tableView.mj_header = nil; self.tableView.mj_footer = nil; } #pragma mark 代理方法 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ // if (_searchController.active) { // return _searchArr.count;//搜索结果 // }else // { // return self.dataurce.count;//原始数据 // } return self.dataurce.count; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 100; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{ XueShiCheckCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellXueShiCheck"]; if (!cell) { //加载xib文件创建cell cell = [[[NSBundle mainBundle] loadNibNamed:@"XueShiCheckCell" owner:nil options:nil] lastObject]; } [cell updataWithDic:self.dataurce[indexPath.row]]; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //选择单元格 if (_isEditing) { //当选中cell时,记录其indexPath if (![_selectedIndexPathes containsObject:indexPath]) { [_selectedIndexPathes addObject:indexPath]; } return; } //[self showMsgByAlertVCWithString:@"点击右下角图标可选择学时进行审核"]; //查看电子日志详细信息 TeachLogForCheckVC *teachlogVC = [[TeachLogForCheckVC alloc] init]; teachlogVC.checkDic = self.dataurce[indexPath.row]; [self.navigationController pushViewController:teachlogVC animated:YES]; // NSDictionary * dic = self.dataurce[indexPath.row]; // NSString * message = [NSString stringWithFormat:@"姓名:%@ 训练时长:%@",dic[@"STUNAME"],dic[@"TIME"]]; // UIAlertController *alertFind = [UIAlertController alertControllerWithTitle:@"审核学时" message:message preferredStyle:UIAlertControllerStyleAlert]; // [alertFind addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { // textField.placeholder = @"输入审核原因[二次审核不能为空]"; // }]; // [alertFind addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]]; // [alertFind addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { // UITextField *tf = alertFind.textFields[0]; // [self trainTimesCheckWithDic:dic withReason:tf.text]; // }]]; // [self presentViewController:alertFind animated:true completion:nil]; } - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { //当取消选中cell时,从记录的数组中删除其indexPath if ([_selectedIndexPathes containsObject:indexPath] && _isEditing) { [_selectedIndexPathes removeObject:indexPath]; if (_allBtn.selected) { _allBtn.selected = NO; } } } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleInsert |UITableViewCellEditingStyleDelete; } #pragma mark searchbar代理相关 -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ _stuName = searchBar.text; _currentPageNum = 0; [self getData]; } #pragma mark searchcontroller代理 -(void)willPresentSearchController:(UISearchController *)searchController{ _searchController.searchBar.showsCancelButton = YES; UIButton *canceLBtn = [_searchController.searchBar valueForKey:@"cancelButton"]; [canceLBtn setTitle:@"取消" forState:UIControlStateNormal]; [canceLBtn setTitleColor:COLOR_THEME forState:UIControlStateNormal]; } -(void)didDismissSearchController:(UISearchController *)searchController{ _searchController.searchBar.frame = CGRectMake(0, 0, _searchController.searchBar.frame.size.width, 44.0); } #pragma mark 网络请求 - (void)getData{ //判断当前是否正在加载数据。如果正在加载数据,直接return。 if (_IS_LOADING) { return; } _IS_LOADING=YES; //获取第一页数据 NSInteger needLoadPage; needLoadPage=_currentPageNum+1; if (_getDataType==MyGetDataTypeHeaderRefresh) { needLoadPage=1; } //@[@"未审核",@"已审核",@"审核不通过"] NSString * statu = @""; if ([_state isEqualToString:@"待审核"]) { statu = @"0"; }else if ([_state isEqualToString:@"审核通过"]){ statu = @"1"; }else if ([_state isEqualToString:@"审核不通过"]){ statu = @"2"; } NSString * kemuStr = @""; if ([_kemu isEqualToString:@"第一部分"]) { kemuStr = @"1"; }else if ([_kemu isEqualToString:@"第二部分"]){ kemuStr = @"2"; }else if ([_kemu isEqualToString:@"第三部分"]){ kemuStr = @"3"; }else if ([_kemu isEqualToString:@"第四部分"]){ kemuStr = @"4"; } NSMutableDictionary * mdic = [NSMutableDictionary new]; [mdic setValue:@"" forKey:@"dqbh"]; [mdic setValue:@"" forKey:@"qxbh"]; [mdic setValue:@"" forKey:@"inscode"]; [mdic setValue:_stuName forKey:@"studentName"]; [mdic setValue:kemuStr forKey:@"subject"]; [mdic setValue:statu forKey:@"auditStatus"]; [mdic setValue:@"1" forKey:@"isPage"]; [mdic setValue:@"10" forKey:@"pageSize"]; [mdic setValue:[NSString stringWithFormat:@"%d",(int)needLoadPage] forKey:@"currentPage"]; __weak typeof(self) weakSelf = self; [self getDataWithDic:mdic method:@"trainTimes" block:^(NSDictionary *successDic) { //设置加载状态 _IS_LOADING=NO; //处理数据 //如果获取到了数据,而且是下拉刷新,清空数组。 if (_getDataType==MyGetDataTypeHeaderRefresh){ [weakSelf.dataurce removeAllObjects]; } //如果是第一页 清空之前的数据 if (_currentPageNum == 0) { [weakSelf.dataurce removeAllObjects]; } //追加数据 [weakSelf.dataurce addObjectsFromArray:successDic[@"body"]]; if (weakSelf.dataurce.count != 0) { self.holderV.hidden = YES; _notifyBtn.hidden = NO; } //如果是切换筛选条件 就重新判断是否显示审核按钮 _notifyBtn.hidden = YES; if ([_state isEqualToString:@"待审核"]) { _notifyBtn.hidden = NO; } //计数器+1 _currentPageNum=needLoadPage; //刷新界面 [weakSelf.tableView reloadData]; //恢复初始状态。 _getDataType=MyGetDataTypeNomal; }]; } -(void)shenheXueshiWithIds:(NSMutableArray *)ids{ if ([MYAPPDELEGATE.userDic[@"qz"] length] == 0) { [self showMsgByAlertVCWithString:@"审核失败,不具备审核权限"]; return; } NSMutableString *str = [NSMutableString new]; for (NSString *obj in ids) { [str appendFormat:@",%@",obj]; } NSString * idsStr = [str substringFromIndex:1]; NSMutableDictionary * mdic = [NSMutableDictionary new]; [mdic setValue:idsStr forKey:@"ids"]; [mdic setValue:MYAPPDELEGATE.userDic[@"id"] forKey:@"userId"]; [mdic setValue:@"1" forKey:@"auditStatus"]; [mdic setValue:@"" forKey:@"reason"]; //判断网络是否连接 if (![NetworkManager connectedToNetWork]) { [self showMsgByAlertVCWithString:@"网络连接异常"]; return; } [MBProgressHUD hideHUDForView:self.view animated:NO]; [MBProgressHUD showHUDAddedTo:self.view animated:YES]; [NetworkManager requestWithMethod:@"trainTimesAudit" parameters:mdic type:0 handler:^(NSDictionary *successDic, NSString *failureStr) { [MBProgressHUD hideHUDForView:self.view animated:YES]; //设置加载状态 _IS_LOADING=NO; if (failureStr) { [self showMsgByAlertVCWithString:failureStr]; return; } if ([successDic[@"code"] isEqualToString:@"1"]) { [self showMsgByAlertVCWithString:successDic[@"msg"]]; return; } [self showMsgByMBWithString:@"审核成功"]; [self getData]; }]; } -(void)trainTimesCheckWithDic:(NSDictionary *)dic withReason:(NSString *)reason{ if ([MYAPPDELEGATE.userDic[@"qz"] length] == 0) { [self showMsgByAlertVCWithString:@"审核失败,不具备审核权限"]; return; } NSMutableDictionary * mdic = [NSMutableDictionary new]; [mdic setValue:@[dic[@"ID"]] forKey:@"ids"]; [mdic setValue:MYAPPDELEGATE.userDic[@"id"] forKey:@"userId"]; [mdic setValue:dic[@"STATUS"] forKey:@"auditStatus"]; [mdic setValue:reason forKey:@"reason"]; [self getDataWithDic:mdic method:@"trainTimesAudit" block:^(NSDictionary *successDic) { [self showMsgByMBWithString:@"审核成功"]; [self getData]; }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #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