import React, { Component } from 'react';
import style from './index.less';
import del_icon from './img/delete.png'
import diagUp from './img/diagUp.png'
import diagDown from './img/diagDown.png'
import {ConfirmModal, Message} from '@commonComp';
import Notify from '@commonComp/Notify';
import Treat from '@containers/Treat'
import store from '@store';
import { initItemList } from '@store/async-actions/historyTemplates';
import { pushAllDataList } from '@utils/tools';
import iconRadioDefault from '@common/images/icon-radio-default.png'
import iconRadioActive from '@common/images/icon-radio-active.png'

class DiagnosticList extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            delItem: {},
            treatTitle: '',
            activeHistory: -1
        }
        this.deleteItem = this.deleteItem.bind(this);
        this.cancel = this.cancel.bind(this);
        this.close = this.close.bind(this);
        this.showTreat = this.showTreat.bind(this);
        this.handleClickDiag = this.handleClickDiag.bind(this);
        this.referRecord = this.referRecord.bind(this);
        this.noReferRecord = this.noReferRecord.bind(this);
        this.getHistoryCase = this.getHistoryCase.bind(this);
        this.closeHistoryCaseModal = this.closeHistoryCaseModal.bind(this);
        this.referCase = this.referCase.bind(this);
    }

    componentWillReceiveProps(nextprops) {
        if (this.props.diagnosticStr != nextprops.diagnosticStr) {
            this.props.getBilling();
        }
        
    }

    upDiagnostic(index) {
        const { upDiagnostic } = this.props;
        upDiagnostic && upDiagnostic(index)
    }
    downDiagnostic(index) {
        const { downDiagnostic } = this.props;
        downDiagnostic && downDiagnostic(index)
    }
    deleteItem() {
        const { delItem } = this.state;
        const { delDiagnostic } = this.props;
        delDiagnostic && delDiagnostic(delItem);
        this.setState({
            visible: false,
        })
        Notify.success('删除成功')
    }
    cancel() {
        this.setState({
            visible: false
        })
    }
    close() {
        this.setState({
            visible: false
        })
    }
    handleDeleteItem(item) {
        this.setState({
            visible: true,
            delItem: item,
        })
    }
    showTreat(item, index) {
        // item.treatIndex = index
        const  { showTreat, getTreatResult } = this.props;
        getTreatResult && getTreatResult(item);
        showTreat && showTreat();
        this.setState({
            treatTitle: item.name
        })
    } 
    handleClickDiag(item) {
        const { getTips } = this.props;
        getTips && getTips(item);
    }
    referRecord() {
        const { hideReferRecord, showHistoryCaseModal } = this.props
        hideReferRecord && hideReferRecord()
        showHistoryCaseModal && showHistoryCaseModal()
        store.dispatch(initItemList());

    }
    noReferRecord() {
        const { hideReferRecord } = this.props
        hideReferRecord && hideReferRecord()
    }
    referCase() {
        const { hideHistoryCaseModal, items } = this.props
        hideHistoryCaseModal && hideHistoryCaseModal()
        if (this.state.activeHistory === -1) {
            return
        }
        let baseList = store.getState();
        
        let baseObj = items[this.state.activeHistory];
  
        // store.dispatch({type: CONFIRM_TYPE, confirmType: baseObj.sign});
        pushAllDataList(baseObj.sign,'push',baseObj,'history')       //引用
        this.props.getBilling();
        this.setState({
            activeHistory: -1
        })
    }
    closeHistoryCaseModal() {
        const { hideHistoryCaseModal } = this.props
        this.setState({
            activeHistory: -1
        })
        hideHistoryCaseModal && hideHistoryCaseModal()
    }
    handleQuoteClick(item, index) {
        console.log('asadsd', item)
        const { handleQuoteClick } = this.props
        // handleQuoteClick && handleQuoteClick(item)
        this.setState({
            activeHistory: index
        })
    }
    getHistoryCase() {
        const { items } = this.props
        return (<div className={style['history-info']}>
            {items.map((item, index) => {
                return<div onClick={this.handleQuoteClick.bind(this, item, index)} style={this.state.activeHistory === index ? {color: '#abcdef'} : ''}>
                    <img src={this.state.activeHistory === index ? iconRadioActive : iconRadioDefault}/>{item.diagnose}
                </div>
            })}
        </div>)
    }
    
    

    render(){
        const { list, treatment, showReferRecord, showHistoryCase } = this.props
        const { visible, treatTitle } = this.state
        const lastIndex = list.length -1;
        return(
                <div className={style['diaglist-wrap']}>
                    {list && (list.length > 0) && list.map((item, index) => {
                        const hasTreat = item.treat && (item.treat.commonTreatment.content || item.treat.surgeryTreatment.content || item.treat.treatment.length>0)
                        return (<div draggable={true} className={style['diag-box'] + ' clearfix'}  key={item.id} >
                                    {index === 0 ? '' : <span className={style['diag-up']} onClick={() => {this.upDiagnostic(index)}}><img className={style["diag-up"]} src={diagUp}/></span>}
                                    {list.length === 1 ? '' : index !== 0 ? '' : <span onClick={() => {this.downDiagnostic(index)}} className={style['diag-down']}><img className={style["diag-down"]} src={diagDown}/></span>}
                                    <span className={style['diag-number']} style={{fontWeight:index === 0 ?'bold':'normal'}}>{index === 0 ? '主' : index+1}</span>
                                    <span className={style['diag-name']} onClick={()=>{this.handleClickDiag(item)}}>{item.name}<span></span></span> 
                                    {item.type === 1 ? <span className={style['diag-first']}>初诊</span> :<span className={style['diag-second']}> 复诊</span>}
                                    <span className={style['treat']}
                                        //   style ={{ color: hasTreat ?'' : 'gray', border: hasTreat ?'1px solid #3B9ED0' : '1px solid gray', cursor: hasTreat ? '' : 'text'}}
                                          style ={hasTreat ? '' : { color: 'gray', border: '1px solid gray', cursor: 'auto'}}
                                          onClick={() =>{hasTreat && this.showTreat(item, index)}}>
                                          治疗方案
                                    </span>
                                    <img className={style['diag-del']} src={del_icon} onClick={()=>{this.handleDeleteItem(item)}}/>
                        </div>)
                    })}
                     {treatment&&<Treat title={treatTitle}></Treat>}
                    <ConfirmModal visible={visible} 
                                  okText='删除' 
                                  cancelText='取消' 
                                  confirm={this.deleteItem}  
                                  cancel={this.cancel}
                                  close={this.close}
                                  >
                                  <div className={style['del-msg']}>是否删除该诊断？</div> 
                    </ConfirmModal>
                    <ConfirmModal visible={showReferRecord} okText='是' cancelText='否' confirm={this.referRecord}  cancel={this.noReferRecord} close={this.noReferRecord}>
                        <div className={style['confirm-info']}>是否引用往期病历</div> 
                    </ConfirmModal>
                    <ConfirmModal visible={showHistoryCase} noFooter='true' title='请选择历史病历' close={this.closeHistoryCaseModal}>
                        {this.getHistoryCase()}
                        <div className={style['confirm-history']}><span className={style['confirm-history-btn']} onClick={this.referCase}>确定</span></div>
                    </ConfirmModal>
                </div>
               
            
        )
    }
}

export default DiagnosticList;