//
//  GroupListCell.m
//  AIIM
//
//  Created by gan on 2025/4/6.
//

#import <Foundation/Foundation.h>
#import "GroupListCell.h"
#import <SDWebImage/UIImageView+WebCache.h>


@implementation GroupListCell

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

- (void)setupViews {
    CGSize size = self.contentView.frame.size;
    _avatar =[[UIImageView alloc] init];
    _avatar.frame=CGRectMake(8, 8, 50, 50);
    _avatar.contentMode = UIViewContentModeScaleAspectFill;
    _avatar.layer.cornerRadius = 25;
    _avatar.layer.masksToBounds = YES;
    _avatar.backgroundColor=[UIColor lightGrayColor];
    [self.contentView addSubview:_avatar];
    
    _name = [[UILabel alloc] init];
    _name.frame=CGRectMake(68, 8,190, 50);
    _name.backgroundColor=[UIColor clearColor];
    [_name setFont:[UIFont boldSystemFontOfSize:15]];
    _name.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
    _name.adjustsFontForContentSizeCategory = YES;
    [_name setTextColor:[UIColor whiteColor]];
    
    [self.contentView addSubview:_name];
    
    
    _tongyi = [[UIButton alloc] init];
    _tongyi.frame=CGRectMake(size.width-70, 16, 50, 30);
    _tongyi.backgroundColor =globalColor(GCTypeGreen);
    _tongyi.layer.cornerRadius = 8;
    _tongyi.layer.masksToBounds = YES; // 防止子视图超出圆角边界
    _tongyi.titleLabel.font = [UIFont systemFontOfSize: 14.0];
    [_tongyi setTitle:@"同意" forState:UIControlStateNormal];
    _tongyi.titleLabel.textColor = [UIColor whiteColor];
    [self.contentView addSubview:_tongyi];
    _jujue = [[UIButton alloc] init];
//    _jujue.frame=CGRectMake(size.width-10, 16, 50, 30);
    _jujue.titleLabel.font = [UIFont systemFontOfSize: 14.0];
    [_jujue setTitle:@"拒绝" forState:UIControlStateNormal];
    _jujue.backgroundColor =globalColor(GCTypeOrangeR);
    _jujue.layer.cornerRadius = 8;
    _jujue.layer.masksToBounds = YES; // 防止子视图超出圆角边界
    _jujue.titleLabel.textColor = [UIColor whiteColor];
    [self.contentView addSubview:_jujue];
    [_tongyi addTarget:self action:@selector(tongyiAct) forControlEvents:UIControlEventTouchUpInside];
    [_jujue addTarget:self action:@selector(jujueAct) forControlEvents:UIControlEventTouchUpInside];
    
    
    
}

- (void)fillWithData:( NSDictionary*)data cellType:(NSInteger)type{
    [_name setTextColor:[UIColor whiteColor]];
    [_jujue mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-0);
        make.top.equalTo(self.contentView).offset(16);
        make.width.height.mas_equalTo(CGSizeMake(50, 30));
    }];
    
    [_tongyi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_jujue.mas_left).offset(-6);
        make.top.equalTo(self.contentView).offset(16);
        make.width.height.mas_equalTo(CGSizeMake(50, 30));
    }];
    if(type==0){
        _tongyi.alpha = 0;
        _jujue.alpha = 0;
    }
    else if(type==1){
        _tongyi.alpha = 1;
        _jujue.alpha = 1;
        _actId =data[@"id"];
    }
    else if(type==2){
        if([self ismaster:data[@"id"]]){
            _tongyi.alpha = 0;
            _jujue.alpha = 0;
            [_name setTextColor:globalColor(GCTypeOrangeR)];
        }
        else{
            _tongyi.alpha = 0;
            _jujue.alpha = 1;
            _actId =data[@"id"];
            [_jujue setTitle:@"转让" forState:UIControlStateNormal];
        }
    }
    else if(type==3){
        _tongyi.alpha = 0;
        _jujue.alpha = 1;
        _actId =data[@"id"];
        [_jujue setTitle:@"邀请" forState:UIControlStateNormal];
    }
    else if(type==4){
        if([self ismaster:data[@"id"]]){
            _tongyi.alpha = 0;
            _jujue.alpha = 0;
            [_name setTextColor:globalColor(GCTypeOrangeR)];
        }
        else{
            _tongyi.alpha = 0;
            _jujue.alpha = 1;
            _actId =data[@"id"];
            [_jujue setTitle:@"踢出" forState:UIControlStateNormal];
        }
        
    }
    
    //NSLog(@"fillWithData:%@",data);
    NSString *avatar =data[@"avatar"];
    if([avatar isKindOfClass:[NSString class]]){
        
    }
    else{
        avatar = @"";
    }
    
   [self.avatar sd_setImageWithURL:[NSURL URLWithString:avatar] placeholderImage:[UIImage imageNamed:@"Avatar"]];
    if(type==1){
        self.name.text = data[@"userName"];
    }
    else{
        self.name.text = data[@"name"];
    }
    
    // tell constraints they need updating
    [self setNeedsUpdateConstraints];

    // update constraints now so we can animate the change
    [self updateConstraintsIfNeeded];

    [self layoutIfNeeded];
}

-(void)tongyiAct{
    if(self.delegate){
        [self.delegate GroupListCellAct:_actId act:1];
    }
}

-(void)jujueAct{
    if(self.delegate){
        [self.delegate GroupListCellAct:_actId act:0];
    }
}

-(BOOL)ismaster:(NSString *)userId{
    NSDictionary * userinfo= [UDManager.shareInstance getDDManager:dkuserinfo];
    NSString *usid = userinfo[@"id"];
    if([userId isEqualToString:usid]){
        return YES;
    }
    return NO;
}


@end
