// // SkillOrderController.m // model // // Created by JuYi on 2018/7/17. // Copyright © 2018年 Mine. All rights reserved. // 技能订单 #import "SkillOrderController.h" #import "SkillsOrderCell.h" #import "ModelTitleView.h" // 选择按钮 #import "OrderDetailViewController.h" #import "MessageViewController.h" @interface SkillOrderController () { NSInteger index; NSString *selectState; } @property(nonatomic, strong) UITableView *tableView; @property(nonatomic, strong) NSMutableArray *dataArr; @property(nonatomic, assign) NSInteger selectIndex; //0 1 2 3 4 @end @implementation SkillOrderController - (NSMutableArray *)dataArr { if (!_dataArr) { _dataArr = [NSMutableArray arrayWithCapacity:0]; } return _dataArr; } - (void)viewDidLoad { [super viewDidLoad]; index = 1; selectState = @"all"; self.selectIndex = 0; //设置导航条 [self creatNavBar]; //设置子试图 [self creatSubViews]; [self getDataFromNetworking:selectState]; } - (void)viewWillAppear:(BOOL)animated { self.tabBarController.tabBar.hidden = YES; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)creatNavBar { self.title = @"技能订单"; // UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(0, 0, 40, 40); [btn setImage:[UIImage imageNamed:@"fanhui2"] forState:UIControlStateNormal]; btn.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0); // backBtn2 [btn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btn]; UIBarButtonItem *nagetiveSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; // nagetiveSpacer.width = -12;//这个值可以根据自己需要自己调整 self.navigationItem.leftBarButtonItems = @[nagetiveSpacer, leftItem]; } - (void)backClick { [self.navigationController popViewControllerAnimated:YES]; } #pragma mark -- 网络请求 - (void)getDataFromNetworking:(NSString *)state { [MBProgressHUD showHUDAddedTo:self.view animated:YES]; NSString *indexStr = [NSString stringWithFormat:@"%zd", index]; NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:[ModelUser user].modelpk, @"modelpk", state, @"state", indexStr, @"index", @"10", @"size", nil]; [YanCNetWorkManager requestPostWithURLStr:Url_dogetorderlist(PublicUrl) parameters:dic finish:^(id dataDic) { [MBProgressHUD hideHUDForView:self.view animated:YES]; NSString *issuccess = dataDic[@"msg"]; if ([issuccess isEqualToString:@"success"]) { if (index == 1) { [self.dataArr removeAllObjects]; } [self.dataArr addObjectsFromArray:dataDic[@"data"]]; [self.tableView reloadData]; NSString *number = dataDic[@"count"]; NSInteger num = [number integerValue]; [self.tableView.mj_header endRefreshing]; if (num == self.dataArr.count || num == 0) { [self.tableView.mj_footer endRefreshingWithNoMoreData]; } else { [self.tableView.mj_footer endRefreshing]; } } else { [MBProgressHUD showInfo:@"请求失败!"]; } } enError:^(NSError *error) { [MBProgressHUD hideHUDForView:self.view animated:YES]; }]; } // - (void)creatSubViews { NSArray *stateArr = @[@"all", @"1", @"2", @"3"]; ModelTitleView *titlesView = [[ModelTitleView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)]; titlesView.titleArr = @[@"全部", @"待接单", @"进行中", @"已完成"]; __weak typeof(self) weakself = self; titlesView.ModelTitleViewBlock = ^(NSInteger tag) { NSLog(@"tag %ld", tag); weakself.selectIndex = tag; if (self.dataArr.count > 0) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO]; } selectState = stateArr[tag]; index = 1; [self getDataFromNetworking:selectState]; }; [self.view addSubview:titlesView]; // float height = Height_NaviBar; self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(titlesView.frame), ScreenWidth, ScreenHeight - 40 - height) style:UITableViewStyleGrouped]; self.tableView.backgroundColor = [UIColor whiteColor]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.backgroundColor = RGBValueColor(0xf7f7f7, 1.0); self.tableView.showsVerticalScrollIndicator = NO; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshList)]; self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)]; [self.view addSubview:self.tableView]; } - (void)refreshList { index = 1; [self getDataFromNetworking:selectState]; } - (void)loadMoreData { index++; [self getDataFromNetworking:selectState]; } #pragma mark -- UITableViewDelegate, UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.dataArr.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellid = @"cellid"; SkillsOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid]; if (!cell) { cell = [[SkillsOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid]; } cell.arrowView.hidden = YES; [cell setSkillsOrderCellValueWithDic:self.dataArr[indexPath.section]]; cell.cancelBtn.tag = indexPath.section; cell.receiveBtn.tag = indexPath.section; cell.completeBtn.tag = indexPath.section; cell.iconImageView.tag = indexPath.section; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toMessageView:)]; [cell.iconImageView addGestureRecognizer:tap]; cell.iconImageView.userInteractionEnabled = YES; [cell.cancelBtn addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside]; [cell.receiveBtn addTarget:self action:@selector(receive:) forControlEvents:UIControlEventTouchUpInside]; [cell.completeBtn addTarget:self action:@selector(complete:) forControlEvents:UIControlEventTouchUpInside]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *dic = self.dataArr[indexPath.section]; OrderDetailViewController *vc = [[OrderDetailViewController alloc] initWithOrderpk:dic[@"pk"]]; [self.navigationController pushViewController:vc animated:YES]; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 156; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.01)]; footerView.backgroundColor = RGBValueColor(0xf7f7f7, 1.0); return footerView; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)]; headerView.backgroundColor = RGBValueColor(0xf7f7f7, 1.0); return headerView; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.01; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (section == 0) { return 0.01; } else { return 10; } } - (void)cancel:(UIButton *)button { [MBProgressHUD showHUDAddedTo:self.view animated:YES]; NSDictionary *orderDic = self.dataArr[button.tag]; NSDictionary *dic = @{@"modelpk": [ModelUser user].modelpk, @"orderpk": orderDic[@"pk"]}; [YanCNetWorkManager requestPostWithURLStr:Url_cancelordermodel(PublicUrl) parameters:dic finish:^(id dataDic) { [MBProgressHUD hideHUDForView:self.view animated:YES]; NSString *issuccess = dataDic[@"msg"]; if ([issuccess isEqualToString:@"success"]) { [MBProgressHUD showInfo:@"已取消"]; [self refreshList]; [self postNotification:orderDic[@"memberpk"] andMsg:@"提示,模特取消了您的订单" orderPK:orderDic[@"pk"]]; } else { [MBProgressHUD showInfo:dataDic[@"desc"]]; } } enError:^(NSError *error) { [MBProgressHUD hideHUDForView:self.view animated:YES]; }]; } - (void)receive:(UIButton *)button { [MBProgressHUD showHUDAddedTo:self.view animated:YES]; NSDictionary *orderDic = self.dataArr[button.tag]; NSDictionary *dic = @{@"modelpk": [ModelUser user].modelpk, @"orderpk": orderDic[@"pk"]}; [YanCNetWorkManager requestPostWithURLStr:Url_confirmordermodel(PublicUrl) parameters:dic finish:^(id dataDic) { [MBProgressHUD hideHUDForView:self.view animated:YES]; NSString *issuccess = dataDic[@"msg"]; if ([issuccess isEqualToString:@"success"]) { [MBProgressHUD showInfo:@"接单成功"]; [self refreshList]; [self postNotification:orderDic[@"memberpk"] andMsg:@"恭喜,模特已经接单啦!" orderPK:orderDic[@"pk"]]; } else { [MBProgressHUD showInfo:dataDic[@"desc"]]; } } enError:^(NSError *error) { [MBProgressHUD hideHUDForView:self.view animated:YES]; }]; } - (void)complete:(UIButton *)button { [MBProgressHUD showHUDAddedTo:self.view animated:YES]; NSDictionary *orderDic = self.dataArr[button.tag]; NSDictionary *dic = @{@"orderpk": orderDic[@"pk"]}; [YanCNetWorkManager requestPostWithURLStr:Url_dosettlementorder(PublicUrl) parameters:dic finish:^(id dataDic) { [MBProgressHUD hideHUDForView:self.view animated:YES]; NSString *issuccess = dataDic[@"msg"]; if ([issuccess isEqualToString:@"success"]) { [MBProgressHUD showInfo:@"已确认完成"]; [self refreshList]; [self postNotification:orderDic[@"memberpk"] andMsg:@"模特已经确认完成订单了" orderPK:orderDic[@"pk"]]; } else { [MBProgressHUD showInfo:dataDic[@"desc"]]; } } enError:^(NSError *error) { [MBProgressHUD hideHUDForView:self.view animated:YES]; }]; } - (void)postNotification:(NSString *)memberpk andMsg:(NSString *)msg orderPK:(NSString *)orderPK { NSDictionary *dict = @{ @"content": msg, @"orderPK": orderPK }; NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil]; NSString *content = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSString *str = [NSString stringWithFormat:@"%@/netease?action=sendAttachMsg&from=%@&to=%@&pushcontent=%@&attach=%@", PublicUrl, [ModelUser modelUser].pk, memberpk, msg, content]; [[AHHttpManager sharedManager] POST:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil success:^(id responseObject) { NSLog(@"%@", responseObject); if ([[responseObject objectForKey:@"msg"] isEqualToString:@"200"]) { } else { } } failure:^(NSError *error) { NSLog(@"error = %@", error); }]; } - (void)toMessageView:(UIGestureRecognizer *)recognizer { UIImageView *img = (UIImageView *) recognizer.view; NSDictionary *orderDic = self.dataArr[img.tag]; NIMSession *session = [NIMSession session:[orderDic objectForKey:@"memberpk"] type:NIMSessionTypeP2P]; MessageViewController *mVc = [[MessageViewController alloc] initWithSession:session]; [self.navigationController pushViewController:mVc animated:YES]; } @end