//
//  addFriendController.m
//  AIIM
//
//  Created by gan on 2025/6/25.
//

#import <Foundation/Foundation.h>
#import "addFriendController.h"
#import "FriendNetApi.h"

@interface addFriendController()<UISearchBarDelegate>

@property (weak, nonatomic) IBOutlet UIView *cntentView;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UIImageView *avarvImg;
@property (weak, nonatomic) IBOutlet UILabel *nameLb;
@property (weak, nonatomic) IBOutlet UITextView *beizhuT;
@property (weak, nonatomic) IBOutlet UIButton *quxiaoBt;
@property (weak, nonatomic) IBOutlet UIButton *tianjiaBt;

@property (weak, nonatomic) IBOutlet UILabel *beizhuNoteLb;

@property (nonatomic,strong) NSString *keyword;
@property (nonatomic,strong) NSDictionary *userDic;

@end

@implementation addFriendController

-(void)viewDidLoad{
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    // 创建一个点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    // 将手势添加到主视图上
    [self.view addGestureRecognizer:tap];
    
    _searchBar.delegate = self;
    [_searchBar becomeFirstResponder];
    _searchBar.searchTextField.backgroundColor =[UIColor whiteColor];
    
//    [[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage new] forState:UIControlStateNormal]; // 移除背景图片
//    [[UISearchBar appearanceWhenContainedInInstancesOfClasses:@[[UINavigationBar class]]] setSearchFieldBackgroundImage:[UIImage new] forState:UIControlStateNormal]; // 仅在 UINavigationBar 中移除背景图片
     
    // 设置文本颜色
    [[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];
    
    
    _avarvImg.layer.cornerRadius = 35;
    
    _cntentView.layer.cornerRadius = 8.0f;
    _cntentView.layer.borderWidth = 0.5f;
    _cntentView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _cntentView.layer.backgroundColor = [UIColor lightGrayColor].CGColor;
    
    _beizhuT.layer.cornerRadius = 8.0f;
    _beizhuT.layer.borderColor = [UIColor whiteColor].CGColor;
    _beizhuT.layer.backgroundColor = [UIColor whiteColor].CGColor;
    _beizhuT.textColor = UIColor.blackColor;
    
    _nameLb.text = @"没有数据";
    [_tianjiaBt setBackgroundColor:globalColor(GCTypeGreen)];
    _tianjiaBt.layer.cornerRadius = 8;
    _tianjiaBt.layer.masksToBounds = YES; // 防止子视图超出圆角边界
    _tianjiaBt.tintColor = UIColor.blackColor;
    self.userDic=nil;
}

-(void)dismissKeyboard{
    NSLog(@"dismissKeyboard");
    [self.view endEditing:YES];
}


- (IBAction)gotoBack:(id)sender {
    NSLog(@"gotoBackgotoBackgotoBack");
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)addFriendBt:(id)sender {
    
    if(self.userDic==nil){
        return;
    }
    NSDictionary *dis = @{
        @"friendId":self.userDic[@"id"],
        @"message":_beizhuT.text
    };
    [FriendNetApi shenqingADDfriend:dis succ:^(int code, NSDictionary * res) {
        NSLog(@"res:%@",res);
        self.userDic = nil;
        [self dismissViewControllerAnimated:YES completion:nil];
    } fail:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
    }];
}
- (IBAction)cancelBt:(id)sender {
    [self searchFriend];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    // 当搜索文本改变时调用
    NSLog(@"Search text changed to: %@", searchText);
    self.keyword = searchText;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    // 当用户点击搜索按钮时调用
    NSLog(@"Search button clicked with text: %@", searchBar.text);
    [searchBar resignFirstResponder]; // 隐藏键盘
    self.keyword = searchBar.text;
    [self searchFriend];
}

-(void)searchFriend{
    if(self.keyword.length>0){
        [FriendNetApi searchfriend:self.keyword succ:^(int code, NSDictionary * res) {
            NSLog(@"res:%@",res);
            NSArray *data= res[@"data"];
            if(data.count>0){
                self.userDic =data[0];
                NSLog(@"1:%@",self.userDic);
                [self getData];
            }
            else{
            }
            
        } fail:^(NSError * _Nonnull error) {
            
        }];
    }
}

-(void)getData{
    if(self.userDic==nil){
        self.nameLb.text = @"没有数据";
        _avarvImg.alpha = 0;
        return;
    }
    self.nameLb.text =self.userDic[@"name"];
    _avarvImg.alpha=1;
    NSString *url = self.userDic[@"avatar"];
    if([url isKindOfClass:[NSNull class]]){
        url=@"";
    }
    [_avarvImg sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"Avatar"]];
}

@end
