// // CoachPlanVC.m // jiaPei // // Created by apple on 16/3/25. // Copyright © 2016年 JCZ. All rights reserved. // #import "CoachPlanVC.h" #import "HolderView.h" #import "CLWeeklyCalendarView.h" #import "PlanCollectionCell.h" #import "ApplyPlanStuListVC.h" #import "UIImageView+WebCache.h" @interface CoachPlanVC () { NSString *taskTime; NSArray *models; NSArray *coachsArray; NSMutableArray *mockDatas; CGFloat calendarY; NSIndexPath *lastIndex; NSString *coachNum; NSString *lackMoney; UICollectionView *mainCollection; HolderView *holderV; UILabel *nameLabel,*countLabel,*addressLabel; UITableView *stuListTV; NSArray *stuListArray; NSDictionary *stuDic; UIButton *applyPlanBtn; } @property (nonatomic, strong) CLWeeklyCalendarView *calendarView; //预约弹出框 @property (nonatomic, strong) UIView *reserveBackView; @property (nonatomic, strong) UITextField *searchTF; @end @implementation CoachPlanVC - (void)viewDidLoad { [super viewDidLoad]; [self myInit]; } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; RemoveHUD(); } -(void)myInit { [self setTitle:@"教练计划"]; [self.view setBackgroundColor:KBackGroundColor]; [self goBackByNavigation]; //初始化数据容器 mockDatas = [[NSMutableArray alloc] init]; coachsArray = [NSArray array]; CGFloat x,y,w,h,bd; x = bd = 10; y = 5; w = kSize.width - 20; h = 25; //上面显示的信息 UILabel *label; label = [[UILabel alloc] KSetxywh]; [label setText:@"当前计划信息:暂无" Font:Font18 TextColor:KTitleColor Alignment:NSTextAlignmentLeft]; [self.view addSubview:label]; nameLabel = label; y += h; label = [[UILabel alloc] KSetxywh]; [label setText:@"场地信息:暂无" Font:Font18 TextColor:KTitleColor Alignment:NSTextAlignmentLeft]; [self.view addSubview:label]; addressLabel = label; y += h; h += bd; label = [[UILabel alloc] KSetxywh]; [label setText:@"暂无预约人数信息" Font:Font17 TextColor:[UIColor orangeColor] Alignment:NSTextAlignmentLeft]; label.numberOfLines = 0; [self.view addSubview:label]; countLabel = label; //日历控件 y += h; calendarY = y; [self.view addSubview:self.calendarView]; y += self.calendarView.height; //因为Y是从64开始算的 即把64当做0 所以要有补偿 h = kSize.height - y - kNavOffSet - 60; //用collection吧 CGFloat width = (kSize.width - 45)/3.0; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; [layout setItemSize:CGSizeMake(width, width)]; [layout setSectionInset:UIEdgeInsetsMake(8, 10, 8, 10)]; mainCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, y, kSize.width, h) collectionViewLayout:layout]; mainCollection.backgroundColor = KBackGroundColor; mainCollection.delegate = self; mainCollection.dataSource = self; [self.view addSubview:mainCollection]; [mainCollection addViewWithRect:CGRectMake(10, y + h, kSize.width - 20, 1)]; [mainCollection registerNib:[UINib nibWithNibName:@"PlanCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"collection"]; holderV = [[HolderView alloc] initWithFrame:mainCollection.frame]; [self.view addSubview:holderV]; [holderV setHidden:YES]; //将日历放前边 防止被遮挡 [self.view bringSubviewToFront:self.calendarView]; x = 0; y += h + 1; w = kSize.width; h = 59; UIView *btnBar = [[UIView alloc] KSetxywh]; btnBar.backgroundColor = kLineColor; [self.view addSubview:btnBar]; x = 10; y = 5; w = kSize.width/2.0 - 20; h = 49; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(x, y, w, h); [btn setTitle:@"预约" textColor:[UIColor whiteColor] font:20 fotState:UIControlStateNormal]; btn.layer.masksToBounds = YES; btn.layer.cornerRadius = 10; btn.backgroundColor = [UIColor orangeColor]; [btn target:self]; btn.tag = 1; [btnBar addSubview:btn]; applyPlanBtn = btn; x = kSize.width/2.0 + 10; btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(x, y, w, h); [btn setTitle:@"查看预约学员" textColor:[UIColor whiteColor] font:20 fotState:UIControlStateNormal]; btn.layer.masksToBounds = YES; btn.layer.cornerRadius = 10; btn.backgroundColor = [UIColor orangeColor]; [btn target:self]; btn.tag = 2; [btnBar addSubview:btn]; } -(void)btnClick:(UIButton*)sender { int tag = (int)sender.tag; if (1 == tag) { if (!lastIndex) { ShowMsg(@"请选择要预约的计划"); return; } [myDelegate.window addSubview:self.reserveBackView]; } if (2 == tag) { if (!lastIndex) { ShowMsg(@"请选择要查看的计划"); return; } if (models.count > lastIndex.row) { NSDictionary *dic = models[lastIndex.row]; ApplyPlanStuListVC *vc = [[ApplyPlanStuListVC alloc] init]; vc.planDic = dic; [self navPushHideTabbarToVC:vc]; }else { ShowMsg(@"请选择未过期教练排班"); } } if (3 == tag) { if (_searchTF.text.length < 1) { ShowMsg(@"请输入学员信息"); return; } [_reserveBackView endEditing:YES]; [self getStudentData]; } if (4 == tag) {//取消 [_reserveBackView removeFromSuperview]; } if (5 == tag) {//预约 [_reserveBackView removeFromSuperview]; [self saveReserve]; } } -(void)setLabelUIWithDic:(NSDictionary *)dic { if (dic.allKeys.count == 1) { //这个if是应对虚拟数据的 nameLabel.text = @"当前计划信息:暂无"; countLabel.text = @"暂无预约人数信息"; return; } NSString *kmString = @"科目二"; if ([[NSString stringWithFormat:@"%@",dic[@"KM"]] isEqualToString:@"3"]) { kmString = @"科目三"; } nameLabel.text = [NSString stringWithFormat:@"当前计划信息:%@ %@ %@元/小时",dic[@"USERNAME"],kmString,dic[@"MONEY"]]; addressLabel.text = [NSString stringWithFormat:@"场地信息:%@",dic[@"JSADDRESS"]]; if ([dic[@"JSADDRESS"] length] < 1) { addressLabel.text = @"场地信息:暂无"; } countLabel.text = [NSString stringWithFormat:@"计划可预约%@人,已预约%@人,有%@人也申请了预约计划",dic[@"PI_NUM"],dic[@"PI_HAV_NUM"],dic[@"HAVNUM"]]; } -(CLWeeklyCalendarView *)calendarView { if(!_calendarView){ _calendarView = [[CLWeeklyCalendarView alloc] initWithFrame:CGRectMake(0, calendarY, kSize.width, 120)]; _calendarView.delegate = self; } return _calendarView; } - (UIView *)reserveBackView { if (!_reserveBackView) { _reserveBackView = [[UIView alloc] initWithFrame:kFrame]; _reserveBackView.backgroundColor = [UIColor colorWithWhite:.2 alpha:.4]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBored)]; [_reserveBackView addGestureRecognizer:tap]; CGFloat w = kSize.width - 40; UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, kSize.height/2.0 - 140, w, 240)]; view.backgroundColor = [UIColor whiteColor]; [view borderCornorRadios:5]; [_reserveBackView addSubview:view]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, w - 40, 30)]; [label setText:@"请选择预约学员" Font:Font21 TextColor:KTitleColor Alignment:NSTextAlignmentCenter]; [view addSubview:label]; _searchTF = [[UITextField alloc] initWithFrame:CGRectMake(20, 40, w - 40 - 60, 30)]; _searchTF.placeholder = @"请输入学员姓名或证件号码"; _searchTF.font = [UIFont scaleSize:Font17]; [_searchTF borderColor:kLineColor width:1 cornorRadios:3]; [view addSubview:_searchTF]; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(w - 75, 40, 55, 30)]; btn.backgroundColor = [UIColor orangeColor]; [btn setTitle:@"搜索" textColor:[UIColor whiteColor] font:Font17 fotState:UIControlStateNormal]; [btn borderCornorRadios:5]; [btn target:self Tag:3]; [view addSubview:btn]; stuListArray = [NSArray array]; stuListTV = [[UITableView alloc] initWithFrame:CGRectMake(20, 80, w - 40, 110) style:UITableViewStylePlain]; stuListTV.delegate = self; stuListTV.dataSource = self; stuListTV.tableFooterView = [UIView new]; [view addSubview:stuListTV]; [stuListTV borderColor:kLineColor width:1 cornorRadios:4]; // label = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, w - 40, 110)]; // [label setText:@"这里是个表 显示搜索到的学员信息" Font:Font17 TextColor:KTitleColor Alignment:NSTextAlignmentCenter]; // label.numberOfLines = 0; // [label borderColor:kLineColor width:1 cornorRadios:4]; // [view addSubview:label]; btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 200, w/2.0 - 30, 30)]; btn.backgroundColor = [UIColor lightGrayColor]; [btn setTitle:@"取消" textColor:KTitleColor font:Font17 fotState:UIControlStateNormal]; [btn borderCornorRadios:5]; [btn target:self Tag:4]; [view addSubview:btn]; btn = [[UIButton alloc] initWithFrame:CGRectMake(w/2.0 + 10, 200, w/2.0 - 30, 30)]; btn.backgroundColor = [UIColor lightGrayColor]; [btn setTitle:@"预约" textColor:KTitleColor font:Font17 fotState:UIControlStateNormal]; [btn borderCornorRadios:5]; [btn target:self Tag:5]; [view addSubview:btn]; } return _reserveBackView; } - (void)closeKeyBored { [_reserveBackView endEditing:YES]; } #pragma mark - CLWeeklyCalendarViewDelegate -(NSDictionary *)CLCalendarBehaviorAttributes { //CLCalendarSelectedDatePrintFormatDefault 显示选中日期格式 return @{ //CLCalendarWeekStartDay : @2, //Start Day of the week, from 1-7 Mon-Sun -- default 1 // CLCalendarDayTitleTextColor : [UIColor yellowColor], // CLCalendarSelectedDatePrintColor : [UIColor greenColor], }; } -(void)dailyCalendarViewDidSelect:(NSDate *)date { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd"]; taskTime = [formatter stringFromDate:date]; //ShowHUD(); if (lastIndex) { PlanCollectionCell *lastCell = (PlanCollectionCell *)[mainCollection cellForItemAtIndexPath:lastIndex]; lastCell.backImageView.image = [UIImage imageNamed:@"plancell3.png"]; } [self getPlanInfos]; } #pragma mark - collection dalegate -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (models.count == 0) { return mockDatas.count; } return models.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { PlanCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath]; //每次获取cell 把隐藏的控件全显示出来 还有会改变的默认属性 这样的话 下面就方便了 cell.countLabel.hidden = NO; cell.headImgView.hidden = NO; cell.timeLabel.textColor = KTitleColor; cell.kmLabel.textColor = KTitleColor; cell.countLabel.textColor = KTitleColor; //把所有情况设置为不可点击 然后在可以点击的情况改变这个状态 cell.isCanSelect = NO; if (models.count == 0) { cell.timeLabel.text = mockDatas[indexPath.row]; cell.countLabel.hidden = YES; cell.headImgView.hidden = YES; cell.backImageView.image = [UIImage imageNamed:@"plancell1.png"]; cell.kmLabel.text = @"已过期"; return cell; } NSDictionary *dic = models[indexPath.row]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *crDate = [NSDate date]; if ([dic[@"TIMES"] rangeOfString:@"-"].location != NSNotFound && [dic[@"TASKTIME"] rangeOfString:@"-"].location != NSNotFound) { NSString *endTime = [NSString stringWithFormat:@"%@ %@:00",dic[@"TASKTIME"],[[dic[@"TIMES"] componentsSeparatedByString:@"-"] lastObject]]; crDate = [formatter dateFromString:endTime]; } NSDate *nowDate = [NSDate date]; NSComparisonResult result = [crDate compare:nowDate]; //NSLog(@"result--->%d-crDate-%@-nowDate-%@",(int)result,crDate,nowDate); //过期不能预约 预约过不能预约 人数已经预约满不能预约 这个判断顺序是没问题的 if (result == NSOrderedAscending){ //NSLog(@"已过期"); cell.backImageView.image = [UIImage imageNamed:@"plancell1.png"]; cell.kmLabel.text = @"已过期"; cell.headImgView.hidden = YES; cell.countLabel.hidden = YES; if ([[NSString stringWithFormat:@"%@",dic[@"STATUS"]] isEqualToString:@"0"]) { cell.kmLabel.text = @"被拒,已过期"; }else if ([[NSString stringWithFormat:@"%@",dic[@"STATUS"]] isEqualToString:@"1"]){ cell.kmLabel.text = @"已预约,已过期"; }else if ([[NSString stringWithFormat:@"%@",dic[@"STATUS"]] isEqualToString:@"2"]){ cell.kmLabel.text = @"预约中,已过期"; } } if (result == NSOrderedDescending) { //NSLog(@"未过期"); cell.backImageView.image = [UIImage imageNamed:@"plancell3.png"]; cell.countLabel.text = [NSString stringWithFormat:@"%d/%d",[dic[@"PI_HAV_NUM"] intValue],[dic[@"PI_NUM"] intValue]]; cell.kmLabel.text = [[NSString stringWithFormat:@"%@",dic[@"KM"]] isEqualToString:@"2"]?@"科目二":@"科目三"; if (indexPath == lastIndex) { cell.backImageView.image = [UIImage imageNamed:@"plancell2.png"]; } if ([[NSString stringWithFormat:@"%@",dic[@"STATUS"]] isEqualToString:@"0"]) { cell.kmLabel.text = @"预约被拒"; cell.kmLabel.textColor = [UIColor orangeColor]; }else if ([[NSString stringWithFormat:@"%@",dic[@"STATUS"]] isEqualToString:@"1"]){ cell.kmLabel.text = @"已预约"; cell.kmLabel.textColor = [UIColor orangeColor]; }else if ([[NSString stringWithFormat:@"%@",dic[@"STATUS"]] isEqualToString:@"2"]){ cell.kmLabel.text = @"预约中"; cell.kmLabel.textColor = [UIColor orangeColor]; }else if ([[NSString stringWithFormat:@"%@",dic[@"STATUS"]] isEqualToString:@"-1"]){ if ([[NSString stringWithFormat:@"%@",dic[@"PI_HAV_NUM"]] isEqualToString:[NSString stringWithFormat:@"%@",dic[@"PI_NUM"]]]) { cell.kmLabel.text = @"已约满"; cell.backImageView.image = [UIImage imageNamed:@"plancell1.png"]; }else{ cell.isCanSelect = YES; } } } cell.timeLabel.text = dic[@"TIMES"]; return cell; } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [collectionView deselectItemAtIndexPath:indexPath animated:YES]; PlanCollectionCell *cell = (PlanCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath]; if (models.count > indexPath.row) { NSDictionary *dic = models[indexPath.row]; [self setLabelUIWithDic:dic]; } //如果之前操作过 找到改变状态 移除数据 if (lastIndex) { if (![lastIndex isEqual:indexPath]) { PlanCollectionCell *lastCell = (PlanCollectionCell *)[collectionView cellForItemAtIndexPath:lastIndex]; if (lastCell.isCanSelect) { lastCell.backImageView.image = [UIImage imageNamed:@"plancell3.png"]; }else { lastCell.backImageView.image = [UIImage imageNamed:@"plancell1.png"]; } } } cell.backImageView.image = [UIImage imageNamed:@"plancell2.png"]; if (cell.isCanSelect == NO) { //不能点击预约按钮 applyPlanBtn.backgroundColor = [UIColor lightGrayColor]; applyPlanBtn.userInteractionEnabled = NO; }else { //可以点击预约按钮 applyPlanBtn.backgroundColor = [UIColor orangeColor]; applyPlanBtn.userInteractionEnabled = YES; } //记录上一次点击按钮 lastIndex = indexPath; } #pragma mark tableView -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return stuListArray.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"]; [cell.imageView borderCornorRadios:3]; cell.imageView.contentMode = UIViewContentModeScaleAspectFit; } NSDictionary *dic = stuListArray[indexPath.row]; NSString *faceImgPath = dic[@"TSO_PHOTO_PATH"]; if (!faceImgPath) { faceImgPath = @""; } [cell.imageView sd_setImageWithURL:[NSURL URLWithString:faceImgPath] placeholderImage:[UIImage imageNamed:@"NOImg"]]; cell.textLabel.text = dic[@"TSO_NAME"]; cell.detailTextLabel.text = dic[@"TSO_IDCARD"]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { stuDic = stuListArray[indexPath.row]; } #pragma mark - 获取数据 -(void)getPlanInfos { if (![NetManager connectedToNetWork]) { [holderV setHidden:NO]; return; } if (!taskTime) { NSDateFormatter* fm = [NSDateFormatter new]; [fm setDateFormat:@"yyyy-MM-dd"]; taskTime = [fm stringFromDate:[NSDate date]]; } if (_coachDic) { coachNum = _coachDic[@"SFZHM"]; } if (coachNum.length < 1) { coachNum = @""; } NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setObject:taskTime forKey:@"planTime"]; [dic setObject:coachNum forKey:@"idcard"]; NSString *method = @"getPlans"; [NetManager requestAnythingWithURL:method dictionary:dic dataArray:nil completion:^(NSDictionary *root) { // NSLog(@"找计划------>%@------>%@",dic,root); if (!root) { ShowMsg(@"网络请求失败"); [holderV setHidden:NO]; return; } NSString* code = root[@"code"]; if (code.intValue >0) { ShowMsg(root[@"body"]); [holderV setHidden:NO]; return; } models = root[@"body"]; if (models.count > 0) { NSDictionary *dic = [models firstObject]; [self setLabelUIWithDic:dic]; [mainCollection reloadData]; [holderV setHidden:YES]; }else{ NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *taskDate = [formatter dateFromString:[NSString stringWithFormat:@"%@ 23:59:59",taskTime]]; NSComparisonResult result = [taskDate compare:[NSDate date]]; if (result == NSOrderedAscending){ //NSLog(@"已过期"); [holderV setHidden:YES]; [self getMockDates]; } if (result == NSOrderedDescending || result == NSOrderedSame) { //NSLog(@"未过期"); ShowMsg(@"暂无计划"); [holderV setHidden:NO]; } } }]; } -(void)getMockDates { mockDatas = [[NSMutableArray alloc] init]; NSInteger time = 8; for (int i = 0; i < 10; i ++) { NSInteger beginTime = time; time += arc4random()%2 + 1; if (time > 18) { break; } NSString *timeString = [NSString stringWithFormat:@"%d:00-%d:00",(int)beginTime,(int)time]; [mockDatas addObject:timeString]; } //NSLog(@"---->%@",mockDatas); [mainCollection reloadData]; NSDictionary *dic = [NSDictionary dictionaryWithObject:@"NOPLAN" forKey:@"NOPLAN"]; [self setLabelUIWithDic:dic]; } //获取学员信息 -(void)getStudentData{ //判断网络是否连接 if (![NetManager connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableDictionary * mdic = [NSMutableDictionary new]; [mdic setValue:_searchTF.text forKey:@"keyWord"]; [NetManager requestAnythingWithURL:@"getStudentList" dictionary:mdic dataArray:nil completion:^(NSDictionary *root) { if (!root) { ShowMsg(@"学员信息查询失败,请重试"); return; } if ([root[@"code"] integerValue] == 1) { ShowMsg(root[@"msg"]); return; } if ([root[@"body"] count] < 1) { ShowMsg(@"查询不到该学员,请确认查询条件"); return; }else { if ([root[@"body"] count] == 1) { stuDic = [root[@"body"] firstObject]; } //查询到多个学员 展示列表 stuListArray = root[@"body"]; [stuListTV reloadData]; } }]; } //预约教练计划 -(void)saveReserve{ //判断网络是否连接 if (![NetManager connectedToNetWork]) { showMsgUnconnect(); return; } NSDictionary *planDic = [NSDictionary dictionary]; if (models.count > lastIndex.row) { planDic = models[lastIndex.row]; } NSMutableDictionary * mdic = [NSMutableDictionary new]; [mdic setValue:planDic[@"ID"] forKey:@"planId"]; [mdic setValue:stuDic[@"TSO_IDCARD"] forKey:@"idcard"]; [mdic setValue:stuDic[@"TSO_PHONE"] forKey:@"telphone"]; [NetManager requestAnythingWithURL:@"saveReserve" dictionary:mdic dataArray:nil completion:^(NSDictionary *root) { if (!root) { ShowMsg(@"预约失败,请重试"); return; } if ([root[@"code"] integerValue] == 1) { ShowMsg(root[@"msg"]); return; } ShowMsg(@"预约成功!"); [self getPlanInfos]; }]; } #pragma mark -(void)playAudioWithString:(NSString *)string { [Tools playAudioWithString:string]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end