import React, { Component } from 'react';
import style from './index.less';
import {ConfirmModal} from '@commonComp';
import Notify from '@commonComp/Notify';

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

        this.state = {
            visible: 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)
    }
    confirm() {
        this.close();
        const diagType = 1;
        this.addDiagodal(diagType)
    }
    cancel() {
        this.close();
        const diagType = 2;
        this.addDiagodal(diagType)
    }
    close(){
            this.setState({
                visible: false
            })
        }
    chooseDiagodal(item) {
        const { diagnosticList,getTips } = this.props;
        getTips && getTips(item);
        for (let i = 0; i < diagnosticList.length; i++) {
            if(diagnosticList[i].id === item.id && diagnosticList[i].name === item.name) {
                Notify.info('该诊断已存在');
                return
            }
        }
        this.setState({
            visible: true
        })
       
    }
    addDiagodal(diagType){
        const {item} = this.props;
        item.type = diagType;
        // setTimeout(()=>{
        //     this.setState({
        //         visible: false,
        //     },()=>{
                const { diagnosticList, addDiagnostic, clearInput, hideSearch } = this.props;
                // for (let i = 0; i < diagnosticList.length; i++) {
                //     if(diagnosticList[i].id === item.id && diagnosticList[i].name === item.name) {
                //         Notify.info('该诊断已存在');
                //         return
                //     }
                // }
                addDiagnostic&&addDiagnostic(item);
                clearInput&&clearInput();
                hideSearch&&hideSearch()
        //     })
        // }, 0)
        document.getElementById("diagnosisResult").scrollIntoView(true)
    }

    render(){
        const { visible } = this.state
        const { item, title } = this.props
        return (<span className={style['diag-item']} >
                    <span className={style['diag-name']} 
                          title = {title && item.name + (item.showType === 2 || item.showType === 3 ? '('+ item.retrievalName+')': '')}
                          onClick={() =>{this.chooseDiagodal(item)}}>
                        {item.name} {item.showType === 2 || item.showType === 3 ? '('+ item.retrievalName+')': ''}
                    </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>)
    }
}

export default DiagnosticItem;