// // CoachPlanVC.m // jiaPei // // Created by apple on 16/3/25. // Copyright © 2016年 JCZ. All rights reserved. // #import "CoachPlanVC.h" #import "PlanCell.h" #import "HolderView.h" #import "CLWeeklyCalendarView.h" #import "PlanCollectionCell.h" #import "Tools.h" #import "SDSoundPlayer.h" #import "TopUpMoneyVC.h" @interface CoachPlanVC () { NSString *taskTime; NSArray *models; NSArray *coachsArray; NSMutableArray *mockDatas; NSMutableArray *selectModel; CGFloat calendarY; NSIndexPath *lastIndex; NSString *coachNum; NSString *lackMoney; UICollectionView *mainCollection; HolderView *holderV; UILabel *nameLabel,*countLabel,*addressLabel,*remindLabel,*timeLabel; UIBarButtonItem *item; } @property (nonatomic, strong) CLWeeklyCalendarView* calendarView; @end @implementation CoachPlanVC - (id)initWithCoder:(NSCoder*)coder { self = [super initWithCoder:coder]; if (self) { //加这句 不让导航栏影响xib中控件的布局 现在不加也行 因为xib取消使用自动布局 问题解决 bingo //self.automaticallyAdjustsScrollViewInsets = NO; } return self; } - (void)viewDidLoad { [super viewDidLoad]; [self myInit]; } -(void)myInit { [self setTitle:@"教练计划"]; [self.view setBackgroundColor:backGroundColor]; if (_isNotification == NO) { //正常状态 [self configNavigationBar]; } else { //推送状态 要将根视图变回去 UIBarButtonItem* backBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"question_pre_checked_icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(dismissNavgation)]; [backBtn setTintColor:defGreen]; self.navigationController.navigationBar.translucent = NO; [self.navigationItem setLeftBarButtonItem:backBtn]; } //初始化数据容器 mockDatas = [[NSMutableArray alloc] init]; selectModel = [[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] setxywh]; [label setText:@"当前计划信息:暂无" Font:FontTitle TextColor:kTitleColor Alignment:NSTextAlignmentLeft]; [self.view addSubview:label]; nameLabel = label; y += h; label = [[UILabel alloc] setxywh]; [label setText:@"场地信息:暂无" Font:FontTitle TextColor:kTitleColor Alignment:NSTextAlignmentLeft]; [self.view addSubview:label]; addressLabel = label; y += h; h += bd; label = [[UILabel alloc] setxywh]; [label setText:@"暂无预约人数信息" Font:NormalFont 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 - 90 - kNavOffSet - kSafeAreaBottomHeight - 4; //用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 = backGroundColor; mainCollection.delegate = self; mainCollection.dataSource = self; [self.view addSubview:mainCollection]; [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]; y += h + 4; h = 30; label = [[UILabel alloc] initWithFrame:CGRectMake(0, y, kSize.width, h)]; label.backgroundColor = contentTextColor; [label setText:@" 每条计划只能取消预约一次哦,请您慎重" Font:NormalFont TextColor:[UIColor orangeColor]]; [self.view addSubview:label]; remindLabel = label; x = 10; y += h; w = 200; h = 60; label = [[UILabel alloc] initWithFrame:CGRectMake(0, y - 3, kSize.width, 3)]; label.backgroundColor = KlineColor; [self.view addSubview:label]; label = [[UILabel alloc] setxywh]; [label setText:@"你已选了0个预约哦" Font:20 TextColor:kTitleColor Alignment:NSTextAlignmentLeft]; [self.view addSubview:label]; timeLabel = label; x = kSize.width - 100; y += 9; w = 90; h = 42; 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; [self.view addSubview:btn]; } -(void)dismissNavgation { //推送过来 返回主页面 [myDelegate gotoLoad]; } -(void)btnClick:(UIButton*)sender { int tag = (int)sender.tag; if (1 == tag) { [self saveReserveInfo]; } } -(void)setLabelUIWithDic:(NSDictionary *)dic { if (dic.allKeys.count == 1) { //这个if是应对虚拟数据的 nameLabel.text = @"当前计划信息:暂无"; countLabel.text = @"暂无预约人数信息"; return; } NSString *kmString = @"科目二"; if ([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; } #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.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; [formatter setDateFormat:@"yyyy-MM-dd"]; taskTime = [formatter stringFromDate:date]; if (lastIndex) { PlanCollectionCell *lastCell = (PlanCollectionCell *)[mainCollection cellForItemAtIndexPath:lastIndex]; lastCell.backImageView.image = [UIImage imageNamed:@"plancell3.png"]; lastCell.isSelected = NO; if (selectModel.count > 0) { [selectModel removeAllObjects]; } timeLabel.text = @"您已选了0个预约哦"; } [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 rq_defaultDateFormatter]; 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 ([dic[@"STATUS"] isEqualToString:@"0"]) { cell.kmLabel.text = @"被拒,已过期"; }else if ([dic[@"STATUS"] isEqualToString:@"1"]){ cell.kmLabel.text = @"已预约,已过期"; }else if ([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 = [dic[@"KM"] isEqualToString:@"2"]?@"科目二":@"科目三"; if (cell.isSelected) { cell.backImageView.image = [UIImage imageNamed:@"plancell2.png"]; } if ([dic[@"STATUS"] isEqualToString:@"0"]) { cell.kmLabel.text = @"预约被拒"; cell.kmLabel.textColor = [UIColor orangeColor]; }else if ([dic[@"STATUS"] isEqualToString:@"1"]){ cell.kmLabel.text = @"已预约"; cell.kmLabel.textColor = [UIColor orangeColor]; }else if ([dic[@"STATUS"] isEqualToString:@"2"]){ cell.kmLabel.text = @"预约中"; cell.kmLabel.textColor = [UIColor orangeColor]; }else if ([dic[@"STATUS"] isEqualToString:@"-1"]){ if ([dic[@"PI_HAV_NUM"] isEqualToString: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 > 0) { NSDictionary *dic = models[indexPath.row]; [self setLabelUIWithDic:dic]; //NSLog(@"进了没---->%@",dic[@"STATUS"]); if ([dic[@"STATUS"] isEqualToString:@"0"]) { //拒绝 remindLabel.text = @" 教练已拒绝您的预约申请,试试其他的吧"; }else if ([dic[@"STATUS"] isEqualToString:@"1"]){ //已预约 remindLabel.text = @" 教练已同意您的预约申请"; }else if ([dic[@"STATUS"] isEqualToString:@"2"]){ //待审核 remindLabel.text = @" 您的预约申请正在等待审核,请耐心等待"; }else if ([dic[@"STATUS"] isEqualToString:@"-1"]){ if ([dic[@"PI_HAV_NUM"] isEqualToString:dic[@"PI_NUM"]]) { //已约满 remindLabel.text = @" 本条计划已被约满,试试其他的吧"; }else{ //正常可约 remindLabel.text = @" 每条计划只能取消预约一次哦,请您慎重"; } } } if (cell.isCanSelect == NO) { return; } //如果之前操作过 找到改变状态 移除数据 if (lastIndex) { if (![lastIndex isEqual:indexPath]) { PlanCollectionCell *lastCell = (PlanCollectionCell *)[collectionView cellForItemAtIndexPath:lastIndex]; if (lastCell.isSelected == YES) { lastCell.isSelected = NO; lastCell.backImageView.image = [UIImage imageNamed:@"plancell3.png"]; if (selectModel.count > 0) { [selectModel removeAllObjects]; } } } } if (cell.isSelected == YES) { if ([selectModel containsObject:models[indexPath.row]]) { [selectModel removeObject:models[indexPath.row]]; } cell.backImageView.image = [UIImage imageNamed:@"plancell3.png"]; }else{ [selectModel addObject:models[indexPath.row]]; cell.backImageView.image = [UIImage imageNamed:@"plancell2.png"]; } timeLabel.text = [NSString stringWithFormat:@"您已选了%d个预约哦",(int)selectModel.count]; //改变按钮状态 cell.isSelected = !cell.isSelected; //记录上一次点击按钮 lastIndex = indexPath; } #pragma mark - 获取数据 -(void)getPlanInfos { if (![Util connectedToNetWork]) { [holderV setHidden:NO]; return; } if (!taskTime) { NSDateFormatter* fm = [NSDateFormatter new]; fm.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; [fm setDateFormat:@"yyyy-MM-dd"]; taskTime = [fm stringFromDate:[NSDate date]]; } if (_coachDic) { coachNum = _coachDic[@"SFZHM"]; } if (coachNum.length < 1) { coachNum = defUser.userDict[@"sjjlbh"]; } NSMutableArray *arr=[NSMutableArray array]; [arr addPro:@"taskTime" Value:taskTime]; //要调接口获取教练 [arr addPro:@"account" Value:coachNum]; [arr addPro:@"userId" Value:defUser.userDict[@"id"]]; [arr addPro:@"isPage" Value:@""]; [arr addPro:@"pageSize" Value:@""]; [arr addPro:@"currentPage" Value:@""]; NSString* method = @"getPlanInfos"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary *root) { //NSLog(@"找计划------>%@------>%@",arr,root); if (!root) { [LoadingView showMsg:@"网络请求失败"]; [holderV setHidden:NO]; return; } NSString* code = root[@"code"]; if (code.intValue >0) { ShowMsgFailed(); [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 rq_defaultDateFormatter]; 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]; } #pragma mark 保存预约信息 -(void)saveReserveInfo { NSDictionary *dic; if (selectModel.count > 0) { dic = [selectModel firstObject]; }else{ ShowMsg(@"您还未选择预约哦!"); return; } NSMutableArray *arr=[NSMutableArray array]; [arr addPro:@"taskId" Value:dic[@"ID"]]; [arr addPro:@"userId" Value:defUser.userDict[@"id"]]; [arr addPro:@"userName" Value:defUser.userDict[@"userName"]]; [arr addPro:@"userTel" Value:defUser.userDict[@"telphone"]]; NSString* method = @"saveReserveInfo"; [MBProgressHUD showLoadToView:self.view]; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary *root) { [MBProgressHUD hideHUDForView:self.view]; //NSLog(@"保存计划-->%@->%@",arr,root); if (!root) { [LoadingView showMsg:@"操作失败"]; return; } NSString* code = root[@"code"]; if (code.intValue == 1) { ShowMsg(root[@"body"]); [self playAudioWithString:root[@"body"]]; return; } if (code.intValue == 2) { //账户没有开户 [self playAudioWithString:root[@"body"]]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:root[@"body"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alert show]; return; } if (code.intValue == 3) { //余额不足 跳转余额页面 [self playAudioWithString:[NSString stringWithFormat:@"您的账户余额不足,还需要充值%@元",root[@"body"]]]; lackMoney = root[@"body"]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:[NSString stringWithFormat:@"您的账户余额不足,还需要充值%@元",root[@"body"]] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; alert.tag = 1; [alert show]; } if (code.intValue == 0) { ShowMsgSuc(); [self playAudioWithString:root[@"body"]]; PlanCollectionCell *lastCell = (PlanCollectionCell *)[mainCollection cellForItemAtIndexPath:lastIndex]; lastCell.backImageView.image = [UIImage imageNamed:@"plancell3.png"]; lastCell.isSelected = NO; if (selectModel.count > 0) { [selectModel removeAllObjects]; } timeLabel.text = @"您已选了0个预约哦"; [self getPlanInfos]; } }]; } -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (alertView.tag == 1) { if (buttonIndex != alertView.cancelButtonIndex) { TopUpMoneyVC *topUpVC = [[TopUpMoneyVC alloc] init]; topUpVC.type = 1; topUpVC.neededMoney = lackMoney; [self navPushHideTabbarToVC:topUpVC]; } } } -(void)playAudioWithString:(NSString *)string { [Tools playAudioWithString:string]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end