//
//  EXFriendListCell.m
//  AIIM
//
//  Created by gan on 2025/4/6.
//
#import "EXFriendListCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "config.h"
#import "FriendNetApi.h"

@interface EXFriendListCell()

@property (strong, nonatomic) UIButton *agreetBt;
@property (strong, nonatomic) UIButton *rejectBt;

@property (strong, nonatomic) NSDictionary *dataDic;

@end

@implementation EXFriendListCell

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

- (void)setupViews {
    CGSize bsize = self.contentView.frame.size;
    _avatar =[[UIImageView alloc] init];
    _avatar.frame=CGRectMake(8, 10, 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, 10, 150, 50);
    _name.backgroundColor=[UIColor clearColor];
    _name.numberOfLines = 2;
    [_name setFont:[UIFont boldSystemFontOfSize:18]];
    [_name setTextColor:[UIColor whiteColor]];
    
    [self.contentView addSubview:_name];
    
    _agreetBt =[[UIButton alloc] init];  
    _agreetBt.layer.backgroundColor =globalColor(GCTypeGreend).CGColor;
    _agreetBt.layer.cornerRadius = 8.0f;
    _agreetBt.frame=CGRectMake(bsize.width-94, 17, 60, 36);
    [_agreetBt setTitle:@"同意" forState:UIControlStateNormal];
    [_agreetBt.titleLabel setFont:[UIFont systemFontOfSize:16.0f]];
    [_agreetBt addTarget:nil action:@selector(agreetex) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_agreetBt];
    
    
    _rejectBt =[[UIButton alloc] init];
    _rejectBt.layer.backgroundColor =[UIColor lightGrayColor].CGColor;
    _rejectBt.layer.cornerRadius = 8.0f;
    _rejectBt.frame=CGRectMake(bsize.width-28, 17, 60, 36);
    [_rejectBt setTitle:@"拒绝" forState:UIControlStateNormal];
    [_rejectBt.titleLabel setFont:[UIFont systemFontOfSize:16.0f]];
    [_rejectBt addTarget:nil action:@selector(rejectex) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_rejectBt];
}

- (void)fillWithData:( NSDictionary*)data {
    
    NSLog(@"fillWithData:%@",data);
    _dataDic = data;
   [self.avatar sd_setImageWithURL:[NSURL URLWithString:data[@"avatar"]] placeholderImage:[UIImage imageNamed:@"Avatar"]];
    self.name.text = data[@"username"];
    
    // tell constraints they need updating
    [self setNeedsUpdateConstraints];

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

    [self layoutIfNeeded];
}

-(void)agreetex{
    NSLog(@"agreetex");
    [FriendNetApi agreeADDfriend:_dataDic[@"userId"] succ:^(int code, NSDictionary * res) {
        NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:res options:0 error:&error];
        if (!jsonData) {
            NSLog(@"Got an error: %@", error);
        } else {
            NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
            NSLog(@"%@", jsonString); // 输出：{"name":"John","age":25}
        }
        if(self.delegate){
            [self.delegate actionNote:@"succ"];
        }
    } fail:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        if(self.delegate){
            [self.delegate actionNote:@"fail"];
        }
    }];
}

-(void)rejectex{
   
    [FriendNetApi deletefriend:_dataDic[@"userId"] succ:^(int code, NSDictionary * res) {
        NSLog(@"res:%@",res);
        if(self.delegate){
            [self.delegate actionNote:@"succ"];
        }
    } fail:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        if(self.delegate){
            [self.delegate actionNote:@"fail"];
        }
    }];
}

@end
