//
//  WithDrawMoreCreditCell.m
//  Open
//
//  Created by qinxuyang on 2021/11/25.
//  Copyright © 2021 QXY. All rights reserved.
//

#import "WithDrawMoreCreditCell.h"
#import "WithDrawCollectionViewCell.h"
@implementation WithDrawMoreCreditCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.collectionView];
    }
    return self;
}
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"WithDrawMoreCreditCell";
    
    WithDrawMoreCreditCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        cell = [[WithDrawMoreCreditCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.dataArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WithDrawCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        cell.hiddenImg = NO;
    
    NSString *amountString = [NSString stringWithFormat:@"立减%@元",self.dataArray[indexPath.row][@"msg"]];
    NSString *str = [NSString stringWithFormat:@"%@",self.dataArray[indexPath.row][@"msg"]];
    NSMutableAttributedString *amountFont = [[NSMutableAttributedString alloc] initWithString:amountString];
    [amountFont addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30 weight:UIFontWeightMedium] range:NSMakeRange(2, str.length)];
    cell.lb_money.attributedText = amountFont;
//        if (self.index == indexPath.row) {
//            cell.cellSelected = YES;
//        }else{
//            cell.cellSelected = NO;
//        }
    return cell;
}
/**
 cell的大小
 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = (ScreenWidth - (3 - 1) * 10 * WIDTHRADIU - 20 * WIDTHRADIU * 2)  / 3;

    CGFloat height = 60 * WIDTHRADIU;
    
    CGSize size = CGSizeMake(width, height);
    return size;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeZero;
}
/**
 每个分区的内边距（上左下右）
 */
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 20 * WIDTHRADIU,0, 20 * WIDTHRADIU);
}
/**
 分区内cell之间的最小行间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 10 * WIDTHRADIU;
}
/**
 分区内cell之间的最小列间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10 * WIDTHRADIU;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
//    if (self.firstWithDraw == NO && indexPath.row == 0) {
//        return;
//    }
//
//    if (self.index == indexPath.item) {
//        return;
//    }
//
//    self.index = indexPath.item;
//
    if (self.delegate && [self.delegate respondsToSelector:@selector(MoreCreditIndex:)]) {
        [self.delegate MoreCreditIndex:indexPath.item];
    }
    [self.collectionView reloadData];
    
}
//- (void)reloadCellCollectViewWithIndex:(NSInteger)index firstWithDraw:(BOOL)firstWithDraw{
//    self.index = index;
//    self.firstWithDraw = firstWithDraw;
//
//    [self collectionView:self.collectionView didSelectItemAtIndexPath:[NSIndexPath indexPathForRow:self.index inSection:0]];
//}
#pragma mark - getters and setters
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layOut = [[UICollectionViewFlowLayout alloc]init];
        layOut.minimumInteritemSpacing = 0;
        layOut.minimumLineSpacing = 0;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth , 80 * WIDTHRADIU) collectionViewLayout:layOut];
        
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[WithDrawCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    }
    return _collectionView;
}
- (void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    
    [self.collectionView reloadData];
}
//- (NSArray *)dataArray
//{
//    if (!_dataArray) {
//        _dataArray = @[@"2",@"5"];
//    }
//    return _dataArray;
//}
@end
