import React, { Component } from "react";
import style from "./index.less";
import config from '@config/index';
import $ from 'jquery';

class Textarea extends Component {
  constructor(props) {
    super(props);
    this.state = {
      timer:null,
      val:'报告描述或意见'
    };
    this.textInput = React.createRef();
    this.$dom = React.createRef();
    this.$domW = React.createRef();
    this.handleInput = this.handleInput.bind(this);
    this.handleFocus = this.handleFocus.bind(this);
  }
  handleInput(e){
    const {handleChangeAssistValue,idx,handlePush} = this.props;
    const text = e.target.innerText || e.target.innerHTML;
    const stimer = this.state.timer;
    handleChangeAssistValue&&handleChangeAssistValue(text,idx);
    //右侧推送--延时推送
    clearTimeout(stimer);
    let timer = setTimeout(function(){
      handlePush&&handlePush();
      clearTimeout(stimer);
    },config.delayPushTime);
    if(text.trim() != '' && text != '<br>'){
      e.target.nextSibling.innerText?(e.target.nextSibling.innerText = ''):(e.target.nextSibling.innerHTML = '')
    }else{
      e.target.nextSibling.innerText?(e.target.nextSibling.innerText = '报告描述或意见'):(e.target.nextSibling.innerHTML = '报告描述或意见')
    }
    this.setState({
      timer
    });
  }
  shouldComponentUpdate(next){
    if(JSON.stringify(next) == JSON.stringify(this.props)){
      return false;
    }
    return true;
  }
  componentWillReceiveProps(next){
    const isRead = this.props.isRead;
    if(next.isRead != isRead){
      this.$dom.current.innerText?(this.$dom.current.innerText = next.value||'') : (this.$dom.current.innerHTML = next.value||'')
      if(next.value && next.value.trim()){
        this.$dom.current.nextSibling.innerText = ''
      }else{
        this.$dom.current.nextSibling.innerHTML = '报告描述或意见'
      }
    }
  }
  componentDidMount(){
    const {value} = this.props;
    if(value && value.trim()){
      this.$dom.current.innerText?(this.$dom.current.innerText = value) : (this.$dom.current.innerHTML = value)
      this.$dom.current.nextSibling.innerText?(this.$dom.current.nextSibling.innerText = ''):(this.$dom.current.nextSibling.innerHTML = '')
    }else{
      this.$dom.current.nextSibling.innerText?(this.$dom.current.nextSibling.innerText = '报告描述或意见'):(this.$dom.current.nextSibling.innerHTML = '报告描述或意见')
    }
  }
  handleFocus(){     //ie8下提示语挡住输入框，导致输入框无法聚焦
    this.textInput.current.previousSibling.focus();
  }
  render() {
    const {idx,disabled} = this.props;
    return (
      <div className={style.textWap}>
        <div className={style.divTextarea}
            contenteditable={disabled?false:true}
            ref={this.$dom}
            onInput={this.handleInput}
            onKeyUp={this.handleInput}
        ></div>
        <p ref={this.textInput} onClick={this.handleFocus} className={style.textareaWarring}></p>
      </div>
    );
  }
}
export default Textarea;
