zjf
2023-03-06 244c476e2a717aede76d6bf036975df8c7d31309
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
//
//  PBEnterpriseCodeController.m
//  IphoneBIMe
//
//  Created by zjf on 2020/6/12.
//  Copyright © 2020 ProBIM. All rights reserved.
//
 
#import "PBEnterpriseCodeController.h"
#import "PBCodeTableViewCell.h"
#import "PBInputCodeTableViewCell.h"
static NSString *const CellID = @"CellID";
static NSString *const InputCellID = @"InputCellID";
@interface PBEnterpriseCodeController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, weak) UILabel *codeTieleL;
@property (nonatomic, weak) UIView *codeBgV;
@property (nonatomic, strong) NSMutableDictionary *inputCellDict;
@property (nonatomic, assign) NSInteger currentSelectIndex;
@end
 
@implementation PBEnterpriseCodeController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.f) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    NSArray *arr = [NSString getApiArrData];
    self.dataList = arr.mutableCopy;
    [self setupUI];
}
- (void)setupUI {
    self.view.backgroundColor = PromptColor;
    self.title = @"企业服务";
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(cancelAction)];
    self.navigationItem.leftBarButtonItem = leftItem;
    UIView *topView = [self creatHeadView];
 
       self.tableView.backgroundColor = PromptColor;
       self.tableView.rowHeight = 166;
       [self.tableView registerClass:[PBCodeTableViewCell class] forCellReuseIdentifier:CellID];
       [self.tableView registerClass:[PBInputCodeTableViewCell class] forCellReuseIdentifier:InputCellID];
//       self.tableView.delegate = self;
//       self.tableView.dataSource = self;
       self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
       self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//       [self.view addSubview:self.tableView];
       [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
           make.top.equalTo(topView.mas_bottom);
           make.bottom.equalTo(self.view);
           make.left.equalTo(self.view).offset(20);
           make.right.equalTo(self.view).offset(-20);
       }];
    
 
//    self.tableView = [[UITableView alloc] init];
//    self.tableView.backgroundColor = PromptColor;
//    self.tableView.rowHeight = 166;
//    [self.tableView registerClass:[PBCodeTableViewCell class] forCellReuseIdentifier:CellID];
//    [self.tableView registerClass:[PBInputCodeTableViewCell class] forCellReuseIdentifier:InputCellID];
//    self.tableView.delegate = self;
//    self.tableView.dataSource = self;
//    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    [self.view addSubview:self.tableView];
//    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(topView.mas_bottom);
//        make.left.right.bottom.equalTo(self.view);
//    }];
//    self.tableView.contentInset = UIEdgeInsetsMake(6, 0, 0, 0);
}
- (void)cancelAction {
    [self dismissViewControllerAnimated:YES completion:nil];
}
 
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == (self.dataList.count - 1)) {
        PBInputCodeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:InputCellID forIndexPath:indexPath];
        cell.dict = self.inputCellDict;
        cell.GetEnterpriseBlock = ^(NSString * _Nonnull code) {
            [self getEnterpriseWithCode:code];
        };
        cell.SolveErr = ^{
            [self.inputCellDict setValue:@"" forKey:@"code"];
            [self.inputCellDict setValue:@"" forKey:@"err"];
        };
        return cell;
    }else {
        PBCodeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID forIndexPath:indexPath];
        cell.dict = self.dataList[indexPath.row + 1];
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == (self.dataList.count - 1)) {
        return;
    }
    if (self.UpdataCodeBlock) {
        self.UpdataCodeBlock(indexPath.row + 1);
       }
       NSArray *arr = [NSString getApiArrData];
       self.dataList = arr.mutableCopy;
       [self setHeaderViewCheck];
       [self.tableView reloadData];
}
 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.dataList.count - 1) {
        return NO;
    }
    NSDictionary *dict = self.dataList[indexPath.row + 1];
    if ([[dict valueForKey:@"Check"] isEqualToString:@"1"]) {
        return NO;
    }
    return YES;
}
- ( UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
    UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        [self.dataList removeObjectAtIndex:indexPath.row + 1];
        completionHandler (YES);
        [self.tableView reloadData];
        if (self.DeleteCodeBlock) {
            self.DeleteCodeBlock(indexPath.row + 1);
        }
    }];
    deleteRowAction.image = [UIImage imageNamed:@"code_delete"];
    deleteRowAction.backgroundColor = PromptColor;
    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
    return config;
}
 
- (void)selectDefault:(UITapGestureRecognizer *)tap {
    if (self.UpdataCodeBlock) {
        self.UpdataCodeBlock(0);
    }
    NSArray *arr = [NSString getApiArrData];
    self.dataList = arr.mutableCopy;
    [self setHeaderViewCheck];
    [self.tableView reloadData];
}
- (void)getEnterpriseWithCode:(NSString *)code {
    if ([code isEqualToString:@""]) {
        return;
    }
    [YJProgressHUD showProgress:@"" inView:self.view];
    [[PBNetworkTools sharedTools] RequestGetUrlsByCodeWithCode:code andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) {
        if (error) {
            NSLog(@"%@",error);
            [YJProgressHUD showMessage:@"设置企业编码失败" inView:self.view];
            [self codeErrWith:code];
            return;
        }
        NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
        PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str];
        if (networkModel.Ret == 1) {
            [YJProgressHUD hide];
            NSDictionary *dict = networkModel.Data;
            NSMutableDictionary *dictM = dict.mutableCopy;
            [dictM setObject:code forKey:@"Code"];
            [dictM setObject:@"1" forKey:@"Check"];
            if (self.SetCodeBlock) {
                self.SetCodeBlock(code, dictM.copy);
            }
            NSArray *arr = [NSString getApiArrData];
            self.dataList = arr.mutableCopy;
            [self setHeaderViewCheck];
            [self.tableView reloadData];
        }else {
            [YJProgressHUD showMessage:@"输入企业编码有误" inView:self.view];
            [self codeErrWith:code];
        }
    }];
}
- (void)codeErrWith:(NSString *)errCode {
    [self.inputCellDict setValue:errCode forKey:@"code"];
    [self.inputCellDict setValue:@"err" forKey:@"err"];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(self.dataList.count - 1) inSection:0];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
- (UIView *)creatHeadView {
    UIView *bgV = [[UIView alloc] init];
    bgV.backgroundColor = PromptColor;
    [self.view addSubview:bgV];
    [bgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.equalTo(@232);
    }];
    UIView *codeBgV = [[UIView alloc] init];
    codeBgV.backgroundColor = [UIColor whiteColor];
    [bgV addSubview:codeBgV];
    [codeBgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgV).offset(20);
        make.left.equalTo(bgV).offset(20);
        make.right.equalTo(bgV).offset(-20);
        make.bottom.equalTo(bgV).offset(-60);
    }];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectDefault:)];
    codeBgV.userInteractionEnabled = YES;
    [codeBgV addGestureRecognizer:tap];
    [codeBgV circleViewWithRadius:6];
    UIImageView *logoImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo_unknown"]];
    [bgV addSubview:logoImageV];
    [logoImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(codeBgV).offset(26);
        make.centerX.equalTo(codeBgV);
        make.size.mas_equalTo(CGSizeMake(36, 36));
    }];
    UIImageView *checkIamgeV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"code_check"]];
    [bgV addSubview:checkIamgeV];
    [checkIamgeV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(codeBgV);
        make.right.equalTo(codeBgV).offset(-20);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    
    UILabel *codeTieleL = [UILabel z_labelWithText:@"管理平台" Color:TitleColor isBold:YES Font:TitleFontSize];
    codeTieleL.textAlignment = NSTextAlignmentCenter;
    [bgV addSubview:codeTieleL];
    [codeTieleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(logoImageV.mas_bottom).offset(26);
        make.left.equalTo(codeBgV).offset(20);
        make.right.equalTo(codeBgV).offset(-20);
        make.height.equalTo(@18);
    }];
    
    UILabel *theirOwnL = [UILabel z_labelWithText:@"自有企业服务" Color:TitleColor isBold:YES Font:13];
    theirOwnL.textAlignment = NSTextAlignmentCenter;
    [bgV addSubview:theirOwnL];
    [theirOwnL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(codeBgV.mas_bottom).offset(30);
        make.size.mas_equalTo(CGSizeMake(84, 16));
        make.centerX.equalTo(codeBgV);
    }];
    UIView *leftLin = [[UIView alloc] init];
    leftLin.backgroundColor = PBColor(233, 235, 237);
    [bgV addSubview:leftLin];
    [leftLin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(theirOwnL);
        make.left.equalTo(codeBgV);
        make.right.equalTo(theirOwnL.mas_left).offset(-15);
        make.height.equalTo(@1);
    }];
    UIView *rightLin = [[UIView alloc] init];
    rightLin.backgroundColor = PBColor(233, 235, 237);
    [bgV addSubview:rightLin];
    [rightLin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(theirOwnL);
        make.left.equalTo(theirOwnL.mas_right).offset(15);
        make.right.equalTo(codeBgV);
        make.height.equalTo(@1);
    }];
    self.codeBgV = codeBgV;
    self.codeTieleL = codeTieleL;
    [self setHeaderViewCheck];
    return bgV;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        UITableViewController *tvc = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
        [self addChildViewController:tvc];
        _tableView = tvc.tableView;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        [self.view addSubview:_tableView];
     }
     return _tableView;
}
- (void)setHeaderViewCheck {
    if (self.dataList.count > 0) {
        NSDictionary *dict = self.dataList[0];
        NSString *check = [dict valueForKey:@"Check"];
        if ([check isEqualToString:@"1"]) {
            self.codeBgV.backgroundColor = PBColor(0,122, 255);
            self.codeTieleL.textColor = [UIColor whiteColor];
        }else {
            self.codeBgV.backgroundColor = [UIColor whiteColor];
            self.codeTieleL.textColor = PBColor(40, 58, 79);
        }
    }
}
- (NSMutableDictionary *)inputCellDict {
    if (_inputCellDict == nil) {
        _inputCellDict = [[NSMutableDictionary alloc] init];
    }
    return _inputCellDict;
}
 
 
 
/*
#pragma mark - Navigation
 
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
 
@end