import React,{Component} from 'react';
import style from './index.less';
import {Button,InlineTag,ItemBox,Notify,Textarea} from '@commonComp';
import TailInlineTag from '@commonComp/TailInlineTag';
import chooseType from '@containers/eleType.js';
import SearchDrop from '@components/SearchDrop';

class CurrentIll extends Component{
  constructor(props){
    super(props);
    this.state = {
      boxEditable:true,
      showMoudle:false,
      forbidInput:false,
      mainFlag:true,
      mainData:[],
      boxMark:"2",
      boxLeft:null,
      boxTop:null,
      show:true
    }
    this.toggleEditable = this.toggleEditable.bind(this);
    this.handleFocus = this.handleFocus.bind(this);
    this.onchange = this.onchange.bind(this);
    this.handleSearchSelect = this.handleSearchSelect.bind(this);
    this.handleClick = this.handleClick.bind(this);
  }
  toggleEditable(){
    this.setState({
      boxEditable:!this.state.boxEditable
    })
  }

  handleFocus(e){
    // 判断主诉是否为空
    const {mainData,mainText,setData,moduleNum,mainIds,data} = this.props;
    const that = this;
    if(mainData.length == 0 && !mainText[0]){
      //弹窗提醒
      Notify.error("无法操作，请先输入主诉");
      this.setState({
        forbidInput:true
      })
    }else {
      const {changeEditIll,editClear} = this.props;
      let useEmpty = mainData.length>0?false:true;
      let num = moduleNum.num;//主诉使用了几个模板
      // if(editClear && data.length==0){//第一次聚焦去设置现病史的data
      if(data.length==0){//第一次聚焦去设置现病史的data
        setData && setData({useEmpty,num,mainData,mainIds});//是否使用空模板 及数据处理
      }
      const showTimer = setTimeout(function(){
        that.setState({
          showMoudle:true,
          forbidInput:false,
          boxEditable:false,
        })
        changeEditIll(false)
      },150)
      clearTimeout(showTimer);
    }

  }

  onchange(e){//监听输入事件,主诉无数据不能输入
    e.target.innerText = "";
  }

  handleClick(e){//让搜索框跟随鼠标点击移动
    // e.stopPropagation(); //冒泡到最顶层关闭其他下拉
    let boxLeft = e.pageX -62 + 'px';
    // let boxTop = e.pageY - 164 + 'px';console.log(124,e.pageY,boxTop);
    let offsetTop = e.target.offsetTop;
    let boxTop = offsetTop + 28 + 'px';
    this.setState({
      boxLeft:boxLeft,
      boxTop:boxTop
    })
  }

  handleSearchSelect(obj){
    const {questionId,name} = obj;
    const {fetchModules,focusIndex,span} = this.props;
    fetchModules&&fetchModules({id:questionId,index:focusIndex,name,span});
  }

  getInlineTag(){  
    const {data,showArr,selecteds,processModuleName,saveText,insertProcess,symptomIds} = this.props;
    const boxMark = '2';

    let list = data&&data.map((item,i)=>{
      if(item.relationModule && item.relationModule==3){
        return <TailInlineTag {...item} showText={processModuleName} handleTailClick={(obj)=>{insertProcess(obj)}}></TailInlineTag>
      }else {
        return chooseType({item,boxMark,i,hideTag:false,showArr,selecteds,saveText,mainIds:symptomIds});
      }
    })
    return list;
  }

  render(){
    const {fuzhen,type,fetchPushInfos,handleInput,isRead,saveText,searchData,totalHide,showArr,focusIndex,editClear,data} = this.props;
    const {showMoudle,forbidInput,boxMark,boxLeft,boxTop,show} = this.state;
    const searchFlag = searchData.length>0 ? true : false;
    if(+type===1){      //文本模式
      return <Textarea  title='现病史' boxMark='2'
                        isRead={isRead}
                        value={saveText[0]}
                        fuzhen={fuzhen}
                        handlePush={fetchPushInfos}
                        handleInput={handleInput}/>;
    }
    return  <div className={style['current-ill']}>
      <ItemBox title='现病史' editable={editClear||data.length==0?true:false} handleFocus={this.handleFocus} onchange={forbidInput?(e)=>{this.onchange(e)}:''} handleClick={this.handleClick}>
        {data.length>0?this.getInlineTag():''}
      </ItemBox>
      {searchFlag ? <SearchDrop data={searchData} show={!totalHide&&show} onSelect={this.handleSearchSelect} left={boxLeft} top={boxTop} />:""}
    </div>
  }
}

export default CurrentIll;