CNMineOrderController.m 10.9 KB
//
//  CNMineOrderController.m
//  CNLiveNetAdd
//
//  Created by 张旭 on 2018/3/1.
//  Copyright © 2018年 cnlive. All rights reserved.
//

#import "CNMineOrderController.h"
#import "CNOrderModel.h"
#import "CNSegmentView.h"

#import "CNMineOrderTableViewCell.h"
#import "CNOrderDetailController.h"
#import "CNOrderListCommonView.h"
#import "UIColor+CNLiveExtension.h"

#define TotalTitleCount 5
#define Segment_Height  45.5

@interface CNMineOrderController ()<UIScrollViewDelegate>
{
    UIButton *_tempBtn;
    NSInteger _curSeleIndex;                //0全部 1待付款 2待收货 3待评价
    CGFloat _oldx;
}
@property (nonatomic, weak) UIScrollView *navScrollView;
@property (nonatomic, weak) UIView *line;
@property (nonatomic, strong) NSMutableArray *titleLists;

@property (nonatomic, strong) CNSegmentView *segmentView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) CNOrderListCommonView *allTblView;
@property (nonatomic, strong) CNOrderListCommonView *payTblView;
@property (nonatomic, strong) CNOrderListCommonView *goodsTblView;
@property (nonatomic, strong) CNOrderListCommonView *evaluateTblView;
@property (nonatomic, strong) UIViewController *vc;
@property (nonatomic, strong) UIViewController *rvc;
@property (nonatomic, assign) BOOL isAddService; //是否添加服务订单页面

@end

@implementation CNMineOrderController
#pragma mark - Init
- (instancetype)initWithType:(NSInteger)type{
    if (self = [super init]) {
        _curSeleIndex = type;
        _index = 0;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBarHidden = NO;
    
//    [self refreshData];

}
- (void)dealloc {
    NSLog(@"订单走了delloc了吗");
}

- (void)viewDidLoad{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
    //标题栏
    [self addTitleLables];
    //服务订单
    if (self.index == 0 && !self.isAddService) {
        [self serviceOrder];
    }
    
}

#pragma mark - UI
/** 添加标题栏 */
- (void)addTitleLables{
    CGFloat width = (KScreenWidth == 320 ? 220 : 280);
    UIScrollView *navScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, width, 44)];
    navScrollView.backgroundColor = [UIColor clearColor];
    navScrollView.showsHorizontalScrollIndicator = NO;
    navScrollView.showsVerticalScrollIndicator = NO;
    self.navigationItem.titleView = navScrollView;
    _navScrollView = navScrollView;

    UIView *line = [[UIView alloc]initWithFrame:CGRectMake((navScrollView.width/self.titleLists.count-50)/2.0, navScrollView.height-4, 50, 4)];
    line.backgroundColor = CNLiveColorWithHexString(KGreenColor);
    line.layer.masksToBounds = YES;
    line.layer.cornerRadius = 1;
    [navScrollView addSubview:line];
    _line = line;
    
    CGFloat lblW = navScrollView.width/self.titleLists.count;
    CGFloat lblH = navScrollView.height-line.height;
    CGFloat lblY = 0;
    for (int i = 0; i < _titleLists.count; i++) {
        CGFloat lblX = i * lblW;
        UIButton *royalBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        royalBtn.frame = CGRectMake(lblX, lblY, lblW, lblH);
        royalBtn.titleLabel.font = [UIFont systemFontOfSize:16];

//        if (0 == i) {
//            _tempBtn = royalBtn;
//            _tempBtn.selected = YES;
//        }
        
        royalBtn.tag = i;
        royalBtn.userInteractionEnabled = YES;
        [royalBtn addTarget:self action:@selector(lblClick:) forControlEvents:UIControlEventTouchUpInside];
        [royalBtn setTitle:_titleLists[i] forState:UIControlStateNormal];
        
        [royalBtn setTitleColor:CNLiveColorWithHexString(@"#4A4A4A") forState:UIControlStateNormal];
        [royalBtn setTitleColor:CNLiveColorWithHexString(KGreenColor) forState:UIControlStateSelected];
        
        royalBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        
        [_navScrollView addSubview:royalBtn];
        
        if (self.index == i) {
            [self lblClick:royalBtn];
        }
        
    }
    
}
/** 服务订单 */
- (void)serviceOrder{
    self.isAddService = YES;
    [self.view addSubview:self.segmentView];
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.allTblView];
    [self.scrollView addSubview:self.payTblView];
    [self.scrollView addSubview:self.goodsTblView];
    [self.scrollView addSubview:self.evaluateTblView];
    
    [_scrollView setContentOffset:CGPointMake(_curSeleIndex * KScreenWidth, 0)];
}
#pragma mark - Action
/** 标题栏label的点击事件 */
- (void)lblClick:(UIButton *)sender{
    _tempBtn.selected = NO;
    sender.selected = YES;
    _tempBtn = sender;
    _line.x = (sender.width-_line.width)*0.5+sender.width*sender.tag;
    if(sender.tag == 0){
        _segmentView.hidden = NO;
        _scrollView.hidden = NO;
        
        [_tempBtn setTitleColor:CNLiveColorWithHexString(KGreenColor) forState:UIControlStateSelected];
        _line.backgroundColor = CNLiveColorWithHexString(KGreenColor);
        if (!self.isAddService) {
            [self serviceOrder];
        }
        [self refreshData];
        self.vc.view.hidden = YES;
        [self.vc.view removeFromSuperview];
        self.rvc.view.hidden = YES;
        [self.rvc.view removeFromSuperview];
        
    }
    else if(sender.tag == 1){
        _segmentView.hidden = YES;
        _scrollView.hidden = YES;
        
        [_tempBtn setTitleColor:CNLiveColorWithHexString(@"#FF725C") forState:UIControlStateSelected];
        _line.backgroundColor = CNLiveColorWithHexString(@"#FF725C");

        self.vc.view.hidden = NO;
        [self.view addSubview:self.vc.view];

        self.rvc.view.hidden = YES;
        [self.rvc.view removeFromSuperview];
        
    }
    else {
        _segmentView.hidden = YES;
        _scrollView.hidden = YES;
        
        [_tempBtn setTitleColor:CNLiveColorWithHexString(@"#FF725C") forState:UIControlStateSelected];
        _line.backgroundColor = CNLiveColorWithHexString(@"#FF725C");
        
        self.vc.view.hidden = YES;
        [self.vc.view removeFromSuperview];

        self.rvc.view.hidden = NO;
        [self.view addSubview:self.rvc.view];
        
    }
    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if ([scrollView isKindOfClass:[UITableView class]]) {
        return;
    }
    [_segmentView setSelectedBtn:scrollView.contentOffset.x/self.view.width];
    if (scrollView.contentOffset.y == 0) {
        static float newx = 0;
        _oldx = _curSeleIndex * self.view.width;
        newx = scrollView.contentOffset.x ;

        if (newx != _oldx ) {
            if (newx > _oldx) {
                //Right
                _curSeleIndex = scrollView.contentOffset.x / self.view.width * 1 ;
                if (_curSeleIndex > (TotalTitleCount - 1)) {
                    _curSeleIndex = TotalTitleCount - 1;
                }
                [self refreshData];
                
            }else if(newx < _oldx){
                //Left
                _curSeleIndex = scrollView.contentOffset.x / self.view.width * 1;
                if (_curSeleIndex < 0) {
                    _curSeleIndex = 0;
                }
                [self refreshData];
            }
        }
    }
}

#pragma mark - Data
//刷新数据
- (void)refreshData {
    
    switch (_curSeleIndex) {
        case 0: {
            [_allTblView refreshData];
        }
            break;
            
        case 1: {
            [_payTblView refreshData];
        }
            break;
            
        case 2: {
            [_goodsTblView refreshData];
        }
            break;
            
        case 3: {
            [_evaluateTblView refreshData];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - Getter
- (CNSegmentView *)segmentView{
    if (!_segmentView) {
        _segmentView = [[CNSegmentView alloc] initWithFrame:CGRectMake(0, kVerticalStatusHeight+44, KScreenWidth, Segment_Height) titles:@[@"全部",@"待付款", @"待收货", @"待评价"] curIndex:_curSeleIndex selectedBlock:^(NSInteger index) {
            
            CGFloat x = index * self.view.width;
            _curSeleIndex = index;
            [_scrollView setContentOffset:CGPointMake(x, 0) animated:NO];
            
            [self refreshData];
            
        }];
    }
    return _segmentView;
}

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kVerticalStatusHeight+44+Segment_Height, KScreenWidth, KScreenHeight-(kVerticalStatusHeight+44)-Segment_Height)];
        _scrollView.contentSize = CGSizeMake(KScreenWidth * 4, KScreenHeight - (kVerticalStatusHeight+44) - Segment_Height);
        _scrollView.pagingEnabled = YES;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.bounces = NO;
        _scrollView.delegate = self;
        _scrollView.scrollEnabled = NO;
    }
    return _scrollView;
}

- (CNOrderListCommonView *)allTblView{
    if (!_allTblView) {
        _allTblView = [[CNOrderListCommonView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, _scrollView.height) orderStatus:@"0"];
    }
    return _allTblView;
}

- (CNOrderListCommonView *)payTblView{
    if (!_payTblView) {
        _payTblView = [[CNOrderListCommonView alloc] initWithFrame:CGRectMake(KScreenWidth, 0, KScreenWidth, _scrollView.height) orderStatus:@"1"];
    }
    return _payTblView;
}

- (CNOrderListCommonView *)goodsTblView{
    if (!_goodsTblView) {
        _goodsTblView = [[CNOrderListCommonView alloc] initWithFrame:CGRectMake(2*KScreenWidth, 0, KScreenWidth, _scrollView.height) orderStatus:@"2"];
    }
    return _goodsTblView;
}

- (CNOrderListCommonView *)evaluateTblView{
    if (!_evaluateTblView) {
        _evaluateTblView = [[CNOrderListCommonView alloc] initWithFrame:CGRectMake(3*KScreenWidth, 0, KScreenWidth, _scrollView.height) orderStatus:@"3"];
    } 
    return _evaluateTblView;
}

- (NSMutableArray *)titleLists{
    if (_titleLists == nil) {
        _titleLists = [NSMutableArray arrayWithObjects:@"服务订单",@"商品订单",@"餐饮订单",nil];
    }
    return _titleLists;
}

- (UIViewController *)vc{
    if (_vc == nil) {
        _vc = [[NSClassFromString(@"DSOrderListViewController") alloc]init];
        _vc.view.frame = CGRectMake(0, kVerticalStatusHeight+44, KScreenWidth, KScreenHeight-(kVerticalStatusHeight+44));
    }
    return _vc;

}

- (UIViewController *)rvc{
    if (_rvc == nil) {
        _rvc = [[NSClassFromString(@"DSROrderListViewController") alloc]init];
        _rvc.view.frame = CGRectMake(0, kVerticalStatusHeight+44, KScreenWidth, KScreenHeight-(kVerticalStatusHeight+44));
        [_rvc setValue:@(NO) forKey:@"isNeedNavigationBar"];
    }
    return _rvc;

}

@end