单军华
2018-03-29 89d748b77b478905732e60f0b4c5807c274b6565
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
//
//  HotelDevicePage.m
//  istanbul
//
//  Created by WindShan on 2017/6/16.
//  Copyright © 2017年 WindShan. All rights reserved.
//
 
#import "HotelDevicePage.h"
#import "NetworkSingleton.h"
#import "DeviceSpareModel.h"
#import "HotelDeviceCell.h"
#import "Global.h"
#import "BaseNaviController.h"
#import "ChangeDeviceNamePage.h"
 
@interface HotelDevicePage ()<UITableViewDelegate,UITableViewDataSource,updateDeviceDelegate>
{
    NSInteger       pageIndex;
    NSInteger       pageSize;
    
}
@property (nonatomic, strong) NSMutableArray                * modelArray;
@property (nonatomic, strong) UITableView                   * tableView;
 
@end
 
@implementation HotelDevicePage
 
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self getFirstPageData];
}
 
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    //self.navigationController.navigationBarHidden = NO;
}
 
- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] init];
        
 
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //_tableView.separatorColor = [UIColor clearColor];
        
        [self.view addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsMake(0 , 0, 0, 0));
        }];
        
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}
 
- (void)updateDeviceSection:(DeviceSpareModel*)model
{
    if( model.cmd == 1)
    {
        // 修改
        //UPDATE_SPARE
        ChangeDeviceNamePage *page = [[ChangeDeviceNamePage alloc] initIsFirstPage:NO];
        page.currentModel = model;
        BaseNaviController *baseNav = [[BaseNaviController alloc] initWithRootViewController:page];
        [self presentViewController:baseNav animated:YES completion:nil];
    }
    else if(model.cmd == 2)
    {
        // 删除
        [self deleteDevice:model];
    }
}
 
-(void)setUpTableView
{
    //添加下拉的动画图片
    // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadNewData方法)
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getFirstPageData)];
    
    // 马上进入刷新状态
    [_tableView.mj_header beginRefreshing];
    
    
    // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadMoreData方法)
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(reloadData)];
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"备用设备";
    [self setNavigationLeft:@"返回" sel:@selector(backAticon)];
    pageIndex = 1;
    pageSize  = 20;
    
    self.modelArray = [[NSMutableArray alloc] init];
 
    
    self.tableView.hidden = NO;
    [self setUpTableView];
    // Do any additional setup after loading the view.
}
 
-(void)getFirstPageData
{
    pageIndex=1;
    
    [self.tableView reloadData];
    
    [self getDeviceList:self.hotelid isTips:NO pageIndex:[NSString stringWithFormat:@"%ld",pageIndex] pageSize:[NSString stringWithFormat:@"%ld",pageSize]];
}
 
-(void) reloadData
{
    pageIndex++;
    [self getDeviceList:self.hotelid isTips:NO pageIndex:[NSString stringWithFormat:@"%ld",pageIndex] pageSize:[NSString stringWithFormat:@"%ld",pageSize]];
}
 
-(void) deleteDevice:(DeviceSpareModel*)model
{
    // 执行登录操作
    MBProgressHUD *HUD = nil;
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.labelText = @"数据更新中...";
    // 隐藏时候从父控件中移除
    HUD.removeFromSuperViewOnHide = YES;
    // YES代表需要蒙版效果
    HUD.dimBackground = YES;
    
    
    NSString *path = [[NSString alloc] initWithFormat:@"%@",[NSString stringWithFormat:DELETE_SPARE,model._id]];
    
    NSMutableDictionary *param = [[NSMutableDictionary alloc] init];
  
    MPWeakSelf(self);
    [NetworkSingleton networkingPostMethod:param urlName:path success:^(id responseBody)
     {
         HUD.hidden = YES;
         
         MPStrongSelf(self);
         BaseResModel * resModel = [Global toBaseModel:responseBody];
         
         if(resModel.code == 0)
         {
             [self.modelArray removeObject:model];
             [_tableView reloadData];
             [Global alertMessageEx:resModel.desc title:@"提示信息" okTtitle:nil cancelTitle:@"确定" delegate:self];
         }
         else
         {
             [_tableView reloadData];
             [Global alertMessageEx:resModel.desc title:@"提示信息" okTtitle:nil cancelTitle:@"确定" delegate:self];
         }
     }
     failure:^(NSString *error)
     {
         HUD.hidden = YES;
         
         _tableView.hidden = YES;
         
         
         [Global alertMessageEx:error title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self];
     }];
    
    
}
 
 
-(void) getDeviceList:(NSString *) hotelid  isTips:(BOOL) isShow pageIndex:(NSString *)page pageSize:(NSString *)size
{
    // 执行登录操作
    MBProgressHUD *HUD = nil;
    if( isShow == YES)
    {
        HUD = [MBProgressHUD showHUDAddedTo:self animated:YES];
        HUD.labelText = @"数据加载中...";
        // 隐藏时候从父控件中移除
        HUD.removeFromSuperViewOnHide = YES;
        // YES代表需要蒙版效果
        HUD.dimBackground = YES;
    }
    
 
    NSString *path = [[NSString alloc] initWithFormat:GET_DEVICE_LIST];
    
    NSMutableDictionary *param = [[NSMutableDictionary alloc] init];
    // tempdebug
    [param setObject:hotelid forKey:@"hotel_id"];
    //[param setObject:page forKey:@"pageIndex"];
    //[param setObject:size forKey:@"pageSize"];
    
    
    MPWeakSelf(self);
    [NetworkSingleton networkingPostMethod:param urlName:path success:^(id responseBody)
     {
         if( isShow == YES)
             HUD.hidden = YES;
         
         [_tableView.mj_header endRefreshing];
         [_tableView.mj_footer endRefreshing];
         MPStrongSelf(self);
         BaseResModel * resModel = [Global toBaseModel:responseBody];
         
         
         if(pageIndex == 1)
         {
             [_modelArray removeAllObjects];
         }
         
         if(resModel.code == 0)
         {
             if(![Global isEmptyObject:resModel.content])
             {
                 NSMutableArray *modelTempArray = [DeviceSpareModel mj_objectArrayWithKeyValuesArray:resModel.content];
                 
                 BASE_INFO_FUN(resModel.content);
                 
                 [_modelArray addObjectsFromArray:modelTempArray];
                 
                 _tableView.hidden = NO;
                 [_tableView reloadData];
                 
             }
             else
             {
                 // 出错处理
                 if(self.modelArray.count > 0)
                 {
                     [self.modelArray removeAllObjects];
                 }
                 
                 [_tableView reloadData];
                 _tableView.hidden = YES;
                 
                 [Global alertMessageEx:@"没有相关设备信息!" title:@"提示信息" okTtitle:nil cancelTitle:@"确定" delegate:self];
             }
             
         }
         else
         {
             if(self.modelArray.count > 0)
             {
                 [self.modelArray removeAllObjects];
                 [_tableView reloadData];
             }
             
             _tableView.hidden = YES;
             
             
         }
     }
                                  failure:^(NSString *error)
     {
         [_tableView.mj_header endRefreshing];
         [_tableView.mj_footer endRefreshing];
         if( isShow == YES)
             HUD.hidden = YES;
         
         _tableView.hidden = YES;
         
         
         [Global alertMessageEx:error title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self];
     }];
    
    
}
 
 
#pragma mark - UITableViewDelegate&UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
 
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 0;
//}
//
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArray.count;
}
 
 
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString * identifier = @"hoteldeviceCell";
    HotelDeviceCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (nil == cell) {
        cell = [[HotelDeviceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }
    
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.userInteractionEnabled = YES;
    cell.accessoryType = UITableViewCellAccessoryNone;
    DeviceSpareModel * model = [self.modelArray objectAtIndex:indexPath.row];
    
    [cell setItemView:model];
    
    return cell;
}
 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DeviceSpareModel * model = [self.modelArray objectAtIndex:indexPath.row];
    
    [Global setCurrentDevice:model];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
 
#pragma mark - 补全分隔线左侧缺失
- (void)viewDidLayoutSubviews {
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}
 
 
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}
 
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
 
    // Dispose of any resources that can be recreated.
}
 
#pragma mark custom action begin
-(void)backAticon
{
    [self.navigationController popToViewController:self animated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}
 
/*
#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