zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
//
//  PBAddRecordViewController.m
//  IphoneBIMe
//
//  Created by zjf on 2019/2/15.
//  Copyright © 2019 ProBIM. All rights reserved.
//
 
#import "PBAddRecordViewController.h"
#import "PBExamineAddModel.h"
#import "PBProjectModel.h"
#import "PBExamineListModel.h"
#import "PBResultModel.h"
#import "PBExamineTableHeaderView.h"
#import "PBVideoPlayViewController.h"
#import "PBCheckHeaderView.h"
#import "PBExamineAddModel.h"
#import "PBRecordInputTableViewCell.h"
#import "PBStatusTableViewCell.h"
#import "PBTopTitleTableViewCell.h"
#import "LXFCameraController.h"
static NSString *const inputCellID = @"inputCellID";
static NSString *const stateCellID = @"stateCellID";
static NSString *const titleCellID = @"titleCellID";
@interface PBAddRecordViewController ()<UITableViewDataSource, UITableViewDelegate, LFImagePickerControllerDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) PBCheckHeaderView *tableheaderView;
@property (nonatomic, strong) NSMutableArray *selectedPhotos;
@property (nonatomic, strong) NSMutableArray *selectedVideo;
@property (nonatomic, strong) NSArray *dataList;
@property (nonatomic, strong) NSMutableArray *selectAssetArr;
@end
 
@implementation PBAddRecordViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.f) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    [self setupNav];
    [self setupUI];
}
- (void)setupNav {
    self.view.backgroundColor = PBColor(244, 245, 246);
    UIView *topV = [[UIView alloc] init];
    topV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topV];
    [topV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.equalTo(@50);
    }];
    UIButton *cancelBtn = [UIButton z_textButton:@"取消" fontSize:16 normalColor:PBColor(97, 111, 125)];
    [cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(16);
        make.left.equalTo(self.view).offset(20);
        make.size.mas_equalTo(CGSizeMake(34, 18));
    }];
    UILabel *titleL = [UILabel z_labelWithText:@"验收" Color:PBColor(97, 111, 125) isBold:YES Font:16];
    titleL.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(16);
        make.size.mas_equalTo(CGSizeMake(69, 18));
        make.centerX.equalTo(self.view);
    }];
    UIView *linV = [[UIView alloc] init];
    linV.backgroundColor = PBColor(233, 235, 237);
    [self.view addSubview:linV];
    [linV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topV.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@1);
    }];
}
- (void)cancelAction {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)setupUI {
    __weak typeof(self) weakSelf = self;
    self.tableView = [[UITableView alloc] init];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 100.f;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.tableView.bounces = NO;
    [self.tableView registerClass:[PBRecordInputTableViewCell class] forCellReuseIdentifier:inputCellID];
    [self.tableView registerClass:[PBStatusTableViewCell class] forCellReuseIdentifier:stateCellID];
    [self.tableView registerClass:[PBTopTitleTableViewCell class] forCellReuseIdentifier:titleCellID];
    [self.view addSubview:self.tableView];
    CGFloat bottomH = 64.f;
    if (IS_IPHONE_X) {
        bottomH = 64.f + IPHONE_X_BOTTOM;
    }
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(51);
        make.left.equalTo(self.view).offset(10);
        make.right.equalTo(self.view).offset(-10);
        make.bottom.equalTo(self.view).offset(-(bottomH));
    }];
    UIButton *applyBtn = [UIButton z_bgImageButton:[UIImage imageNamed:@"acceptance_nor"]];
    [applyBtn addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:applyBtn];
    [applyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tableView.mas_bottom).offset(7);
        make.left.equalTo(self.view).offset(10);
        make.right.equalTo(self.view).offset(-10);
        make.height.equalTo(@50);
    }];
    self.tableheaderView = [[PBCheckHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 100)];
    self.tableheaderView.ChooseImageBlock = ^{
        [weakSelf.view endEditing:YES];
        [weakSelf chooseImageOrVideo];
    };
    self.tableheaderView.DeleteImageBlock = ^(NSInteger index) {
        [weakSelf.view endEditing:YES];
        [weakSelf.selectedPhotos removeObjectAtIndex:index];
        [weakSelf setTableHeaderViewImage];
    };
    self.tableheaderView.DeleteVideoBlock = ^{
        [weakSelf.view endEditing:YES];
        [weakSelf.selectedVideo removeAllObjects];
        [weakSelf setTableHeaderViewImage];
    };
    self.tableheaderView.PlayVideoBlock = ^{
        [weakSelf.view endEditing:YES];
        NSLog(@"打开视频");
        PBVideoPlayViewController *playVC = [[PBVideoPlayViewController alloc] init];
        PBResultVideo *result = weakSelf.selectedVideo[0];
        playVC.filePath = result.url;
        [weakSelf.navigationController pushViewController:playVC animated:YES];
    };
    self.tableheaderView.projectModel = self.projectModel;
    self.tableheaderView.isAddExamine = YES;
    self.tableView.tableHeaderView = self.tableheaderView;
}
- (void)chooseImageOrVideo {
    if (self.selectedPhotos.count > 0) {
        [self takePhotoLibrary];
        return;;
    }
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"请选择" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *videoAction  = [UIAlertAction actionWithTitle:@"拍摄" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self videoBtnFunc];
    }];
    UIAlertAction *imageAction  = [UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self takePhotoLibrary];
    }];
    UIAlertAction *cancelAction  = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertVC addAction:videoAction];
    [alertVC addAction:imageAction];
    [alertVC addAction:cancelAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}
- (void)videoBtnFunc {
    LXFCameraController *cameraController = [LXFCameraController defaultCameraController];
    
    __weak LXFCameraController *weakCameraController = cameraController;
    
    cameraController.takePhotosCompletionBlock = ^(UIImage *image, NSError *error) {
        NSLog(@"takePhotosCompletionBlock");
        [weakCameraController dismissViewControllerAnimated:YES completion:nil];
        NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
        UIImage *originalImage = [UIImage imageWithData:imageData];
        [self.selectedPhotos addObject:originalImage];
        [self setTableHeaderViewImage];
    };
    
    cameraController.shootCompletionBlock = ^(NSURL *videoUrl, CGFloat videoTimeLength, UIImage *thumbnailImage, NSError *error) {
        NSLog(@"shootCompletionBlock");
        [weakCameraController dismissViewControllerAnimated:YES completion:nil];
        PBResultVideo *video = [[PBResultVideo alloc] init];
        video.coverImage = thumbnailImage;
        video.url = videoUrl;
        video.data = [NSData dataWithContentsOfURL:videoUrl];
        [self.selectedVideo addObject:video];
        [self setTableHeaderViewImage];
    };
    
    cameraController.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:cameraController animated:YES completion:nil];
}
//新增图片选择图片后填充
- (void)setTableHeaderViewImage {
    if (self.selectedVideo.count > 0) {
        self.tableheaderView.videoArr = self.selectedVideo;
    }else {
        self.tableheaderView.imageArr = self.selectedPhotos;
    }
}
- (void)takePhotoLibrary {
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusNotDetermined) {//未作出回应
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            if(status == PHAuthorizationStatusAuthorized) {
                dispatch_async(dispatch_get_main_queue(), ^{//用户点击ok
                    [self pushLFImagePickerController];
                });
            } else {
                dispatch_async(dispatch_get_main_queue(), ^{//点击不允许访问
                    NSLog(@"点击不允许访问");
                });
            }
        }];
    }else if (status == PHAuthorizationStatusRestricted) {//没有被授权访问 可能家长控制
 
    }else if (status == PHAuthorizationStatusDenied) {//已明确否认相册权限
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"无法访问相册" message:@"请在iPhone的""设置-隐私-相册""中允许访问相册" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (@available(iOS 10.0, *)) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
            } else {
                NSLog(@"选择取消");
            }
        }];
        [alertController addAction:cancelAction];
        [alertController addAction:okAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }else if (status == PHAuthorizationStatusAuthorized){//有权限访问
        [self pushLFImagePickerController];
    }
}
 
- (void)pushLFImagePickerController {
    NSInteger maxImageCount = 9 - self.selectedPhotos.count;
    LFImagePickerController *imagePicker = [[LFImagePickerController alloc] initWithMaxImagesCount:maxImageCount delegate:self];
    imagePicker.maxVideosCount = 1;
    imagePicker.allowTakePicture = YES;//内部显示相机
    imagePicker.isSelectOriginalPhoto = YES;//是否选择原图
    if (self.selectedPhotos.count > 0) {
        imagePicker.allowPickingType = LFPickingMediaTypePhoto;//用户是否可以选择视频
    }else {
        imagePicker.allowPickingType = LFPickingMediaTypeALL;//用户是否可以选择视频
    }
    imagePicker.allowPickingOriginalPhoto = NO;//原图按钮将隐藏,用户将不能发送原图
    imagePicker.maxPhotoBytes = 1024 * 1024 * 100;//最大图片所选字节大小
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0f) {
        imagePicker.syncAlbum = YES; /** 实时同步相册 */
    }
//    imagePicker.selectedAssets = self.selectAssetArr.copy;
    [self presentViewController:imagePicker animated:YES completion:nil];
}
#pragma mark - LFImagePickerControllerDelegate
 
- (void)lf_imagePickerController:(LFImagePickerController *)picker didFinishPickingResult:(NSArray <LFResultObject /* <LFResultImage/LFResultVideo> */*> *)results {
    NSMutableArray *videoArr = [[NSMutableArray alloc] init];
    NSMutableArray *imageArrM = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < results.count; i++) {
        LFResultObject *result = results[i];
        if ([result isKindOfClass:[LFResultImage class]]){
            LFResultImage *resultImage = (LFResultImage *)result;
            NSData *imageData = UIImageJPEGRepresentation(resultImage.originalImage, 0.5);
            UIImage *originalImage = [UIImage imageWithData:imageData];
            [imageArrM addObject:originalImage];
        }else {
            LFResultVideo *resultVideo = (LFResultVideo *)result;
            PBResultVideo *video = [[PBResultVideo alloc] init];
            video.coverImage = resultVideo.coverImage;
            video.url = resultVideo.url;
            video.data = resultVideo.data;
            [videoArr addObject:video];
        }
    }
    if (videoArr.count > 0) {
        self.selectedVideo = videoArr;
        [self.selectedPhotos removeAllObjects];
    }else {
        [self.selectedPhotos addObjectsFromArray:imageArrM.copy];
    }
    [self setTableHeaderViewImage];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        PBRecordInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:inputCellID forIndexPath:indexPath];
        PBExamineAddModel *examineAddModel = self.dataList[indexPath.row];
        cell.examineAddModel = examineAddModel;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.row == 1) {
        PBTopTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:titleCellID forIndexPath:indexPath];
        PBExamineAddModel *examineAddModel = self.dataList[indexPath.row];
        cell.examineAddModel = examineAddModel;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else {
        PBStatusTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:stateCellID forIndexPath:indexPath];
        PBExamineAddModel *examineAddModel = self.dataList[indexPath.row];
        cell.examineAddModel = examineAddModel;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
#pragma mark - 提交保存数据
- (void)saveAction {
    [self.view endEditing:NO];
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"确认保存" message:@"保存后将不可更改,是否确认保存" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *saveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self saveTextDataAndExamine];
    }];
    UIAlertAction *canceAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertVC addAction:saveAction];
    [alertVC addAction:canceAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}
- (void)saveTextDataAndExamine {
    NSMutableArray *arrM;
    if (self.selectedVideo.count > 0) {
        arrM = self.selectedVideo;
    }else {
        arrM = self.selectedPhotos;
    }
    PBExamineAddModel *examindeAddModel = self.dataList[0];
    NSString *remark = examindeAddModel.dataStr;
    
    PBExamineAddModel *examindeAddModel1 = self.dataList[2];
    NSString *result = @"0";
    BOOL updateFlag = NO;
    if ([examindeAddModel1.dataStr isEqualToString:@"已合格"]) {
        result = @"1";
        updateFlag = YES;
    }
    [YJProgressHUD showProgress:@"" inView:self.view];
    [[PBNetworkTools sharedTools] tryToAcceptanceWithExamineID:self.examineListModel.ExamineID andOrganizeId:self.projectModel.organizeid andRectificationRemark:remark andIsPassed:result andImageArr:arrM andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) {
       if (error) {
           [YJProgressHUD showMessage:@"保存失败" inView:self.view];
           return;
       }
        NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
        PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str];
        if (networkModel.Ret == 1) {
            [YJProgressHUD hide];
            if (self.UpdateRecordListBlock) {
                
                self.UpdateRecordListBlock(updateFlag);
            }
            [self dismissViewControllerAnimated:YES completion:nil];
        }else {
            [YJProgressHUD showMessage:networkModel.Msg inView:self.view];
        }
    }];
}
- (NSArray *)dataList {
    if (_dataList == nil) {
        PBExamineAddModel *examineAddModel = [[PBExamineAddModel alloc] init];
        examineAddModel.prompt = @"请输入验收内容";
        PBExamineAddModel *examineAddModel1 = [[PBExamineAddModel alloc] init];
        examineAddModel1.title = @"验收情况";
        PBExamineAddModel *examineAddModel2 = [[PBExamineAddModel alloc] init];
        _dataList = @[examineAddModel, examineAddModel1, examineAddModel2];
    }
    return _dataList;
}
- (void)setExamineListModel:(PBExamineListModel *)examineListModel {
    _examineListModel = examineListModel;
}
 
- (void)setProjectModel:(PBProjectModel *)projectModel {
    _projectModel = projectModel;
}
 
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
- (NSMutableArray *)selectedPhotos {
    if (_selectedPhotos == nil) {
        _selectedPhotos = [[NSMutableArray alloc] init];
    }
    return _selectedPhotos;
}
- (NSMutableArray *)selectedVideo {
    if (_selectedVideo == nil) {
        _selectedVideo = [[NSMutableArray alloc] init];
    }
    return _selectedVideo;
}
- (NSMutableArray *)selectAssetArr {
    if (_selectAssetArr == nil) {
        _selectAssetArr = [[NSMutableArray alloc] init];
    }
    return _selectAssetArr;
}
- (void)setIsBackList:(BOOL)isBackList {
    _isBackList = isBackList;
}
 
@end