import React,{Component} from 'react';
import classNames from 'classnames';
import $ from 'jquery';
import style from "./index.less";
import {storageLocal} from "@utils/tools.js";
/**
 * author: Liucf
 * 主诉常见症状下拉--修改为横铺多选（3.13）
 * 接收参数：
 * data: json数组，数据源；
 * show: true/false,与私有属性hide共同控制组件的显示隐藏；
 * onSelect: 确定事件；
 * 
 * **/

class CommonSymptom extends Component{
  constructor(props){
    super(props);
    this.state={
      select:[],
      ids:[],
      hide:false,
      conceptId:[]
    }
    this.div = React.createRef();
    this.handleSelect = this.handleSelect.bind(this);
    this.handleClear = this.handleClear.bind(this);
    this.handleConfirm = this.handleConfirm.bind(this);
  }
  getClass(){
    let isHide = this.props.show?'':style['hide'];
    return classNames(style['list'],isHide);
  }

  handleSelect(e,item){
    e.stopPropagation();
    let {select,ids,conceptId} = this.state;
    const id = item.questionId||item.id; //缓存localStorage中的数据有id
    const copid = item.conceptId
    if(conceptId.includes(copid)){
      conceptId.splice(conceptId.indexOf(copid),1);
      if(ids.includes(id)){
        ids.splice(ids.indexOf(id),1);
      }
      let selectData = select;  
      select.forEach((it,i)=>{
        if(it.conceptId==copid){
         selectData.splice(i,1); 
        }
      })
      select = selectData; 
    }else{
      // ids.push(id); //questionId可能没有
      if(id){
        ids.push(id);
      }
      conceptId.push(copid);
      select.push(Object.assign(item,{id:item.questionId}));
    }
    this.setState({
      select,
      ids,
      conceptId
    })
  }
  componentWillReceiveProps(next){
    if(next.show!==this.props.show){
      this.handleClear();
    }
  }
  getStyle(id){
    const {conceptId} = this.state;
    if(conceptId.includes(id)){
      return style['selected'];
    }
    return '';
  }
  handleClear(e){
    // e.stopPropagation();
    this.setState({
      select:[],
      ids:[],
      conceptId:[]
    })
  }
  handleConfirm(e){
    // e.stopPropagation();
    const {onSelect} = this.props;
    const {select,ids,conceptId} = this.state;
    onSelect&&onSelect({select,ids,conceptId});
  }

  render(){ 
    const {data,isCurrent} = this.props;
    const mainSymp = storageLocal.get('mainSymp');
    const mainHis = mainSymp?JSON.parse(mainSymp).reverse():[];
    return <div className={this.getClass()} contenteditable="false" style={{left:isCurrent?"auto":undefined}} ref={this.div}>
      <ul className={style["listBox"]}>
        {mainHis.length>0?<ul className={style["his"]}>
          <div className='clearfix'>
          <p>最近输入症状：</p>
          </div>
          {mainHis.map((it)=>{
            return <li onClick={(e)=>this.handleSelect(e,it)} className={this.getStyle(it.conceptId)} title={it.name.length>5?it.name:''}>{it.name.length>5?it.name.slice(0,4)+'...':it.name}</li>
          })}
        </ul>:''}
        <p className={style['c-title']}>常见症状：</p>
        <div className={style['oper']}>
          <span className={style['clear']} onClick={this.handleClear}>清空选项</span>
          <span className={style['confirm']} onClick={this.handleConfirm}>确定</span>
        </div>
        {data&&data.map((it)=>{
          return <li onClick={(e)=>this.handleSelect(e,it)} className={this.getStyle(it.conceptId)} title={it.name.length>5?it.name:''}>{it.name.length>5?it.name.slice(0,4)+'...':it.name}</li>
        })}
      </ul>
    </div>
  }
}

export default CommonSymptom;