InspectTaskViewController.m 13.4 KB
Newer Older
admin's avatar
admin committed
1 2 3 4 5 6 7 8 9
//
//  InspectTaskViewController.m
//  redstar
//
//  Created by admin on 15/10/27.
//  Copyright © 2015年 ZWF. All rights reserved.
//

#import "InspectTaskViewController.h"
10 11 12 13 14
#import "InspectSortTableCell.h"

#import "TaskGroup.h"
#import "TaskModel.h"
#import "InspectHeaderView.h"
admin's avatar
admin committed
15
#import "HttpClient.h"
admin's avatar
admin committed
16

17
#import "TaskDetailModel.h"
admin's avatar
admin committed
18
#import <MBProgressHUD.h>
19

admin's avatar
admin committed
20 21 22
#import "InspectNotUploadViewController.h"
#import "InspectUploadedViewController.h"

23
#define kTaskSortCell @"InspectTaskSortCell"
admin's avatar
admin committed
24

25

admin's avatar
admin committed
26 27 28 29 30 31
#import "InspectTaskDetailCell.h"
#define kTaskDetailCell @"inspectTaskDetailCell"

#import "InspectTitleTableViewCell.h"
#define kTAskTitleCell @"inspectTitleTaViewCell"

32
@interface InspectTaskViewController () <UITableViewDelegate, UITableViewDataSource, InspectHeaderDelegate>
33
@property (nonatomic, strong) UITableView *tableView;
34 35

@property (nonatomic, strong) NSArray *taskData;
admin's avatar
admin committed
36
@property (nonatomic, assign) BOOL isOpen;
admin's avatar
admin committed
37
@property (nonatomic, strong) TaskDetailModel *taskDetail;
admin's avatar
admin committed
38 39 40 41
@end

@implementation InspectTaskViewController

admin's avatar
admin committed
42 43 44
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
admin's avatar
admin committed
45
    self.tabBarController.tabBar.hidden = YES;
admin's avatar
admin committed
46 47 48
    self.navigationController.navigationBar.hidden = NO;
}

admin's avatar
admin committed
49 50 51
- (void)viewDidLoad {
    [super viewDidLoad];
    
52 53 54 55 56 57 58 59 60
    self.view.backgroundColor = kSectionBackGroundColor;
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
        self.navigationController.navigationBar.translucent = NO;
    }
    
61 62 63 64 65 66 67 68
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 30, 44);
    [backBtn setImage:[UIImage imageNamed:@"back_btn"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    
    
admin's avatar
admin committed
69 70 71 72 73
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:@"口碑巡检任务"];
    customLab.font = [UIFont boldSystemFontOfSize:19];
    self.navigationItem.titleView = customLab;
74
    
75
    
admin's avatar
admin committed
76 77
    // 口碑巡检明细
    [self requestRankingDetail];
admin's avatar
admin committed
78
    _isOpen = NO;
admin's avatar
admin committed
79 80 81
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(requestCurrentList)
admin's avatar
admin committed
82
                                                 name:kRefreshInspectPointNotification
admin's avatar
admin committed
83
                                               object:nil];
admin's avatar
admin committed
84 85
}

admin's avatar
admin committed
86 87 88 89 90
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

admin's avatar
admin committed
91 92 93 94 95
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

96

admin's avatar
admin committed
97

98
#pragma mark - Private Methods
99

admin's avatar
admin committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

- (void)requestCurrentList
{
    NSString *url = [NSString stringWithFormat:@"%@%@%@/%@",kRedStarURL, kInspectDetailURL, self.uuid, self.store_uuid];
    HttpClient *httpClient = [[HttpClient alloc] initWithUrl:url];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [httpClient getPraiseDetailWithParameters:nil completion:^(id response, NSError *error) {
        NSDictionary *dataDict = response[@"data"];
        TaskDetailModel *taskDetail = [TaskDetailModel taskDetailModelWithDict:dataDict];
        _taskDetail = taskDetail;
        
        NSArray *dataArray = dataDict[@"categories"];
        
        NSMutableArray *tgArray = [NSMutableArray array];
        for (NSDictionary *dict in dataArray) {
            TaskGroup *taskGroup = [TaskGroup taskGroupWithDict:dict];
            [tgArray addObject:taskGroup];
        }
        
        _taskData = tgArray;
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.tableView reloadData];
    }];

}

127 128 129 130 131 132 133
// 返回上一页面
- (void)doBack:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


admin's avatar
admin committed
134 135 136 137
- (void)requestRankingDetail
{
    NSString *url = [NSString stringWithFormat:@"%@%@%@/%@",kRedStarURL, kInspectDetailURL, self.uuid, self.store_uuid];
    HttpClient *httpClient = [[HttpClient alloc] initWithUrl:url];
138
    
admin's avatar
admin committed
139
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
admin's avatar
admin committed
140
    [httpClient getPraiseDetailWithParameters:nil completion:^(id response, NSError *error) {
141 142
        NSDictionary *dataDict = response[@"data"];
        TaskDetailModel *taskDetail = [TaskDetailModel taskDetailModelWithDict:dataDict];
admin's avatar
admin committed
143
        _taskDetail = taskDetail;
144 145 146 147 148 149 150 151 152 153
        
        NSArray *dataArray = dataDict[@"categories"];
        
        NSMutableArray *tgArray = [NSMutableArray array];
        for (NSDictionary *dict in dataArray) {
            TaskGroup *taskGroup = [TaskGroup taskGroupWithDict:dict];
            [tgArray addObject:taskGroup];
        }
        
        _taskData = tgArray;
admin's avatar
admin committed
154 155 156
        
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
157
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
admin's avatar
admin committed
158
        self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, CGFLOAT_MIN)];
admin's avatar
admin committed
159 160
        [MBProgressHUD hideHUDForView:self.view animated:YES];

admin's avatar
admin committed
161 162 163
    }];
}

admin's avatar
admin committed
164 165 166 167 168 169 170
- (void)unfoldCilck:(UIButton *)sender
{
    _isOpen = !_isOpen;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    
}
171
#pragma mark - TableView Delegate/DataSource
172

173 174
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
admin's avatar
admin committed
175
    return _taskData.count + 1;
176 177 178 179 180
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
admin's avatar
admin committed
181 182 183 184 185 186 187 188
    if (section == 0) {
        return 2;
    } else {
        TaskGroup *taskGroup = _taskData[section - 1];
        NSInteger count = taskGroup.isOpened ? taskGroup.answers.count : 0;
        return count;
    }
   
189 190 191 192 193
}

// cell显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
admin's avatar
admin committed
194 195 196 197 198 199 200 201 202 203
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            InspectTaskDetailCell *cell=[tableView dequeueReusableCellWithIdentifier:kTaskDetailCell];
            if (!cell) {
                cell = [[InspectTaskDetailCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kTaskDetailCell];
            }
            cell.taskDetail = _taskDetail;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell.unfoldBtn addTarget:self action:@selector(unfoldCilck:) forControlEvents:UIControlEventTouchUpInside];
            if (_isOpen) {
204 205 206 207 208 209
                if (_taskDetail.remark == nil || _taskDetail.remark == NULL || [_taskDetail.remark isEqual:[NSNull null]] || _taskDetail.remark == Nil || [_taskDetail.remark isEqualToString:@"(null)"]) {
                    cell.introDetailLabel.text = @"";
                    cell.arrowImageView.transform = CGAffineTransformMakeRotation(M_PI);
                } else {
                    cell.introDetailLabel.text = [NSString stringWithFormat:@"%@", _taskDetail.remark];
                }
admin's avatar
admin committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
            } else {
                cell.introDetailLabel.text = @"";
            }

            return cell;

        } else {
            InspectTitleTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:kTAskTitleCell];
            if (!cell) {
                cell = [[InspectTitleTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kTAskTitleCell];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
        
    } else {
        InspectSortTableCell *cell=[tableView dequeueReusableCellWithIdentifier:kTaskSortCell];
        if (!cell) {
            cell = [[InspectSortTableCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kTaskSortCell];
        }
        
        TaskGroup *taskGroup = _taskData[indexPath.section - 1];
        TaskModel *task = taskGroup.answers[indexPath.row];
        cell.task = task;
        cell.titleLabel.text = [NSString stringWithFormat:@"%d、%@", (int)indexPath.row + 1 ,task.title];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        return cell;
239
    }
240

admin's avatar
admin committed
241
    
242 243 244 245
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
admin's avatar
admin committed
246 247 248 249 250
    if (indexPath.section == 0) {
        return;
    } else {
        TaskGroup *taskGroup = _taskData[indexPath.section - 1];
        TaskModel *task = taskGroup.answers[indexPath.row];
admin's avatar
admin committed
251
        
admin's avatar
admin committed
252
        if (task.state == 0) {
admin's avatar
admin committed
253
            
admin's avatar
admin committed
254 255 256
            NSArray *permissions = [[NSUserDefaults standardUserDefaults] objectForKey:@"permissions"];
            if ([permissions containsObject:@"500103"]) {
                
admin's avatar
admin committed
257
                if (task.readonly) {
admin's avatar
admin committed
258 259 260
                    InspectUploadedViewController *inspectVC = [[InspectUploadedViewController alloc] init];
                    inspectVC.questionCount = taskGroup.questionCount;
                    inspectVC.taskModel = task;
admin's avatar
admin committed
261
                    inspectVC.catesgory = taskGroup.category;
262
                    inspectVC.reportCount = taskGroup.reportCount;
admin's avatar
admin committed
263 264 265 266 267 268 269 270
                    [self.navigationController pushViewController:inspectVC animated:YES];
                } else {
                    InspectNotUploadViewController *inspectNotVC = [[InspectNotUploadViewController alloc] init];
                    inspectNotVC.questionCount = taskGroup.questionCount;
                    inspectNotVC.taskModel = task;
                    inspectNotVC.store_uuid = _taskDetail.store_uuid;
                    inspectNotVC.praiseUuid = _taskDetail.uuid;
                    inspectNotVC.questionUuid = task.uuid;
admin's avatar
admin committed
271
                    inspectNotVC.catesgory = taskGroup.category;
272
                    inspectNotVC.reportCount = taskGroup.reportCount;
admin's avatar
admin committed
273 274
                    [self.navigationController pushViewController:inspectNotVC animated:YES];
                }
admin's avatar
admin committed
275 276 277 278
            } else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您没有编辑检查点的权限!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alert show];
            }
admin's avatar
admin committed
279
        } else {
admin's avatar
admin committed
280 281
            InspectUploadedViewController *inspectVC = [[InspectUploadedViewController alloc] init];
            inspectVC.questionCount = taskGroup.questionCount;
282
            inspectVC.reportCount = taskGroup.reportCount;
admin's avatar
admin committed
283
            inspectVC.taskModel = task;
admin's avatar
admin committed
284
            inspectVC.catesgory = taskGroup.category;
admin's avatar
admin committed
285
            [self.navigationController pushViewController:inspectVC animated:YES];
admin's avatar
admin committed
286
        }
admin's avatar
admin committed
287
    }
288 289 290 291
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
admin's avatar
admin committed
292 293 294 295 296
    if (section == 0) {
        return 0;
    } else {
        return 50;
    }
297 298 299 300 301 302 303 304 305 306
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

// 自定义section
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
admin's avatar
admin committed
307 308 309 310 311 312 313 314
    if (section == 0) {
        return nil;
    } else {
        InspectHeaderView *headView = [InspectHeaderView headViewWithTableView:tableView];
        headView.delegate = self;
        headView.taskGroup = _taskData[section - 1];
        return headView;
    }
315 316
}

317 318
- (void)clickHeadView
{
admin's avatar
admin committed
319
    [_tableView reloadData];
320
}
321 322 323 324 325 326 327 328
#pragma mark - lazy loading
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.translatesAutoresizingMaskIntoConstraints = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
admin's avatar
admin committed
329
        [_tableView registerClass:[InspectSortTableCell class] forCellReuseIdentifier:kTaskSortCell];
admin's avatar
admin committed
330 331 332 333 334 335
        [_tableView registerClass:[InspectTaskDetailCell class] forCellReuseIdentifier:kTaskDetailCell];
        [_tableView registerClass:[InspectTitleTableViewCell class] forCellReuseIdentifier:kTAskTitleCell];

        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 150.0;
        
336 337
        [self.view addSubview:_tableView];
        
admin's avatar
admin committed
338
        NSLayoutConstraint *tableTop = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
339 340 341 342 343 344 345 346 347 348 349 350 351 352
        [self.view addConstraint:tableTop];
        
        NSLayoutConstraint *tableLeft = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
        [self.view addConstraint:tableLeft];
        
        NSLayoutConstraint *tableRight = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
        [self.view addConstraint:tableRight];
        
        NSLayoutConstraint *tableBottom = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
        [self.view addConstraint:tableBottom];
    }
    return _tableView;
}

admin's avatar
admin committed
353
@end