import React, { Component } from 'react';
import style from './index.less';
import {ConfirmModal} from '@commonComp';
import Notify from '@commonComp/Notify';
import store from '@store';
import infoShow from '@common/images/info-show.png';
import infoMove from '@common/images/info-move.png';
import {getChronic} from '@store/async-actions/homePage.js';
import {storageLocal} from '@utils/tools';
import {
    embedPush
  } from '@store/async-actions/pushMessage';

class DiagnosticItem extends Component{
    constructor(props){
        super(props);

        this.state = {
            visible: false,
            hasEnterItem: false,
            hasEnterImg: false
        }

        this.addDiagodal = this.addDiagodal.bind(this);
        this.chooseDiagodal = this.chooseDiagodal.bind(this);
        this.confirm = this.confirm.bind(this);
        this.cancel = this.cancel.bind(this)
        this.close = this.close.bind(this)
        this.getTips = this.getTips.bind(this)
    }
    confirm() {
        this.props.setHighter&&this.props.setHighter(80)
        this.close();
        const diagType = 1;
        this.addDiagodal(diagType)
    }
    cancel() {
        this.props.setHighter&&this.props.setHighter(80)
        this.close();
        const diagType = 2;
        this.addDiagodal(diagType)
    }
    close(){
        this.setState({
            visible: false
        })
    }
    chooseDiagodal(item) {
        const { diagnosticList,isTcm,tcmList,showSym} = this.props;
        if(isTcm){ //添加的是中医症候，判断是否重复，添加中医诊断不需要判断
            if(showSym){
                const lastTcmDiag = tcmList[tcmList.length-1].tcmDiag;
                for (let i = 0; i < tcmList.length-1; i++) {
                    if(tcmList[i].tcmDiag+tcmList[i].tcmSyndrome=== lastTcmDiag+item.name) {
                        Notify.info('该中医诊断+中医证候已存在');
                        return
                    }
                }
            }
        }else{
            for (let i = 0; i < diagnosticList.length; i++) {
                if(diagnosticList[i].name === item.name) {
                    Notify.info('该诊断已存在');
                    return
                }
            }
        }
        this.props.setHighter&&this.props.setHighter(80)
        const diagType = 1;
        this.addDiagodal(diagType)
        this.setState({
            visible: true
        })
       
    }
    getTips(item, e) {
        e.stopPropagation();
        const {getTips } = this.props;
        getTips && getTips({id:item.id,type:7,name: item.name, position: 1});
    }
    addDiagodal(diagType){
        const {item,EMRScrollCont,isTcm,showSym,type,addDiagnostic, clearInput, hideSearch} = this.props;
        item.type = diagType;
         /*if(item.type == 2&&mode=='0') {      //文本模式不走慢病。6.0慢病取消
             isChronicMag(item);
         }*/
         addDiagnostic&&addDiagnostic(item,isTcm,showSym);
         clearInput&&clearInput();
         hideSearch&&hideSearch();
        this.context.scrollArea&&this.context.scrollArea.refresh();
         //document.getElementById("diagnosisResult").scrollIntoView(true)
         if(type == 'disSelect') {
            const scrollTop = document.getElementById("diagnosisResult").offsetTop
            setTimeout(() =>{
               EMRScrollCont.scrollYTo(scrollTop);
            })
         }
        showSym&&store.dispatch(embedPush({action: "diag",isTcm:true}))

    }
    handleMouseEnterDrug() {
        this.setState({
            hasEnterItem: true, 
        })
      }
      handleMouseLeaveDrug() {
        this.setState({
            hasEnterItem: false, 
        })
      }
      handleMouseEnterImg() {
        this.setState({
          hasEnterImg: true
        })
      }
      handleMouseLeaveImg() {
        this.setState({
          hasEnterImg: false
        })
      }

    render(){
        const { visible,hasEnterItem,hasEnterImg } = this.state
        const { item, title, type } = this.props
        return (<span className={style['diag-item']} >
                    <span className={`${style['diag-name']}`} 
                          title = {item.name}
                          onMouseEnter={this.handleMouseEnterDrug.bind(this)}
                          onMouseLeave = {this.handleMouseLeaveDrug.bind(this)}
                          onClick={() =>{this.chooseDiagodal(item)}}
                          >
                        <p className={`${style['diag-name-box']} ${type == 'search'?style['diag-name-search']:style['diag-name-disSelect']}`}>{item.name} </p>
                        {/* {type== 'disSelect' &&<img className={style['info-img']}  
                                title='点击i图标可查看详细说明'
                                style ={hasEnterItem  ? {display: "inline-block"} : {display: "none"}}
                                src={hasEnterImg ? infoMove : infoShow} 
                                onMouseEnter={this.handleMouseEnterImg.bind(this)}
                                onMouseLeave = {this.handleMouseLeaveImg.bind(this)}
                                onClick={this.getTips.bind(this,item)}
                        />} */}
                    </span>
                    {/* <ConfirmModal visible={visible} okText='初诊' cancelText='复诊' confirm={this.confirm}  cancel={this.cancel} close={this.close}>
                        <div className={style['confirm-info']}>确定选择“{item.name}”为</div> 
                    </ConfirmModal> */}
                   
                </span>)
    }
}
DiagnosticItem.contextTypes = {
  scrollArea: React.PropTypes.object
};
export default DiagnosticItem;