import React, { Component } from "react";
import styles from "./index.less";
import { normalVal,getArrow,timestampToTime,getDomUpDown,getPageCoordinate,setPosition,setFontColorSize } from '@utils/tools';
import { InspectCommon, Notify,DelToast} from '@commonComp';
import slideUp from "@common/images/slide-up.png";
import slideDown from "@common/images/slide-down.png";
import $ from 'jquery';
import checkOff from '@common/images/check_off.png';
import checkOn from '@common/images/check_on.png';
import ScrollArea from 'react-scrollbar';
import InspectName from '../InspectName';

import up from '@images/up.png';
import down from '@images/down.png';

class SlidePic extends Component {
  constructor(props) {
    super(props);
    this.state = {
      show:false,
      activeInd:false,
      activeName:'',
      canEdit:true,
      style:'',
    };
    this.toTime = this.toTime.bind(this);
    this.handleSlide = this.handleSlide.bind(this);
    this.handleDel = this.handleDel.bind(this);
    this.handleCancel = this.handleCancel.bind(this);
    this.delConfirm = this.delConfirm.bind(this);
    this.timeSure = this.timeSure.bind(this);
    this.setEdit = this.setEdit.bind(this);
    this.handleLabelSub = this.handleLabelSub.bind(this);
    this.handleBlur = this.handleBlur.bind(this);
    this.checkOnOff = this.checkOnOff.bind(this);
  }
  handleChangeDate(date) {
    // console.log(date,'外')
  }
  
  componentDidMount() {
    const {item} = this.props;
    $(document).click((event) => {
      if($(event.target).attr("id")!='addClose'&&$(event.target).attr("id")!='delTit'){
        this.setState({
          activeInd:false
        })
      }
    });
    let type = getArrow(item.minValue,item.maxValue,item.value||null)
    this.setState({style:type,value:item.value,time:item.time})
    const that = this;
    document.addEventListener('mousedown',function(e){
      //onMousedown的目标为滚动条时，标签填写单不关闭
      if(e.target.className=='scrollbar'){
        that.isBar = true;
      }else{
        that.isBar = false;
      }
    });
    // this.setState({ dateTime: getCurrentDate(1) })
  }
  toTime(time){
    let tmpTim = time.split(',').join('')-0
    if(time && tmpTim.toString() != 'NaN'){
      let date = new Date('1900-01-01');
      let dateTim = date.getTime();
      let times = (tmpTim-2)*24*60*60*1000;
      let result = timestampToTime(dateTim+times).split(' ')[0]
      return result;
    }else{
      return time;
    }
  }
  handleSlide(){
    let tmpShow = this.state.show
    this.setState({
      show:!tmpShow
    })
  }
  handleDel(time){
    const {handleDelClick,item} = this.props;
    this.setState({
      activeInd:true,
      activeName:item.uniqueName,
    })
    handleDelClick&&handleDelClick(1,time);
  }
  handleCancel(){
    this.setState({
      activeInd:false,
      activeName:''
    })
  }
  delConfirm(item){
    const {handleDelConfirm,handlePush} = this.props;
    handleDelConfirm&&handleDelConfirm(item);
    Notify.success("删除成功");
    handlePush && handlePush({mode:8});       //右侧推送
    this.setState({
      activeInd:false,
      activeName:''
    })
  }
  timeSure(date){
    const {handleChangeDate} = this.props;
    handleChangeDate&&handleChangeDate(date)
  }
  handleLabelSub(e,questionId,idx){
    const {handleLabelSub,handleFillShow,setHighter,refreshScroller} = this.props;
    handleLabelSub(e,questionId,idx);
    handleFillShow(e,idx);
    //弹窗高度超出屏幕，增加页面高度
    setPosition(e,"#inspectFill",setHighter);
    /*setTimeout(function(){//如果检验下面有很多数据，则会跳过头
      refreshScroller()&&refreshScroller().scrollYTo(290);
    })*/
  }
  handleInput(e,item,sign){
    const {setTipValue} = this.props
    setTipValue(item,e.target.value,sign)
  }
  handleBlur(){
    const {handlePush} = this.props;
    $('.canEdit').attr('disabled','disabled')
    handlePush && handlePush({mode:8});       //右侧推送
  }
  checkOnOff(item){
    const {checkOnOff,handlePush} = this.props
    checkOnOff(item)
    handlePush && handlePush({mode:8});       //右侧推送
  }
  setEdit(e){
    $('.canEdit').blur().attr('disabled','disabled')
    $(e.target).removeAttr('disabled').focus()
  }
  render() {
    const {item,time} = this.props;
    const {canEdit,activeInd,activeName,value,style} = this.state;
    return (
      <li key={item.time} className={`${styles.slideLi} clearfix`}> 
          <img className={styles.imgCheck} src={item.check?checkOn:checkOff} onClick={()=>this.checkOnOff(item)} alt=""/>
          <span className={styles.bigname}>{item.name}</span>
          <span className={styles.smallname}>{item.flg == 5?'药品':'手术/操作'}</span>
          <span className={styles.edit}>
            {
              item.flg == 5?
              <input disabled='disabled' type="text" 
                class="canEdit" 
                onDoubleClick={(e)=>this.setEdit(e)}
                style={{color:style==2||style==4?'red':style==1?'#D949FF':'#333'}}
                placeholder='(填写用法计量)'
                autoComplete="off"
                value={item.value}
                onInput={(e)=>{this.handleInput(e,item,2)}}
                onBlur={()=>{this.handleBlur()}}
                />:null
            }
          </span>
          <span className={styles.pass}>
            <input disabled="disabled" type="text" 
                class="canEdit" 
                onDoubleClick={(e)=>this.setEdit(e)}
                style={{color:'#333'}}
                placeholder='时间'
                autoComplete="off"
                value={item.time}
                onInput={(e)=>{this.handleInput(e,item,1)}}
                onBlur={()=>{this.handleBlur()}}
                />
          </span>
          <span id="addClose" className={styles.partDel} onClick={()=>{this.handleDel(item.time)}}></span>
          <DelToast show={time==item.time&&activeInd?true:false}
                    name={activeName}
                    right={'-34px'}
                    top={'30px'}
                    cancel={this.handleCancel}
                    confirm={()=>{this.delConfirm(item)}}/>
      </li>
    );
  }
}
export default SlidePic;
