InspectTaskViewController.m 12.9 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 127

- (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) {
        NSLog(@"口碑巡检明细PraiseDetail = %@", response);
        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];
    }];

}

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


admin's avatar
admin committed
135 136 137
- (void)requestRankingDetail
{
    NSString *url = [NSString stringWithFormat:@"%@%@%@/%@",kRedStarURL, kInspectDetailURL, self.uuid, self.store_uuid];
138
    NSLog(@"url = %@", url);
admin's avatar
admin committed
139
    HttpClient *httpClient = [[HttpClient alloc] initWithUrl:url];
140
    
admin's avatar
admin committed
141
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
admin's avatar
admin committed
142 143
    [httpClient getPraiseDetailWithParameters:nil completion:^(id response, NSError *error) {
        NSLog(@"口碑巡检明细PraiseDetail = %@", response);
144
        NSLog(@"error = %@", error);
145 146
        NSDictionary *dataDict = response[@"data"];
        TaskDetailModel *taskDetail = [TaskDetailModel taskDetailModelWithDict:dataDict];
admin's avatar
admin committed
147
        _taskDetail = taskDetail;
148 149 150 151 152 153 154 155 156 157
        
        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
158 159 160
        
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
161
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
admin's avatar
admin committed
162
        self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, CGFLOAT_MIN)];
admin's avatar
admin committed
163 164
        [MBProgressHUD hideHUDForView:self.view animated:YES];

admin's avatar
admin committed
165 166 167
    }];
}

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

177 178
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
admin's avatar
admin committed
179
    return _taskData.count + 1;
180 181 182 183 184
}


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

// cell显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
admin's avatar
admin committed
198 199 200 201 202 203 204 205 206 207 208 209 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
    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) {
                cell.introDetailLabel.text = [NSString stringWithFormat:@"%@", _taskDetail.remark];
            } 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;
238
    }
239

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

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

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
admin's avatar
admin committed
285 286 287 288 289
    if (section == 0) {
        return 0;
    } else {
        return 50;
    }
290 291 292 293 294 295 296 297 298 299
}

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

// 自定义section
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
admin's avatar
admin committed
300 301 302 303 304 305 306 307
    if (section == 0) {
        return nil;
    } else {
        InspectHeaderView *headView = [InspectHeaderView headViewWithTableView:tableView];
        headView.delegate = self;
        headView.taskGroup = _taskData[section - 1];
        return headView;
    }
308 309
}

310 311
- (void)clickHeadView
{
admin's avatar
admin committed
312
    [_tableView reloadData];
313
}
314 315 316 317 318 319 320 321
#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
322
        [_tableView registerClass:[InspectSortTableCell class] forCellReuseIdentifier:kTaskSortCell];
admin's avatar
admin committed
323 324 325 326 327 328
        [_tableView registerClass:[InspectTaskDetailCell class] forCellReuseIdentifier:kTaskDetailCell];
        [_tableView registerClass:[InspectTitleTableViewCell class] forCellReuseIdentifier:kTAskTitleCell];

        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 150.0;
        
329 330
        [self.view addSubview:_tableView];
        
admin's avatar
admin committed
331
        NSLayoutConstraint *tableTop = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
332 333 334 335 336 337 338 339 340 341 342 343 344 345
        [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
346
@end