import React, { Component } from 'react';
import style from './index.less';
import delIcon from '@common/images/del_nor.png';
import {windowEventHandler,getCurrentDate,getWindowInnerHeight} from '@utils/tools'

class ScaleSearch extends Component {
  constructor(props) {
    super(props);
    this.$inp = React.createRef();
    this.$cont = React.createRef();
    this.state={
      val:''
    };
    this.search = this.search.bind(this);
    this.handleChange = this.handleChange.bind(this);
    this.clear = this.clear.bind(this);
    this.handleEnter = this.handleEnter.bind(this);
  }
  getResult(id){
    const {formulaResult} = this.props;
    const scale=formulaResult&&formulaResult[id];
    let result = scale&&scale.calcalculate?scale.calcalculate.result:null;
    if(result){
      return <i>( 结果：{result.value} {result.text} )</i>;
    }
    return '';
  }
  showScale(item){
    const {scaleInfo,getScale,showScaleFn} = this.props;
    if(scaleInfo&&scaleInfo[item.id]){
      showScaleFn&&showScaleFn(item);
    }else{
      getScale(item);
    }
  }
  getSearchList() {
    const { searchResult } = this.props;
    return searchResult && searchResult.map((item) => {
      return <li key={item.conceptId} onClick={this.showScale.bind(this,{id:item.conceptId,name:item.name})}>
        <span className={style['scale-name']}>{item.name}{this.getResult(item.conceptId)}</span>
        <button>查看</button>
      </li>;
    });
  }
  search(){
    const {handleChangeValue} = this.props;
    const val = this.$inp.current.value;
    handleChangeValue&&handleChangeValue(val);
  }
  handleChange(){
    this.setState({
      val:this.$inp.current.value
    });
  }
  handleEnter(e){
    if(e.keyCode==13){
      this.search();
    }
  }
  clear(){
    this.$inp.current.value = '';
    this.setState({
      val:''
    });
  }
  componentDidMount(){
    const height = getWindowInnerHeight()-170;
    this.$cont.current.style.height = height+"px";
    if(this.$cont.current){
      windowEventHandler('resize', ()=>{
        const height = getWindowInnerHeight()-170;
        this.$cont.current.style.height = height+"px";
      });
    }
  }
  render() {
    const {searchResult} = this.props;
    const {val} = this.state;
    return (
        <div className={style['search-cont']} ref={this.$cont}>
          <div className={style['search-box']}>
            <p className={style['title']}>量表搜索</p>
            <p className={style['cont']}>
              <input type="text" className={style['input']} ref={this.$inp} onInput={this.handleChange} onKeyUp={this.handleEnter}/>
              {val?<img src={delIcon} alt="清空" onClick={this.clear}/>:''}
              <button onClick={this.search}>搜索</button>
            </p>
          </div>
          {searchResult&&searchResult.length>0?<div className={style['result']}>
            <p className={style['title']}>查询内容</p>
            <ul>
              {this.getSearchList()}
            </ul>
          </div>:<p className={style['no-data']}>暂无搜索结果！</p>}
        </div>
    )



  }
}

export default ScaleSearch;
