import { Tag, Input, Tooltip } from 'antd';
import { PlusOutlined } from '@ant-design/icons';

import styles from './index.less';

class TagSelect extends React.Component {
  state = {
    value: this.props.value!=null?this.props.value:[],
    inputVisible: false,
    inputValue: '',
    editInputIndex: -1,
    editInputValue: '',
  };
  // componentDidUpdate(){
  //   console.log("--组件更新--")
  //   console.log(this.props.value)
  // }
  // componentDidMount(){
  //   console.log("--组件加载完成--")
  //   console.log(this.state.value)
  // }
  handleClose = removedTag => {
    const value = this.state.value.filter(tag => tag !== removedTag);
    console.log(value);
    this.setState({ value });
    this.props.onChange(value)
  };

  showInput = () => {
    this.setState({ inputVisible: true }, () => this.input.focus());
  };

  handleInputChange = e => {
  
    this.setState({ inputValue: e.target.value });
  };

  handleInputConfirm = () => {
    const { inputValue } = this.state;
    let { value } = this.state;
  
    if (inputValue && value.indexOf(inputValue) === -1) {
      this.props.onAdd(inputValue);
      value = [...value, inputValue];
    }
    console.log(value);
    this.props.onChange(value)
    this.setState({
      value,
      inputVisible: false,
      inputValue: '',
    });
  };

  handleEditInputChange = e => {
    this.setState({ editInputValue: e.target.value });
  };

  handleEditInputConfirm = () => {
    this.setState(({ value, editInputIndex, editInputValue }) => {
      const newTags = [...value];
      newTags[editInputIndex] = editInputValue;
      this.props.onChange(newTags)
      return {
        value: newTags,
        editInputIndex: -1,
        editInputValue: '',
      };
    });
  };

  saveInputRef = input => {
    this.input = input;
  };

  saveEditInputRef = input => {
    this.editInput = input;
  };

  render() {
    const { value, inputVisible, inputValue, editInputIndex, editInputValue } = this.state;
    return (
      <>
        {
          value.map((tag, index) => {
          if (editInputIndex === index) {
            return (
              <Input
                ref={this.saveEditInputRef}
                key={tag}
                size="small"
                className={styles.tagInput}
                value={editInputValue}
                onChange={this.handleEditInputChange}
                onBlur={this.handleEditInputConfirm}
                onPressEnter={this.handleEditInputConfirm}
              />
            );
          }

          const isLongTag = tag.length > 20;

          const tagElem = (
            <Tag
              className="editTag"
              key={tag}
              closable={index !== -1}
              onClose={() => this.handleClose(tag)}
            >
              <span
                onDoubleClick={e => {
               
                    this.setState({ editInputIndex: index, editInputValue: tag }, () => {
                      this.editInput.focus();
                    });
                    e.preventDefault();
                
                }}
              >
                {isLongTag ? `${tag.slice(0, 20)}...` : tag}
              </span>
            </Tag>
          );
          return isLongTag ? (
            <Tooltip title={tag} key={tag}>
              {tagElem}
            </Tooltip>
          ) : (
            tagElem
          );
          })
        }
        
        {inputVisible && (
          <Input
            ref={this.saveInputRef}
            type="text"
            size="small"
            className={styles.tagInput}
            value={inputValue}
            onChange={this.handleInputChange}
            onBlur={this.handleInputConfirm}
            onPressEnter={this.handleInputConfirm}
          />
        )}
        {!inputVisible && (
          <Tag className={styles.siteTagPlus} onClick={this.showInput}>
            <PlusOutlined /> New Services
          </Tag>
        )}
      </>
    );
  }
}

export default TagSelect;