//
//  JCTextFieldFormCell.m
//  EZOpenSDKDemo
//
//  Created by JuneCheng on 2026/3/12.
//  Copyright © 2026 hikvision. All rights reserved.
//

#import "JCTextFieldFormCell.h"

#import "JCFormItem.h"

static NSString * const ID = @"JCTextFieldFormCell";

@interface JCTextFieldFormCell()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *lab_title;
@property (nonatomic, strong) UITextField *tf_value;

@end

@implementation JCTextFieldFormCell

+ (instancetype)cellWithTableView:(UITableView *)tableView cellStyle:(UITableViewCellStyle)style {
    JCTextFieldFormCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[self alloc] initWithStyle:style reuseIdentifier:ID];
    }
    return cell;
}

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

/** 创建UI */
- (void)setUI {
    _lab_title = [[UILabel alloc] init];
    [self.contentView addSubview:_lab_title];
    
    _tf_value = [[UITextField alloc] init];
    _tf_value.textAlignment = NSTextAlignmentRight;
    _tf_value.backgroundColor = [UIColor clearColor];
    _tf_value.clearButtonMode = UITextFieldViewModeWhileEditing;
    _tf_value.returnKeyType = UIReturnKeyDone;
    _tf_value.delegate = self;
    [self.contentView addSubview:_tf_value];
}

/** 设置约束 */
- (void)setLayout {
    [_lab_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.centerY.equalTo(self);
        make.width.mas_equalTo(85);
    }];
    [_tf_value mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.bottom.equalTo(self);
        make.left.equalTo(_lab_title.mas_right).offset(5);
        make.right.equalTo(self).offset(-15);
    }];
}

/** 输入框监听 */
- (void)textFieldDidChange {
    self.item.subTitle = _tf_value.text;
}

/** UI赋值 */
- (void)setItem:(JCFormItem *)item {
    if (!_item) {
        _item = item;
        [kNotificationCenter addObserver:self selector:@selector(textFieldDidChange) name:UITextFieldTextDidChangeNotification object:nil];
    } else {
        _item = item;
    }
    [self fillData];
    [self changeUI];
}

/** 数据填充 */
- (void)fillData {
    self.lab_title.textColor = self.item.titleColor;
    self.tf_value.text = self.item.subTitle;
    // 是否必填
    if (self.item.isRequired) {
        NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"* %@", self.item.title]];
        [attrString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,2)];
        self.lab_title.attributedText = attrString;
    } else {
        self.lab_title.text = self.item.title;
    }
}

/** UI调整 */
- (void)changeUI {
    self.lab_title.font = self.item.titleFont;
    
    self.tf_value.placeholder = [NSString stringWithFormat:@"请输入%@", self.item.title];
    self.tf_value.font = self.item.subTitleFont;
    self.tf_value.textColor = self.item.subTitleColor;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

@end
