import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { SelectField, Button, MenuItem, Input } from 'patd';
import tool from './tool';
import InputComposition from '../Common/InputComposition';

class NewSelectSearch extends Component {
  static propTypes = {
    types: PropTypes.array.isRequired,
    hintText: PropTypes.string,
    defaultValue: PropTypes.string,
    style: PropTypes.object,
    onChange: PropTypes.func,
    onSubmit: PropTypes.func,
    onChangeType: PropTypes.func,
    small: PropTypes.bool,
  }

  static defaultProps = {
    hintText: '开始搜索',
    style: {},
    small: false,
    onChange: () => { },
    onSubmit: () => { },
    onChangeType: () => { },
  }

  constructor(props) {
    super(props);
    this.searchText = ''
    this.state = {
      type: '',
      searchText: ''
    }
    this.debounce = tool.debounce(this.changeValue, 600);
  }

  componentDidMount() {

    if (this.state.type === '' && this.props.types && this.props.types.length) {
      this.setState({ type: this.props.types[0].key })
    }
  }

  componentWillReceiveProps(nextProps) {
    if (this.props.type !== nextProps.type) {
      this.setState({ type: nextProps.type })
    }

    if ((this.props.types !== nextProps.types) && nextProps.types.length) {
      this.setState({ type: nextProps.types[0].key })
    }
    if (this.state.type === '' && nextProps.types && nextProps.types.length) {
      this.setState({ type: nextProps.types[0].key })
    }


    if (this.props.value !== nextProps.value && nextProps.value) {
      this.setState({ searchText: nextProps.value })
    }

  }
  onKeyUp = (e) => {
    const key = e.nativeEvent.keyCode
    if (key === 13) {
      const type = this.props.type || this.state.type;
      const text = this.props.value || this.state.searchText;
      this.debounce({ type, text }, 'enter');
    }
  }
  onSubmit = () => {
    const type = this.props.type || this.state.type;
    const text = this.props.value || this.state.searchText;
    this.props.onSubmit({
      type,
      text
    });
  }

  changeValue = (data, type) => {
    if (!data) {
      return;
    }
    if (type === 'enter') {
      this.props.onSubmit(data);
    } else {
      this.props.onChange(data, data.target.value);
    }
  }
  // checkTypesEquality = (nextTypes, currentTypes) => {
  //   if (nextTypes.length !== currentTypes.length) {
  //     return false;
  //   }
  //   for (let i = 0; i < nextTypes.length; i += 1) {
  //     if (nextTypes[i].key !== currentTypes[i].key) {
  //       return false;
  //     }
  //   }
  //   return true;
  // }



  // reset = () => {
  //   this.setState({ searchType: this.props.type });
  //   this.textfield.value = '';
  //   this.props.onChange('');
  //   this.props.onChangeType(this.props.type);
  // }

  render() {
    const { useChild } = this.props;
    return (
      <div
        style={Object.assign({}, { display: 'flex', marginBottom: 24 }, this.props.style)}
      >
        <SelectField
          style={{ border: 0, minWidth: 80 }}
          width={this.props.typeWidth || '121px'}
          value={this.props.type || this.state.type}
          inputStyle={{
            border: '1px solid #d7dbe0',
            borderRight: 0,
            borderRadius: '2px 0px 0px 2px',
          }}
          onChange={(e, i, v) => {
            this.setState({ type: v })
            this.props.onChangeType(e, i, v)
          }}
        >
          {
            this.props.types.map(item => (
              <MenuItem key={item.key} value={item.key} >
                {item.text}
              </MenuItem>
            ))
          }
        </SelectField>
        {!useChild ? <Input
          type="text"
          value={(this.props.value === '' || this.props.value) ? this.props.value : this.state.searchText}
          placeholder={this.props.hintText}
          // onChange={(e)=>{
          //   this.setState({searchText:e.target.value})
          //   this.props.onChange(e,e.target.value)}
          // }
          {...InputComposition({
            onChange: (e) => {
              e.persist();
              const { value } = e.target;
              if (!e.reactComposition.composition) {
                this.debounce(e);
              }
              if (this.props.value === '' || this.props.value) {
                if (this.props.onChange) {
                  this.props.onChange(e, value);
                }
              } else {
                this.setState({ searchText: value })
              }
            }
          })}
          style={this.props.small ? { width: 150 } : { width: 240, margin: 0 }}
          onKeyUp={this.onKeyUp}
        /> : this.props.children}


        {/* <input
          type="text" className="select-search-input"
          ref={(node) => { this.textfield = node; }}
          placeholder={this.state.hintText}
          style={this.props.small ? { width: 150 } : null}
          // onChange={this.props.onChange}
          // onKeyUp={(e) => {
          //   this.searchText = e.target.value;
          //   if (e.keyCode === 13) {
          //     this.props.onSubmit(this.searchText);
          //   }
          // }}
        /> */}
        <Button
          label={<i className="fa fa-search" />}
          primary
          style={{ borderTopLeftRadius: 0, borderBottomLeftRadius: 0, width: 60, fontSize: 16 }}
          onClick={this.onSubmit}
        />
      </div >
    );
  }
}

export default NewSelectSearch;
