//
//  GovBtnTableViewCell.m
//  Open
//
//  Created by 雷俊博 on 2021/7/20.
//  Copyright © 2021 黄江涛 . All rights reserved.
//

#import "GovBtnTableViewCell.h"
#import "ZWCollectionViewCell.h"



@interface GovBtnTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong)UICollectionView*collectionView;

@property (nonatomic, strong)NSArray* bgArray;


@property (nonatomic, strong)NSArray* titleArray;

@property (nonatomic, strong)NSArray* redPackageClickEventsArray;

@end
@implementation GovBtnTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    
    //UICollectionView
    UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc]init];
    //布局
    fl.minimumLineSpacing = 1.0;
    fl.minimumInteritemSpacing = 1.0;
           
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 210 * WIDTHRADIU) collectionViewLayout:fl];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.scrollEnabled = NO;
   
    // _collectionView.backgroundColor = K_GrayColor;
   
    _collectionView.backgroundColor = [UIColor whiteColor];
    
   
    //cell注册
    [_collectionView registerNib:[UINib nibWithNibName:@"ZWCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    
    [self.contentView addSubview:_collectionView];
    
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
//    if (section == 0) {
//        return 1;
//    }
    
    if (self.redPackageClickEventsArray.count > 0) {
        return self.redPackageClickEventsArray.count;
    }
    
    return self.titleArray.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.section == 0) {
//        ZWHeaderViewCell*cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];
//
//
//
//        return cell;
//    }
    
    ZWCollectionViewCell*cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    if (self.redPackageClickEventsArray.count > 0) {
        KKRedPackageClickEventModel *model = self.redPackageClickEventsArray[indexPath.item];
        cell.lb_title.text = model.menuName;
        [cell.icon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KKFindPackageIP,model.menuIcon]]];
        
    }else{
        
        cell.lb_title.text = self.titleArray[indexPath.item];
        cell.icon.image = [UIImage imageNamed:self.bgArray[indexPath.item]];
       
    }
    
    cell.lb_title.textColor = [UIColor colorWithHex:0x404040];
    
    cell.backgroundColor = [UIColor whiteColor];
    
//     [cell.btn layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleTop imageTitleSpace:13 * WIDTHRADIU];
    
    return cell;
}

/**
 cell的大小
 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
//    if (indexPath.section == 0) {
//        return CGSizeMake(ScreenWidth, 150 * WIDTHRADIU);
//    }
    
    CGFloat width = (ScreenWidth - 20 * WIDTHRADIU - 3.0)/4;
    CGFloat realWidth = (ScreenWidth - 20 * WIDTHRADIU - 3.0)  - 3 * width - 1.0;
    
    if (indexPath.row %4 != 3) {
        return CGSizeMake(width, 99 * WIDTHRADIU  - 0.5);
    }
    
    return CGSizeMake(realWidth, 99 * WIDTHRADIU - 0.5);
    
}

/**
 每个分区的内边距（上左下右）
 */
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
//    if (section == 0) {
//       return  UIEdgeInsetsZero;
//    }
    
    return UIEdgeInsetsMake(6 * WIDTHRADIU, 10 * WIDTHRADIU, 6 * WIDTHRADIU, 10 * WIDTHRADIU);
}


/**
 分区内cell之间的最小行间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    
    return 1.0;
}
/**
 分区内cell之间的最小列间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 1.0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.redPackageClickEventsArray.count > 0) {
        KKRedPackageClickEventModel *model = self.redPackageClickEventsArray[indexPath.row];
        if (self.delegate && [self.delegate respondsToSelector:@selector(GovBtnClickWithServerModel:)]) {
            [self.delegate GovBtnClickWithServerModel:model];
        }
    }else{
        if (self.delegate && [self.delegate respondsToSelector:@selector(GovBtnClickWithIndex:)]) {
            [self.delegate GovBtnClickWithIndex:indexPath.row];
        }
    }
}


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

- (void)reloadDataWithArray:(NSArray *)array
{
    self.redPackageClickEventsArray = array;
    [self.collectionView reloadData];
}


- (NSArray *)bgArray
{
    if (!_bgArray) {
        _bgArray = @[@"zw_renzhengchaxun",@"zw_jindu",@"zw_butie",@"zw_shebaojigou",@"zw_fp",@"zw_helptel",@"zw_hosptial",@"zw_yanglaoyuan"];
        
    }
    return _bgArray;
}


- (NSArray *)titleArray
{
    if (!_titleArray) {
        
        _titleArray = @[@"认证查询",@"申报查询",@"补贴登记",@"社保机构",@"缴社保",@"救助热线",@"医院",@"养老院"];
        
    }
    return _titleArray;
}


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

    // Configure the view for the selected state
}

@end
