CNLiveMonthBillView.m 8.54 KB
//
//  CNLiveMonthBillView.m
//  CNLiveBalanceModule
//
//  Created by 153993236@qq.com on 11/12/2019.
//  Copyright © 2019 153993236@qq.com. All rights reserved.
//

#import "CNLiveMonthBillView.h"
#import "QMUIKit.h"
#import <Masonry/Masonry.h>
#import "CNLiveCategory.h"
#import "CNLiveBaseKit.h"
#import "PGDatePickManager.h"
#import "CNLiveBalanceRequest.h"

@interface CNLiveMonthBillView ()<PGDatePickerDelegate>
@property (nonatomic, strong) QMUIButton *switchTime;

@property (nonatomic, strong) UIButton *defray;//收入
@property (nonatomic, strong) UIButton *income;//支出
@property (nonatomic, strong) UIView *defrayLine;
@property (nonatomic, strong) UIView *incomeLine;

@end

@implementation CNLiveMonthBillView
static const NSInteger margin = 20;

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.userInteractionEnabled = YES;
        self.backgroundColor = [UIColor clearColor];
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews {
    [self addSubview:self.switchTime];
    [self addSubview:self.defray];
    [self addSubview:self.income];
    
    [self addSubview:self.defrayLine];
    [self addSubview:self.incomeLine];
    
    [_defray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).with.offset(-margin);
        make.centerY.equalTo(self.mas_centerY);
        make.width.height.offset(40);
    }];
    
    [_defrayLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.centerX.equalTo(_defray);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(3);
    }];
    
    [_income mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.defray.mas_left).with.offset(-margin);
        make.centerY.equalTo(self.mas_centerY);
        make.width.height.offset(40);
    }];
    
    [_incomeLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.centerX.equalTo(_income);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(3);
    }];
  
    [_switchTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).with.offset(0);
        make.left.equalTo(self.mas_left).with.offset(margin);
        make.width.offset(120);
        make.height.offset(self.height);
        
    }];
    _incomeLine.hidden = NO;
    _defrayLine.hidden = YES;
}

- (void)setDate:(NSString *)date{
    _date = date;
    [_switchTime setTitle:date forState:UIControlStateNormal];
}

#pragma mark - PGDatePickerDelegate
- (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents{
    dateComponents.day = 1;
    NSCalendar * calendar = [NSCalendar currentCalendar];
    NSDate *date = [calendar dateFromComponents:dateComponents];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[NSLocale currentLocale]];
    [dateFormatter setDateFormat:@"yyyy年M月"];
    NSString *value = [dateFormatter stringFromDate:date];
    [_switchTime setTitle:value forState:UIControlStateNormal];

    NSArray *times = [CNLiveBalanceRequest getMonthFirstAndLastDayWithDate:date];
    NSLog(@"%@",times);
    if (self.delegate && [self.delegate respondsToSelector:@selector(monthBillView:startTime:endTime:)]) {
        [self.delegate monthBillView:self startTime:times.firstObject endTime:times.lastObject];
    }
    
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

#pragma mark - 响应方法
- (void)switchTimeDidClicked:(UIButton *)btn{
    PGDatePickManager *datePickManager = [[PGDatePickManager alloc] init];
    datePickManager.style = PGDatePickManagerStyleSheet;
    datePickManager.isShadeBackground = true;
    datePickManager.cancelButtonTextColor = RGB(136, 136, 136);
    datePickManager.confirmButtonTextColor = RGB(33, 188, 35);
    PGDatePicker *datePicker = datePickManager.datePicker;
    datePicker.isHiddenMiddleText = false;
    datePicker.delegate = self;
    datePicker.datePickerType = PGDatePickerTypeLine;
    datePicker.datePickerMode = PGDatePickerModeYearAndMonth;
    datePicker.lineBackgroundColor = RGB(232, 232, 232);
    datePicker.textColorOfSelectedRow = KBlack51Color;
    datePicker.textColorOfOtherRow = RGB(169, 169, 169);
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateFormat = @"yyyy年MM月";
    //获取当前时间日期
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    datePicker.minimumDate = [dateFormatter dateFromString:@"2017年1月"];
    datePicker.maximumDate = [dateFormatter dateFromString:dateString];
    [(UIViewController *)self.delegate presentViewController:datePickManager animated:NO completion:nil];

}
- (void)setInteractionEnabled:(BOOL)interactionEnabled{
    _interactionEnabled = interactionEnabled;
    _defray.userInteractionEnabled = interactionEnabled;
    _income.userInteractionEnabled = interactionEnabled;

}
- (void)btnDidClicked:(UIButton *)btn{
    if (self.delegate && [self.delegate respondsToSelector:@selector(switchType:)]) {
        btn.userInteractionEnabled = NO;
        if(btn.tag == 10000){//收入
            [self.delegate switchType:@"1"];
            _defray.selected = YES;
            _defrayLine.hidden = NO;
            _income.selected = NO;
            _incomeLine.hidden = YES;

        }else if (btn.tag == 10001){//支出
            [self.delegate switchType:@"2"];
            _income.selected = YES;
            _incomeLine.hidden = NO;
            _defray.selected = NO;
            _defrayLine.hidden = YES;
        }

         
    }
}

#pragma mark - 懒加载
- (QMUIButton *)switchTime {
    if (!_switchTime) {
        _switchTime = [QMUIButton buttonWithType:UIButtonTypeCustom];
        _switchTime.adjustsImageWhenHighlighted = NO;
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy年M月"];
        NSString *dateTime = [formatter stringFromDate:[NSDate date]];
        [_switchTime setTitle:dateTime forState:UIControlStateNormal];
        [_switchTime setTitleColor:[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0] forState:UIControlStateNormal];
        _switchTime.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        UIImage *xw_image = [self getImageWithImageName:@"record_switch_time" bundleName:@"CNLiveBalanceModule" targetClass:[self class]];
        [_switchTime setImage:[xw_image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        _switchTime.titleLabel.font = UIFontCNMake(17);
        [_switchTime addTarget:self action:@selector(switchTimeDidClicked:) forControlEvents:UIControlEventTouchUpInside];
        _switchTime.imagePosition = QMUIButtonImagePositionRight;
        _switchTime.spacingBetweenImageAndTitle = 8;
    }
    return _switchTime;
}

- (UIButton *)defray {
    if (!_defray) {
        _defray = [UIButton buttonWithType:UIButtonTypeCustom];
        _defray.adjustsImageWhenHighlighted = NO;
        _defray.tag = 10000;
        _defray.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [_defray setTitle:@"收入" forState:UIControlStateNormal];
        [_defray setTitleColor:RGB(58, 58, 58) forState:UIControlStateNormal];
        [_defray setTitleColor:RGB(11, 190, 6) forState:UIControlStateSelected];
        [_defray addTarget:self action:@selector(btnDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _defray;
}

- (UIButton *)income {
    if (!_income) {
        _income = [UIButton buttonWithType:UIButtonTypeCustom];
        _income.adjustsImageWhenHighlighted = NO;
        _income.tag = 10001;
        _income.titleLabel.font = [UIFont systemFontOfSize:14.0];
        [_income setTitle:@"支出" forState:UIControlStateNormal];
        [_income setTitleColor:RGB(58, 58, 58) forState:UIControlStateNormal];
        [_income setTitleColor:RGB(11, 190, 6) forState:UIControlStateSelected];
        [_income addTarget:self action:@selector(btnDidClicked:) forControlEvents:UIControlEventTouchUpInside];
        _income.selected = YES;
    }
    return _income;
}

- (UIView *)defrayLine{
    if (!_defrayLine) {
        _defrayLine = [[UIView alloc] init];
        _defrayLine.backgroundColor = RGB(11, 190, 6);
    }
    return _defrayLine;
}

- (UIView *)incomeLine{
    if (!_incomeLine) {
        _incomeLine = [[UIView alloc] init];
        _incomeLine.backgroundColor = RGB(11, 190, 6);
    }
    return _incomeLine;
}

@end