//
//  XVOrderSearchView.m
//  fastservice
//
//  Created by xuning on 4/25/25.
//  Copyright © 2025 FastService. All rights reserved.
//

#import "XVOrderSearchView.h"

@interface XVOrderSearchView()
@property (nonatomic, strong) UIButton *searchButton;

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UITextField *searchField;
@end

@implementation XVOrderSearchView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    self.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0];
    
    [self addSubview:self.searchButton];
    [self.searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self);
        make.centerY.mas_equalTo(self);
        make.size.mas_equalTo(CGSizeMake(48,48));
    }];
    kViewBorderRadius(self.searchButton, 29, 0, BB_3_COLOR);
    
    self.searchButton.layer.shadowColor = BB_3_COLOR.CGColor;
    //剪切边界 如果视图上的子视图layer超出视图layer部分就截取掉 如果添加阴影这个属性必须是NO 不然会把阴影切掉
    self.searchButton.layer.masksToBounds = NO;
    //阴影半径，默认3
    self.searchButton.layer.shadowRadius = 5;
    //shadowOffset阴影偏移，默认(0, -3),这个跟shadowRadius配合使用
    self.searchButton.layer.shadowOffset = CGSizeMake(0.0f,0.0f);
    // 阴影透明度，默认0
    self.searchButton.layer.shadowOpacity = 0.9f;
    
    
    
    [self addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.searchButton.mas_right).mas_offset(5);
        make.right.mas_equalTo(self);
        make.height.mas_equalTo(48);
        make.centerY.mas_equalTo(self);
    }];
    
    [self.bgView layoutIfNeeded];
    [self.bgView addRoundedCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft) withRadii:CGSizeMake(24, 24)];
    
    [self.bgView addSubview:self.searchField];
    
    [self.searchField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.bgView).mas_offset(5);
        make.right.mas_equalTo(self.bgView).mas_offset(-5);
        make.centerY.mas_equalTo(self);
        make.height.mas_equalTo(36);
    }];
    


}

- (void)show{
    
    self.left = SCREEN_WIDTH - self.width;
    
    [self.searchField becomeFirstResponder];
    
}
- (void)cancle {
    [UIView animateWithDuration:1 animations:^{
        self.left = SCREEN_WIDTH;
    }];
    
    [self.searchField resignFirstResponder];
    
    BlockCallWithVoidArg(self.cancelBlock);

}
- (void)searchButtonClick {
    
    [self cancle];
    
}

- (void)textFieldDidChange:(UITextField *)textField {
    
    
}

- (UIButton *)searchButton {
    if (_searchButton == nil) {
        _searchButton = [[UIButton alloc]initWithNormalImageName:@"sousuo-2" selectedImgName:@"sousuo-2"];
        [_searchButton addTarget:self action:@selector(searchButtonClick) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _searchButton;
}

- (UIView *)bgView {
    if (_bgView == nil) {
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = EF_3_COLOR;
    }
    return _bgView;
}

- (UITextField *)searchField {
    if(_searchField == nil) {
        _searchField = [[UITextField alloc]init];

        _searchField.font = [UIFont systemFontOfSize:14.0f];

        _searchField.textColor = [UIColor blackColor];
        NSAttributedString *attr = [[NSAttributedString alloc]initWithString:@"输入订单号手机号地址商户信息快速查找" attributes:@{NSForegroundColorAttributeName : BB_3_COLOR,NSFontAttributeName:[UIFont systemFontOfSize:12]}];
        _searchField.attributedPlaceholder = attr;
        _searchField.backgroundColor = [UIColor whiteColor];
        _searchField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 35)];
        _searchField.leftViewMode = UITextFieldViewModeAlways;
        _searchField.returnKeyType = UIReturnKeySearch;
        _searchField.clearButtonMode = UITextFieldViewModeAlways;
        [_searchField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        
        kViewBorderRadius(self.searchField, 36/2, 0, [UIColor clearColor]);
    }
    return _searchField;
}
@end
