PurchaseNoticeViewController.m 16.7 KB
Newer Older
zhu's avatar
zhu committed
1 2 3 4 5 6 7 8 9 10
//
//  PurchaseNoticeViewController.m
//  XFFruit
//
//  Created by mac on 15/9/5.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "PurchaseNoticeViewController.h"
#import "SeePurchaseNoticeViewController.h"
陈俊俊's avatar
陈俊俊 committed
11
#import "NewPurchaseNoticeViewController.h"
zhu's avatar
zhu committed
12 13
#import "QueryOrder.h"
#import "SortMaskView.h"
陈俊俊's avatar
陈俊俊 committed
14
#import "NoticeBoltView.h"
zhu's avatar
zhu committed
15
#import "MJRefresh.h"
陈俊俊's avatar
陈俊俊 committed
16 17
#import "PurchaseNoticeListCell.h"
#import "PurchaseNotice.h"
陈俊俊's avatar
陈俊俊 committed
18
#import "SurveyUser.h"
zhu's avatar
zhu committed
19 20 21 22 23 24 25 26 27 28 29 30
#define TopMargin 44
#define TableHeight 120
#define LeftMargin 20
#define BtnWidth 60
#define BtnHeight 24
#define BtnSize 16

typedef enum : NSUInteger {
    SortTag = 3000,
    BoltTag
    
} BtnTag;
陈俊俊's avatar
陈俊俊 committed
31
@interface PurchaseNoticeViewController ()<UITableViewDataSource,UITableViewDelegate,BoltMaskViewDelegate,SortMaskViewDelegate,UIGestureRecognizerDelegate>
zhu's avatar
zhu committed
32 33 34 35
{
    UIView *_maskView;
    UIButton *currentBtn;
    SortMaskView *_sortView;
陈俊俊's avatar
陈俊俊 committed
36
    NoticeBoltView *_boltView;
zhu's avatar
zhu committed
37 38 39 40 41 42 43 44 45
    UIBarButtonItem  *_sureBtn;
    UIButton *_sortBtn;
    UIButton *_boltBtn;
    BOOL _isRefresh;
    BOOL _isLoadMore;
    NSInteger _currentPage;
}
@property (nonatomic,strong)NSMutableArray *dataArr;
@property (nonatomic,strong)UITableView *tableView;
陈俊俊's avatar
陈俊俊 committed
46
@property (nonatomic,strong)NSString *titleLike;
zhu's avatar
zhu committed
47 48 49 50 51 52 53 54 55
@property (nonatomic,strong)NSString *state;
@property (nonatomic,strong)NSString *billLike;
@property (nonatomic,strong)NSString *orderDirection;//asc(升序),desc(降序)
@end

@implementation PurchaseNoticeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
zhu's avatar
zhu committed
56
    [self initData];
zhu's avatar
zhu committed
57 58 59
    [self createTableView];
    [self createRefresh];
    [self getData];
陈俊俊's avatar
陈俊俊 committed
60
    [self createBackBtn];
zhu's avatar
zhu committed
61 62 63
}

- (void)initData{
zhu's avatar
zhu committed
64
    self.view.backgroundColor  = XXFBgColor;
zhu's avatar
zhu committed
65 66 67
    self.dataArr = [NSMutableArray array];
}
- (void)getData{
68 69 70 71
    [ICRUserUtil sharedInstance].needFresh = NO;
    __weak typeof(self)weakSelf = self;
    void(^succ)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
zhu's avatar
zhu committed
72 73 74 75 76 77 78 79
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        [strongSelf fetchtPuchaseList:data];
    };
    void(^fail)(id) = ^(id data) {
        [IBTLoadingView hideHUDWithText:nil];
        [IBTLoadingView showTips:data];
    };
    [IBTLoadingView showProgressLabel:@"正在加载..."];
80
    //    ICRUserUtil *userUtil = [ICRUserUtil sharedInstance];
zhu's avatar
zhu committed
81 82 83 84
    
    NSMutableArray *orderArr =[NSMutableArray array];
    if (self.orderDirection.length > 0 && ![self.orderDirection isEqualToString:@"none"]) {
        QueryOrder *order = [QueryOrder new];
陈俊俊's avatar
陈俊俊 committed
85
        order.field = @"created";
zhu's avatar
zhu committed
86 87 88 89 90 91 92 93 94 95 96
        order.direction = self.orderDirection;
        [orderArr addObject:[order dictForCommit]];
    }
    id billObject = [NSNull null];
    if (self.billLike.length > 0) {
        billObject = self.billLike;
    }
    id stateObject = [NSNull null];
    if (self.state.length > 0 && ![self.state isEqualToString:@"none"]) {
        stateObject = self.state;
    }
陈俊俊's avatar
陈俊俊 committed
97
    id titleObject = [NSNull null];
陈俊俊's avatar
陈俊俊 committed
98
    if (self.titleLike.length > 0) {
陈俊俊's avatar
陈俊俊 committed
99
        titleObject = self.titleLike;
zhu's avatar
zhu committed
100 101 102
    }
    if (orderArr.count == 0) {
        QueryOrder *order = [QueryOrder new];
陈俊俊's avatar
陈俊俊 committed
103
        order.field = @"created";
zhu's avatar
zhu committed
104 105 106 107 108 109 110
        order.direction = @"desc";
        [orderArr addObject:[order dictForCommit]];
        
    }
    NSDictionary *dict = @{
                           @"billNumberLike":billObject,
                           @"state":stateObject,
陈俊俊's avatar
陈俊俊 committed
111 112
                           @"title":titleObject,
                           @"queryOrders":orderArr,
zhu's avatar
zhu committed
113 114
                           @"pageNumber":@(_currentPage),
                           @"pageSize":@(20)};
陈俊俊's avatar
陈俊俊 committed
115
    [[ICRHTTPController sharedController] getPurchaseNoticeListWithData:dict success:succ failure:fail];
zhu's avatar
zhu committed
116
}
zhu's avatar
zhu committed
117
//刷新列表
zhu's avatar
zhu committed
118 119 120 121 122 123
- (void)fetchtPuchaseList:(id)data{
    if (data) {
        NSInteger success = [data[@"success"] integerValue];
        NSString *message  = data[@"message"] ;
        if (success == 1) {
            if (_currentPage == 0) {
陈俊俊's avatar
陈俊俊 committed
124 125 126
                if (self.dataArr.count > 0) {
                    [self.dataArr removeAllObjects];
                }
zhu's avatar
zhu committed
127 128 129
            }
            NSArray *recodesArr = data[ @"data" ][ @"records" ];
            for (NSDictionary *purchaseBillDict in recodesArr) {
陈俊俊's avatar
陈俊俊 committed
130
                PurchaseNotice *purchaseBill = [[PurchaseNotice alloc]init];
zhu's avatar
zhu committed
131
                [purchaseBill setValuesForKeysWithDictionary:purchaseBillDict];
陈俊俊's avatar
陈俊俊 committed
132 133 134 135 136 137 138
                NSMutableArray *users = [NSMutableArray array];
                for (NSDictionary *userDict in purchaseBill.purchasers) {
                    SurveyUser *user = [SurveyUser new];
                    [user setValuesForKeysWithDictionary:userDict];
                    [users addObject:user];
                }
                purchaseBill.purchasers = users;
zhu's avatar
zhu committed
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
                [self.dataArr addObject:purchaseBill];
            }
            NSDictionary *pageDict = data[ @"data" ][ @"paging" ];
            NSInteger pageCount = [pageDict[@"pageCount"] integerValue];
            [self.tableView reloadData];
            [self endRefreshing];
            if (pageCount <= _currentPage) {
                [self.tableView.footer noticeNoMoreData];
            }
            if (_currentPage == 0 && self.dataArr.count > 0) {
                [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
            }
        }else{
            [IBTLoadingView showTips:message];
        }
    }else{
        [IBTLoadingView showTips:@"      无记录      "];
    }
    
    
}
- (void)createRefresh{
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (_isRefresh) {
            return ;
        }
        _isRefresh = YES;
        _currentPage = 0;
        [self getData];
    }];
陈俊俊's avatar
陈俊俊 committed
169
    self.tableView.header.lastUpdatedTimeKey = PurchaseNoticeUpdateDate;
zhu's avatar
zhu committed
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
    
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (_isLoadMore) {
            return ;
        }
        _isLoadMore = YES;
        _currentPage ++;
        [self getData];
    } ];
}
#pragma mark - 结束刷新
- (void)endRefreshing{
    _isLoadMore = NO;
    _isRefresh = NO;
    [self.tableView.header endRefreshing];
    [self.tableView.footer endRefreshing];
}
- (void)createTableView
{
    _sureBtn = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(sureClick)];
    self.navigationItem.rightBarButtonItem = _sureBtn;
    
    _sortBtn = [IBTCustomButtom creatButtonWithFrame:CGRectMake(LeftMargin, 10, BtnWidth, BtnHeight) target:self sel:@selector(sortClick:) tag:SortTag image:@"black_arrow_down_with_text" title:nil titleColor:GXF_DETAIL_COLOR isCorner:NO corner:0 bgColor:nil];
    _sortBtn.titleLabel.font = FontSize(BtnSize);
    [self.view addSubview:_sortBtn];
    
    _boltBtn = [IBTCustomButtom creatButtonWithFrame:CGRectMake(ScreenSize.width-BtnWidth - LeftMargin,10,BtnWidth, BtnHeight) target:self sel:@selector(sortClick:) tag:BoltTag image:@"black_filter_with_text" title:nil titleColor:GXF_DETAIL_COLOR isCorner:NO corner:0 bgColor:nil];
    _boltBtn.titleLabel.font = FontSize(BtnSize);
    [self.view addSubview:_boltBtn];
    
    
Sandy's avatar
Sandy committed
201
    self.tableView = [[UITableView alloc]initWithFrame:(CGRectMake(0, TopMargin,ScreenSize.width, ScreenHeight - 64 - TopMargin)) style:(UITableViewStylePlain)];
zhu's avatar
zhu committed
202 203 204 205
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
Sandy's avatar
Sandy committed
206
    _maskView = [[UIView alloc]initWithFrame:CGRectMake(0, TopMargin, ScreenSize.width, ScreenHeight - 64- TopMargin)];
zhu's avatar
zhu committed
207
    _maskView.backgroundColor = RGBA(0, 0, 0, 0.5);
陈俊俊's avatar
陈俊俊 committed
208 209 210
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenMaskView)];
    [_maskView addGestureRecognizer:tap];
    tap.delegate = self;
zhu's avatar
zhu committed
211 212 213
    _maskView.hidden = YES;
    [self.view addSubview:_maskView];
}
陈俊俊's avatar
陈俊俊 committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
#pragma mark - 手势代理方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    // 输出点击的view的类名
    NSLog(@"%@", NSStringFromClass([touch.view class]));
    
    // 若为UITableViewCellContentView(即点击了tableViewCell),则不截获Touch事件
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    return  YES;
}
#pragma mark - 隐藏maskView
- (void)hiddenMaskView{
    if (_sortView) {
        [self hiddenSortMaskView:YES];
    }
    if (_boltView) {
        [self hiddenBoltMaskView:YES];
    }
}
zhu's avatar
zhu committed
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
#pragma mark -按钮事件
- (void)sortClick:(UIButton *)btn{
    _maskView.backgroundColor = RGBA(0, 0, 0, 0.5);
    _maskView.hidden = NO;
    if (btn.tag == SortTag) {
        if(_boltView){
            [self hiddenBoltMaskView:NO];
        }
        if (currentBtn == btn) {
            [btn setImage:[UIImage imageNamed:@"black_arrow_down_with_text"] forState:UIControlStateNormal];
            [self hiddenSortMaskView:YES];
            currentBtn = nil;
        }else{
            [btn setImage:[UIImage imageNamed:@"select_arrow_up_text"] forState:UIControlStateNormal];
            if (_sortView == nil) {
                _sortView = [[SortMaskView alloc]initWithFrame:CGRectMake(0, -105, ScreenSize.width, 150) withOrderDirection:self.orderDirection];
                _sortView.dataArr = [[NSMutableArray alloc]initWithObjects:@"按时间顺序",@"按时间逆序",@"默认排序",nil];
                _sortView.delegate = self;
                [_maskView addSubview:_sortView];
                [UIView animateWithDuration:0.25 animations:^{
                    CGRect sortFrame = _sortView.frame;
                    sortFrame.origin.y = 0;
                    _sortView.frame = sortFrame;
                }];
            }
            currentBtn = btn;
        }
    }else if(btn.tag == BoltTag){
        if (_sortView) {
            [self hiddenSortMaskView:NO];
        }
        if (currentBtn == btn) {
            [btn setImage:[UIImage imageNamed:@"black_filter_with_text"] forState:UIControlStateNormal];
            [self hiddenBoltMaskView:YES];
            currentBtn = nil;
            
        }else{
            [btn setImage:[UIImage imageNamed:@"selected-filter_with_text"] forState:UIControlStateNormal];
            if (_boltView == nil) {
陈俊俊's avatar
陈俊俊 committed
274
                 _boltView = [[NoticeBoltView alloc]initWithFrame:CGRectMake(0, -105, ScreenSize.width, 274) state:self.state title:self.titleLike bill:self.billLike];
zhu's avatar
zhu committed
275 276
                _boltView.backgroundColor = XXFBgColor;
                _boltView.delegate = self;
陈俊俊's avatar
陈俊俊 committed
277
                _boltView.dataArr = [[NSMutableArray alloc]initWithObjects:@"不限",@"未提交",@"未接受",@"发运中",@"已完成",nil];
zhu's avatar
zhu committed
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
                [_maskView addSubview:_boltView];
                [UIView animateWithDuration:0.25 animations:^{
                    CGRect sortFrame = _boltView.frame;
                    sortFrame.origin.y = 0;
                    _boltView.frame = sortFrame;
                }];
                currentBtn = btn;
            }
        }
    }
}

- (void)hiddenSortMaskView:(BOOL)isHide{
    currentBtn = nil;
    [self checkSortBtnColor];
    if (isHide == NO) {
        [_sortView removeFromSuperview];
        _sortView = nil;
    }else{
        [UIView animateWithDuration:0.25 animations:^{
            CGRect sortFrame = _sortView.frame;
            sortFrame.origin.y = -105;
            _sortView.frame = sortFrame;
            _maskView.backgroundColor = RGBA(0, 0, 0, 0);
        }completion:^(BOOL finished) {
            [_sortView removeFromSuperview];
            _sortView = nil;
            _maskView.hidden = YES;
        }];
    }
}
- (void)hiddenBoltMaskView:(BOOL)isHide{
    currentBtn = nil;
    [self checkBoltBtnColor];
    if (isHide == NO) {
        [_boltView removeFromSuperview];
        _boltView = nil;
    }else{
        [UIView animateWithDuration:0.25 animations:^{
            CGRect sortFrame = _boltView.frame;
            sortFrame.origin.y = -105;
            _boltView.frame = sortFrame;
            _maskView.backgroundColor = RGBA(0, 0, 0, 0);
        }completion:^(BOOL finished) {
            [_boltView removeFromSuperview];
            _boltView = nil;
            _maskView.hidden = YES;
        }];
    }
}

//检测排序的颜色
- (void)checkSortBtnColor{
    if (self.orderDirection.length > 0) {
        [_sortBtn setImage:[UIImage imageNamed:@"select_arrow_up_text"] forState:UIControlStateNormal];
    }else{
        [_sortBtn setImage:[UIImage imageNamed:@"black_arrow_down_with_text"] forState:UIControlStateNormal];
        
    }
}
- (void)checkBoltBtnColor{
陈俊俊's avatar
陈俊俊 committed
339
    if (self.state.length > 0 || self.titleLike.length > 0 || self.billLike.length > 0) {
zhu's avatar
zhu committed
340 341 342 343 344 345 346 347 348
        [_boltBtn setImage:[UIImage imageNamed:@"selected-filter_with_text"] forState:UIControlStateNormal];
    }else{
        [_boltBtn setImage:[UIImage imageNamed:@"black_filter_with_text"] forState:UIControlStateNormal];
        
    }
}
#pragma mark - 确定
- (void)sureClick{
    
陈俊俊's avatar
陈俊俊 committed
349 350 351
    [_boltView.titleFiled resignFirstResponder];
    [_boltView.billFiled resignFirstResponder];
    _currentPage = 0;
zhu's avatar
zhu committed
352
    
陈俊俊's avatar
陈俊俊 committed
353 354 355 356 357 358 359 360 361
    //请求
    [self getData];
    //还原数据
    if (_sortView) {
        [self hiddenSortMaskView:YES];
    }
    if (_boltView) {
        [self hiddenBoltMaskView:YES];
    }
zhu's avatar
zhu committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375
}

#pragma mark - sortDelegate
- (void)getSortValueSelectRow:(NSString *)state{
    if (state.length > 0) {
        self.orderDirection = state;
        CLog(@"%@",state);
    }
}
#pragma mark - boltDelegate
- (void)getBoltValueSelectRow:(NSString *)state{
    self.state = state;
    CLog(@"%@",state);
}
陈俊俊's avatar
陈俊俊 committed
376 377 378
- (void)getBillNumber:(NSString *)billNumber{
    self.billLike = billNumber;
    CLog(@"%@",billNumber);
zhu's avatar
zhu committed
379
}
陈俊俊's avatar
陈俊俊 committed
380 381 382
- (void)getTitleLike:(NSString *)titleLike{
    self.titleLike = titleLike;
    CLog(@"%@",titleLike);
zhu's avatar
zhu committed
383 384 385 386 387 388
}
- (void)clearBoltInformation{
    //清空所有条件
    if (self.billLike.length > 0) {
        self.billLike = @"";
    }
陈俊俊's avatar
陈俊俊 committed
389 390
    if (self.titleLike.length > 0) {
        self.titleLike = @"";
zhu's avatar
zhu committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404
    }
    if (self.state.length > 0) {
        self.state = @"";
    }
}

#pragma mark - 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
陈俊俊's avatar
陈俊俊 committed
405 406
    static NSString *cellID = @"PurchaseNoticeListCell";
    PurchaseNoticeListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
zhu's avatar
zhu committed
407
    if (cell == nil) {
陈俊俊's avatar
陈俊俊 committed
408
        cell = [[PurchaseNoticeListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
zhu's avatar
zhu committed
409 410 411 412
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (self.dataArr.count > 0) {
陈俊俊's avatar
陈俊俊 committed
413 414
        PurchaseNotice *bill = self.dataArr[indexPath.row];
        cell.purchaseNotice = bill;
zhu's avatar
zhu committed
415 416 417 418 419
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
陈俊俊's avatar
陈俊俊 committed
420 421 422 423 424
    if (_dataArr.count > 0) {
        PurchaseNotice *purchaseNotice = _dataArr[indexPath.row];
        if ([purchaseNotice.state isEqualToString:PURCHASENOTICE_STATE_INITIAL]) {
            NewPurchaseNoticeViewController *nvc = [NewPurchaseNoticeViewController new];
            nvc.purchaseNotice = purchaseNotice;
陈俊俊's avatar
陈俊俊 committed
425
            nvc.title = @"修改采购通知单";
陈俊俊's avatar
陈俊俊 committed
426 427 428
            [self PushViewController:nvc animated:YES];
        }else{
            SeePurchaseNoticeViewController *pvc = [SeePurchaseNoticeViewController new];
陈俊俊's avatar
陈俊俊 committed
429
            pvc.title = @"查看采购通知单";
陈俊俊's avatar
陈俊俊 committed
430 431 432
            pvc.purchaseNotice = purchaseNotice;
            [self PushViewController:pvc animated:YES];
        }
zhu's avatar
zhu committed
433 434 435 436 437
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return TableHeight;
}
陈俊俊's avatar
陈俊俊 committed
438 439 440 441 442 443
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if ([ICRUserUtil sharedInstance].needFresh && self.tableView != nil) {
        [self.tableView.header beginRefreshing];
    }
}
zhu's avatar
zhu committed
444

陈俊俊's avatar
陈俊俊 committed
445 446 447 448 449 450 451 452 453 454 455
- (void)createBackBtn{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    btn.frame  = CGRectMake(0, 0, 12, 20);
    [btn addTarget:self action:@selector(backRV) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = back;
}
- (void)backRV{
    [self PopToRootViewControllerAnimated:YES];
}
zhu's avatar
zhu committed
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
/*
 #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