/**
 * 处理医嘱清单
 */
import React from 'react';
import { Modal, Row, Col, Select, Card, Radio, Input, Button, Tree, Icon, Tag, Table, Divider, message } from 'antd';
import { Util, dayFormat, timeFormat } from 'tools/index';
import { $http } from 'containers/config/https';
import ShowCommonTableModal from 'pages/workstation/component/ShowCommonTableModal';
import ColumnAuthoritysModel from 'pages/columnAuthority/ColumnAuthoritysModel';
import '../style/index.less';
import moment from 'moment';

const Option = Select.Option;
const RadioButton = Radio.Button;
const confirm = Modal.confirm;
const RadioGroup = Radio.Group;
const { TreeNode } = Tree;

class ShowCollectionCostModal extends React.Component {

    constructor(props) {
        super(props);
        var userData = JSON.parse(sessionStorage.getItem('userData'));
        this.state = {
            userData: userData,
            radioValue: '1',
            dataSource: [{ indexTable: 0 }],
            ids: []
        }
        this.ordMedItemDesc = React.createRef();
    }
    componentWillReceiveProps(nextProp) {
        if (nextProp.isShow === true) {
            this.setState({
                patientMsg: nextProp.patientMsg,
                patientIDs: nextProp.patientIDs,
            }, () => {
                this.loadModalColumns()
                this.loadModuleData()
            })
        } else if (this.props.isShow === false) {
            this.clearInput()
        }
    }

    componentDidMount() {
        this.initData()
    }

    clearInput(){
        this.setState({
            patientMsg:[],
            dataSource:[{ indexTable: 0 }],
            ids:[]
        })
    }

    loadModalColumns() {
        this.sunColumns = [
            {
                title: '编码',
                dataIndex: 'medCode',
                width: 120,
                align: 'left',
            }, {
                title: '名称*规格',
                dataIndex: 'medDesc',
                width: 260,
                align: 'left',
            }, {
                title: '库存',
                dataIndex: 'medStock',
                width: 80,
                align: 'left',
            }, {
                title: '单价(元)',
                dataIndex: 'medPrice',
                width: 80,
                align: 'left',
            }, {
                title: '大类',
                dataIndex: 'medCatDesc',
                width: 100,
                align: 'left',
            }, {
                title: '子类',
                dataIndex: 'medSubCatDesc',
                width: 200,
                align: 'left',
            }
        ]
    }
    initData() {
        let data = {
            params: [{
                compontName: "addAdviceListPage",
                type: "C",
            }]
        }
        $http.post('urlDeault', this, {
            code: "01040073",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    var columns = res.result.C;
                    var dynColumns = []
                    for (var i = 0; i < columns.length; i++) {
                        if (columns[i].dataIndex == 'ordMedItemDesc') {
                            dynColumns.push({
                                title: columns[i].title,
                                dataIndex: columns[i].dataIndex,
                                width: parseInt(columns[i].width),
                                align: columns[i].align || 'center',
                                render: (text, data, index) => {
                                    data.tabIndex = index + 1
                                    return <Input style={{ width: '98%' }} id={"name-inputs" + index}
                                        onKeyDown={this.showCommonTableModal.bind(this, index)} allowClear={true}
                                        autoFocus ref={this.ordMedItemDesc}
                                        value={data.ordMedItemDesc ? data.ordMedItemDesc : this.state.changeValue}
                                        // value={this.state.changeValue?this.state.changeValue:data.ordMedItemDesc}
                                        onChange={this.handleSearchInput.bind(this, 'ordMedItemDesc', index)}
                                    />
                                }
                            })
                        } else if (columns[i].dataIndex == 'ordQtyUomID') {
                            dynColumns.push({
                                title: columns[i].title,
                                dataIndex: columns[i].dataIndex,
                                width: parseInt(columns[i].width),
                                align: columns[i].align || 'center',
                                render: (text, data, index) => {
                                    var qtyUnitListOption = []
                                    // var { qtyUnitList } = this.state;
                                    var qtyUnitList = data.qtyUnitList;
                                    var newData = []
                                    qtyUnitList && qtyUnitList.map((item, index) => {
                                        qtyUnitListOption.push(<Option key={index} code={item.price} value={item.unitID} qtyMolecule={item.molecule}
                                            onClick={this.handleOnSelectColumns.bind(this, 'ordQtyUomID', index, data)}
                                            qtyDenominator={item.denominator} price={item.price}>{item.unitDesc}</Option>)
                                    })
                                    return <Select onChange={(e, option) => this.changeCommonSelect('ordQtyUomID', index, data, e, option)} style={{ width: '98%' }}
                                        showArrow={false}
                                        onSelect={this.handleOnSelectColumns.bind(this, 'ordQtyUomID', index, data)}
                                        value={data.ordQtyUomID}>{qtyUnitListOption}</Select>
                                }
                            })
                        } else if (columns[i].dataIndex == 'ordPrice') {
                            dynColumns.push({
                                title: columns[i].title,
                                dataIndex: columns[i].dataIndex,
                                width: parseInt(columns[i].width),
                                align: columns[i].align || 'center'
                            })
                        } else if (columns[i].dataIndex == 'ordQty') {
                            dynColumns.push({
                                title: columns[i].title,
                                dataIndex: columns[i].dataIndex,
                                width: parseInt(columns[i].width),
                                align: columns[i].align || 'center',
                                render: (text, data, index) => {
                                    return <Input value={data.ordQty}
                                        onChange={this.commonInput.bind(this, 'ordQty', index, data)} />
                                }
                            })
                        } else if (columns[i].dataIndex == 'totalPrice') {
                            dynColumns.push({
                                title: columns[i].title,
                                dataIndex: columns[i].dataIndex,
                                width: parseInt(columns[i].width),
                                align: columns[i].align || 'center',
                                render: (text, data, index) => {
                                    return <span>{(!Util.isEmpty(data.ordPrice) && !Util.isEmpty(data.ordQty) ?
                                        (parseFloat(data.ordPrice) * parseFloat(data.ordQty)).toFixed(2) : 0)}</span>
                                }
                            })
                        } else {
                            dynColumns.push({
                                title: columns[i].title,
                                dataIndex: columns[i].dataIndex,
                                width: parseInt(columns[i].width),
                                align: columns[i].align || 'center'
                            })
                        }
                    }
                    this.setState({
                        feeColumns: dynColumns,
                    })
                    this.props.hanlderThis.setState({
                        reload: false,
                    })
                }
            }
        })
    }

    //获取列表行数据
    loadTableData(status, record, data) {
        var that = this;
        var date = new Date();
        var { dataSource } = this.state;
        var source = []
        for (var i = 0; i < dataSource.length; i++) {
            if (Util.isEmpty(dataSource[i].ordID) && !Util.isEmpty(dataSource[i].medID)) {
                source.push(dataSource[i])
            }
        }
        var params = {
            params: [{
                admID: this.state.patientIDs?this.state.patientIDs[0]:'',
                indexID: source ? source.length : 0,
                orders: [{
                    medID: status == 'temp' ? data.props.nodeId : (status == 'batch' ? (record[0].ordSetID ? '' : record[0].medID) : record.medID),
                    ordSetID: status == 'temp' ? data.props.ordSetID : (status == 'batch' ? record[0].ordSetID : record.ordSetID || '')
                }]
            }]
        }
        $http.post('urlDeault', this, {
            code: "03060020",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    if (res.errorData.length == 1) {
                        message.error(res.errorData[0].errorMessage)
                        return
                    }
                    if (res.errorData.length > 1) {
                        this.setState({
                            isShowConfirmModal: true,
                            errorData: res.errorData,
                            confirmData: res.confirmData,
                        })
                        return
                    }
                    if (res.confirmData.length > 0) {
                        confirm({
                            title: this.setMessage('DoctorAdviceListPageConfirmation', 'descripts'),
                            content: res.confirmData[0].errorMessage,
                            okText: that.state.userData.language == 'EN' ? 'Ok' : '确定',
                            cancelText: that.state.userData.language == 'EN' ? 'cancel' : '取消',
                            onOk() {
                                that.state.dataSource[that.state.indexTable]['ordMedItemDesc'] = res.data[0].defaultData.ordDesc,
                                    that.state.dataSource[that.state.indexTable]['ordDose'] = res.data[0].defaultData.ordDose,
                                    that.state.dataSource[that.state.indexTable]['ordDoseUnitID'] = res.data[0].defaultData.ordDoseUnitID,
                                    // that.state.dataSource[that.state.indexTable]['ordFreqID'] = res.data[0].defaultData.ordFrequenceID,
                                    that.state.dataSource[that.state.indexTable]['ordFreqID'] = res.data[0].defaultData.ordFreqID,
                                    that.state.dataSource[that.state.indexTable]['ordFreqDesc'] = res.data[0].defaultData.ordFreqDesc,
                                    that.state.dataSource[that.state.indexTable]['ordUsageDesc'] = res.data[0].defaultData.ordUsageDesc,
                                    that.state.dataSource[that.state.indexTable]['ordUsageID'] = res.data[0].defaultData.ordUsageID,
                                    that.state.dataSource[that.state.indexTable]['ordDurQty'] = res.data[0].defaultData.ordDurQty,
                                    that.state.dataSource[that.state.indexTable]['ordDurationID'] = res.data[0].defaultData.ordDurationID,
                                    that.state.dataSource[that.state.indexTable]['ordSkinTestUsageID'] = res.data[0].defaultData.ordSkinTestUsageID,
                                    that.state.dataSource[that.state.indexTable]['ordSkinTestUsageDesc'] = res.data[0].defaultData.ordSkinTestUsageDesc,
                                    that.state.dataSource[that.state.indexTable]['ordQty'] = res.data[0].defaultData.ordQty,
                                    that.state.dataSource[that.state.indexTable]['ordQtyUomID'] = res.data[0].defaultData.ordQtyUomID,
                                    that.state.dataSource[that.state.indexTable]['ordPrice'] = res.data[0].defaultData.ordPrice,
                                    that.state.dataSource[that.state.indexTable]['ordSkinTestFlag'] = res.data[0].defaultData.ordSkinTestFlag,
                                    that.state.dataSource[that.state.indexTable]['testFlag'] = res.data[0].defaultData.ordSkinTestFlag,
                                    that.state.dataSource[that.state.indexTable]['ordSkinTestWayID'] = res.data[0].defaultData.ordSkinTestWayID,
                                    that.state.dataSource[that.state.indexTable]['ordRecLocID'] = res.data[0].defaultData.ordRecLocID,
                                    that.state.dataSource[that.state.indexTable]['ordPriorityID'] = res.data[0].defaultData.ordPriorityID,
                                    that.state.dataSource[that.state.indexTable]['medDesca'] = res.data[0].defaultData.ordDesca,
                                    that.state.dataSource[that.state.indexTable]['ordAdmReasonID'] = res.data[0].defaultData.ordAdmReasonID,
                                    that.state.dataSource[that.state.indexTable]['ordSeqNo'] = parseInt(that.state.indexTable) + 1,
                                    that.state.dataSource[that.state.indexTable]['ordDate'] = res.data[0].defaultData.ordDate ?
                                        moment(res.data[0].defaultData.ordDate).format(dayFormat) : moment(date).format(dayFormat),
                                    that.state.dataSource[that.state.indexTable]['ordTime'] = res.data[0].defaultData.ordTime ?
                                        moment(res.data[0].defaultData.ordTime).format(timeFormat) : moment(date).format(timeFormat),
                                    that.state.dataSource[that.state.indexTable]['ordStartDate'] = moment(date).format(dayFormat),
                                    that.state.dataSource[that.state.indexTable]['ordStartTime'] = moment(date).format(timeFormat),
                                    that.state.dataSource[that.state.indexTable]['ordNote'] = res.data[0].defaultData.ordNote,
                                    that.state.dataSource[that.state.indexTable]['medID'] = status == 'temp' ? data.props.nodeId : (status == 'batch' ? record[0].medID : record.medID),
                                    that.state.dataSource[that.state.indexTable]['ordMedItemID'] = status == 'temp' ? data.props.nodeId : (status == 'batch' ? record[0].medID : record.medID),
                                    that.state.dataSource[that.state.indexTable]['ordTypeCode'] = res.data[0].defaultData.ordTypeCode,
                                    that.state.dataSource[that.state.indexTable]['ordSpecID'] = res.data[0].defaultData.ordSpecID,
                                    that.state.dataSource[that.state.indexTable]['ordToalCountO'] = res.data[0].defaultData.ordToalCountO,
                                    that.state.dataSource[that.state.indexTable]['ordToalCountI'] = res.data[0].defaultData.ordToalCountI,
                                    that.state.dataSource[that.state.indexTable]['frequency'] = res.data[0].defaultData.frequency,
                                    that.state.dataSource[that.state.indexTable]['cycleDays'] = res.data[0].defaultData.cycleDays,
                                    that.state.dataSource[that.state.indexTable]['ordStageID'] = res.data[0].defaultData.ordStageID,
                                    that.state.dataSource[that.state.indexTable]['intervalDays'] = res.data[0].defaultData.intervalDays,
                                    that.state.dataSource[that.state.indexTable]['duraFactor'] = res.data[0].defaultData.duraFactor,
                                    that.state.dataSource[that.state.indexTable]['ordSkinTestFlagValue'] = res.data[0].defaultData.ordSkinTestFlagValue,
                                    that.state.dataSource[that.state.indexTable]['ordEMFlagValue'] = res.data[0].defaultData.ordEMFlagValue,
                                    that.state.dataSource[that.state.indexTable]['ordEMFlag'] = res.data[0].defaultData.ordEMFlag,
                                    that.state.dataSource[that.state.indexTable]['testEMFlag'] = res.data[0].defaultData.ordEMFlag,
                                    that.state.dataSource[that.state.indexTable]['ordSkinTestFlag'] = res.data[0].defaultData.ordSkinTestFlag,
                                    that.state.dataSource[that.state.indexTable]['testFlag'] = res.data[0].defaultData.ordSkinTestFlag,
                                    that.state.dataSource[that.state.indexTable]['doseDenominator'] = res.data[0].defaultData.doseDenominator,
                                    that.state.dataSource[that.state.indexTable]['doseMolecule'] = res.data[0].defaultData.doseMolecule,
                                    that.state.dataSource[that.state.indexTable]['qtyMolecule'] = res.data[0].defaultData.qtyMolecule,
                                    that.state.dataSource[that.state.indexTable]['qtyDenominator'] = res.data[0].defaultData.qtyDenominator,
                                    that.state.dataSource[that.state.indexTable]['ordPriorityExtID'] = res.data[0].defaultData.ordPriorityExtID,
                                    that.state.dataSource[that.state.indexTable]['ordPriorityExtDesc'] = res.data[0].defaultData.ordPriorityExtDesc,
                                    that.state.dataSource[that.state.indexTable]['ordMessage'] = res.data[0].defaultData.ordMessage,
                                    that.state.dataSource[that.state.indexTable]['qtyUnitList'] = res.data[0].dataList.qtyUnitList,
                                    that.setState({
                                        ordMessage: res.data[0].defaultData.ordMessage,
                                        tableData: res.data[0].defaultData,
                                        qtyUnitList: res.data[0].dataList.qtyUnitList,//数量单位
                                        changeValue: undefined,
                                    })
                                // that.deleteNullData()
                            },
                            onCancel() {
                                console.log('Cancel');
                            },
                        });
                    } else {
                        //双击医嘱项，根据index，渲染对应行的默认数据
                        this.state.dataSource[this.state.indexTable]['ordMedItemDesc'] = res.data[0].defaultData.ordDesc,
                            this.state.dataSource[this.state.indexTable]['ordDose'] = res.data[0].defaultData.ordDose,
                            this.state.dataSource[this.state.indexTable]['ordDoseUnitID'] = res.data[0].defaultData.ordDoseUnitID,
                            // this.state.dataSource[this.state.indexTable]['ordFreqID'] = res.data[0].defaultData.ordFrequenceID,
                            this.state.dataSource[this.state.indexTable]['ordFreqID'] = res.data[0].defaultData.ordFreqID,
                            that.state.dataSource[that.state.indexTable]['ordFreqDesc'] = res.data[0].defaultData.ordFreqDesc,
                            that.state.dataSource[that.state.indexTable]['ordUsageDesc'] = res.data[0].defaultData.ordUsageDesc,
                            this.state.dataSource[this.state.indexTable]['ordUsageID'] = res.data[0].defaultData.ordUsageID,
                            this.state.dataSource[this.state.indexTable]['ordDurQty'] = res.data[0].defaultData.ordDurQty,
                            this.state.dataSource[this.state.indexTable]['ordDurationID'] = res.data[0].defaultData.ordDurationID,
                            this.state.dataSource[this.state.indexTable]['ordQty'] = res.data[0].defaultData.ordQty,
                            this.state.dataSource[this.state.indexTable]['ordQtyUomID'] = res.data[0].defaultData.ordQtyUomID,
                            this.state.dataSource[this.state.indexTable]['ordPrice'] = res.data[0].defaultData.ordPrice,
                            this.state.dataSource[this.state.indexTable]['ordSkinTestFlag'] = res.data[0].defaultData.ordSkinTestFlag,
                            this.state.dataSource[this.state.indexTable]['testFlag'] = res.data[0].defaultData.ordSkinTestFlag,
                            this.state.dataSource[this.state.indexTable]['ordSkinTestWayID'] = res.data[0].defaultData.ordSkinTestWayID,
                            this.state.dataSource[this.state.indexTable]['ordRecLocID'] = res.data[0].defaultData.ordRecLocID,
                            this.state.dataSource[this.state.indexTable]['ordPriorityID'] = res.data[0].defaultData.ordPriorityID,
                            this.state.dataSource[this.state.indexTable]['ordStageID'] = res.data[0].defaultData.ordStageID,
                            this.state.dataSource[this.state.indexTable]['medDesca'] = res.data[0].defaultData.ordDesca,
                            this.state.dataSource[this.state.indexTable]['ordSkinTestUsageID'] = res.data[0].defaultData.ordSkinTestUsageID,
                            this.state.dataSource[this.state.indexTable]['ordSkinTestUsageDesc'] = res.data[0].defaultData.ordSkinTestUsageDesc,
                            this.state.dataSource[this.state.indexTable]['ordEMFlag'] = res.data[0].defaultData.ordEMFlag,
                            this.state.dataSource[this.state.indexTable]['testEMFlag'] = res.data[0].defaultData.ordEMFlag,
                            this.state.dataSource[this.state.indexTable]['ordSkinTestFlag'] = res.data[0].defaultData.ordSkinTestFlag,
                            this.state.dataSource[this.state.indexTable]['testFlag'] = res.data[0].defaultData.ordSkinTestFlag,
                            this.state.dataSource[this.state.indexTable]['ordAdmReasonID'] = res.data[0].defaultData.ordAdmReasonID,
                            this.state.dataSource[this.state.indexTable]['ordSeqNo'] = parseInt(this.state.indexTable) + 1,
                            this.state.dataSource[this.state.indexTable]['ordDate'] = res.data[0].defaultData.ordDate ?
                                moment(res.data[0].defaultData.ordDate).format(dayFormat) : moment(date).format(dayFormat),
                            this.state.dataSource[this.state.indexTable]['ordTime'] = res.data[0].defaultData.ordTime ?
                                moment(res.data[0].defaultData.ordTime).format(timeFormat) : moment(date).format(timeFormat),
                            this.state.dataSource[this.state.indexTable]['ordStartDate'] = moment(date).format(dayFormat),
                            this.state.dataSource[this.state.indexTable]['ordStartTime'] = moment(date).format(timeFormat),
                            this.state.dataSource[this.state.indexTable]['ordNote'] = res.data[0].defaultData.ordNote,
                            this.state.dataSource[this.state.indexTable]['medID'] = status == 'temp' ? data.props.nodeId : (status == 'batch' ? record[0].medID : record.medID),
                            this.state.dataSource[this.state.indexTable]['ordMedItemID'] = status == 'temp' ? data.props.nodeId : (status == 'batch' ? record[0].medID : record.medID),
                            this.state.dataSource[this.state.indexTable]['ordTypeCode'] = res.data[0].defaultData.ordTypeCode,
                            this.state.dataSource[this.state.indexTable]['ordSpecID'] = res.data[0].defaultData.ordSpecID,
                            this.state.dataSource[this.state.indexTable]['ordSkinTestFlagValue'] = res.data[0].defaultData.ordSkinTestFlagValue,
                            this.state.dataSource[this.state.indexTable]['ordEMFlagValue'] = res.data[0].defaultData.ordEMFlagValue,
                            this.state.dataSource[this.state.indexTable]['ordToalCountO'] = res.data[0].defaultData.ordToalCountO,
                            this.state.dataSource[this.state.indexTable]['ordToalCountI'] = res.data[0].defaultData.ordToalCountI,
                            this.state.dataSource[this.state.indexTable]['frequency'] = res.data[0].defaultData.frequency,
                            this.state.dataSource[this.state.indexTable]['cycleDays'] = res.data[0].defaultData.cycleDays,
                            this.state.dataSource[this.state.indexTable]['intervalDays'] = res.data[0].defaultData.intervalDays,
                            this.state.dataSource[this.state.indexTable]['duraFactor'] = res.data[0].defaultData.duraFactor,
                            this.state.dataSource[this.state.indexTable]['doseDenominator'] = res.data[0].defaultData.doseDenominator,
                            this.state.dataSource[this.state.indexTable]['doseMolecule'] = res.data[0].defaultData.doseMolecule,
                            this.state.dataSource[this.state.indexTable]['qtyMolecule'] = res.data[0].defaultData.qtyMolecule,
                            this.state.dataSource[this.state.indexTable]['qtyDenominator'] = res.data[0].defaultData.qtyDenominator,
                            this.state.dataSource[this.state.indexTable]['ordPriorityExtDesc'] = res.data[0].defaultData.ordPriorityExtDesc,
                            this.state.dataSource[this.state.indexTable]['ordPriorityExtID'] = res.data[0].defaultData.ordPriorityExtID,
                            this.state.dataSource[this.state.indexTable]['qtyUnitList'] = res.data[0].dataList.qtyUnitList,
                            this.setState({
                                tableData: res.data[0].defaultData,
                                ordMessage: res.data[0].defaultData.ordMessage,
                                qtyUnitList: res.data[0].dataList.qtyUnitList,//数量单位
                                changeValue: undefined,
                            })
                    }
                }
            }
        });
    }

    loadDateTime() {
        var params = {
            params: [{}]
        }
        $http.post('urlS', this, {
            code: '04030004',
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        dateFrom: res.date,
                        timeFrom: res.time,
                    })
                }
            }
        })
    }

    loadModuleData() {
        var params = {
            params: [{
                orderTemplTypeFlag: '1'
            }]
        }
        $http.post('urlS', this, {
            code: '03050017',
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        moduleData: res.orderTemplType,
                        defaultTemplTypeID: res.defaultTemplTypeID
                    }, () => {
                        this.loadTemplateData()
                    })
                }
            }
        })
    }

    /**
     * @shizi: noCheckDetailFlag为Y不返回dataList明细数据，解决数据过多时接口返回很慢
     * Create 2021-06-13
     */
    loadTemplateData() {
        var params = {
            params: [{
                input: this.state.templateInput,
                templetCatCode: '01',
                templetTypeID: this.state.radioValue ? this.state.radioValue : this.state.defaultTemplTypeID,
                noCheckDetailFlag: 'Y'
            }],
        }
        $http.post('urlDeault', this, {
            code: "03050023",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        templateData: res.result
                    })
                }
            }
        });
    }

    handleOnSelectColumns(name, index, data, e) {
        this.state.dataSource[index][name] = e
        this.setState({})
    }

    commonInput(name, index, data, e) {
        this.state.dataSource[index][name] = e.target.value
        this.setState({})
    }

    changeCommonSelect(name, index, data, e, option) {
        this.state.dataSource[index][name] = e
        this.setState({})
    }

    //列修改为input框，change事件处理
    handleSearchInput(name, index, e) {
        var { dataSource } = this.state;
        let top = document.getElementById('name-inputs' + index).getBoundingClientRect().top + 40;
        let left = document.getElementById('name-inputs' + index).getBoundingClientRect().left + 'px';
        let windowHeight = window.innerHeight;
        if ((top + 360) > windowHeight) {
            top = top - 330 - 80 + 'px';
        }
        var value = e.target.value;
        //change名称规格的时候，讲关联关系去除
        if (Util.isEmpty(value)) {
            this.state.dataSource[index] = { tabIndex: index + 1 }
        }
        this.state.dataSource[index].ordMedItemDesc = value
        this.setState({
            changeValue: value,
            indexTab: index,
            top: top,
            left: left,
        })
        if (value.length >= 2) {
        } else {
            this.setState({
                selectData: [],
                total: 0,
                page: 1,
            })
        }
    }

    loadSelectData(value, page, pageSize, search) {
        var params = {
            params: [{
                admID: this.state.patientIDs?this.state.patientIDs[0]:'',
                searchCode: value ? value.replace(/^\s*|\s*$/g, '') :
                    (this.state.changeValue ? this.state.changeValue.replace(/^\s*|\s*$/g, '') : ''),
                logonLocID: this.state.userData.locID,
                userID: this.state.userData.userID,
                groupID: this.state.userData.groupID,
                hospID: this.state.userData.hospID,
                judgeFlag: 'Y'
            }],
            pagination: [{
                pageSize: this.state.pageSize,
                currentPage: page
            }]

        }
        $http.post('urlDeault', this, {
            code: "03050019",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.state.selectData = res.result.data.length > 0 ? res.result.data : []
                    this.setState({
                        total: res.result.totalCount,
                        rowMedID: res.result.data ? res.result.data[0].medID : (res.result.data ? res.result.data[0].ordSetID : ''),
                        search: search
                    })
                }
            }
        });
    }

    showCommonTableModal(index, e) {
        if (e.keyCode == '13') {
            this.setState({
                isShowCommonTableModal: true,
                indexTable: index//删除一条空数据，重新添加
            }, () => {
                this.child.showModal();
                this.loadSelectData(this.state.changeValue, '', '')
            })
        }
    }


    changeRadio(e) {
        this.setState({
            radioValue: e.target.value
        }, () => {
            this.loadTemplateData()
        })
    }

    onSearchTemplate(e) {
        if (e.keyCode == '13') {
            this.loadTemplateData()
        }
    }

    checkTreeNode() {

    }

    loadTempTableData() {

    }

    //加载医嘱模板
    loadTemplate() {
        var templateTree = []
        this.state.templateData && this.state.templateData.map((item, index) => {
            templateTree.push(<TreeNode title={item.title} key={index + 'i' + '^'}>
                {this.loadSunTemplate(item.data)}
            </TreeNode>)
        })
        return templateTree
    }

    loadSunTemplate(data) {
        //depDoctorFlag 非本院医生不允许开医嘱
        var sunData = []
        data && data.map((item, index) => {
            // if(item.data) {
            sunData.push(<TreeNode title={item.title} nodeId={item.medID} ordSetID={item.ordSetID}
                disabled={this.state.titleData && this.state.titleData.depDoctorFlag == 'N' ? true :
                    ((item.stock == '0') ? true : false)} stock={item.stock}
                key={item.ordSetID + '-' + item.ordTempletDetailID + '^' + item.ordTempletDetailItemID || item.id || index}>
                {this.loadSunTemplate(item.data)}
            </TreeNode>)
            // }
        })
        return sunData
    }

    //标签点击删除按钮
    handleOnCloseTag(data) {
        var data = []
        var { patientMsg } = this.state;
        for (var i = 0; i < patientMsg.length; i++) {
            if (data.admID !== patientMsg[i].admID) {
                data.push(patientMsg[i])
            }
        }
        this.setState({ patientMsg: data })
    }

    onSelectChange(ids) {
        this.setState({
            ids
        })
    }

    closeModal(name) {
        let { onHide } = this.props;
        onHide && onHide();
    }

    onSubmit() {
        var { patientMsg } = this.state;
        var code = []
        if (Util.isEmpty(patientMsg)) {
            message.error('无患者信息，无法补录医嘱')
            return
        }
        for (var i = 0; i < patientMsg.length; i++) {
            code.push(patientMsg[i].admID)
        }

        var { onSubmit } = this.props;
        onSubmit && onSubmit(code, this.state.dataSource)
    }

    //分页切换
    changePageSize(current, pageSize) {
        this.setState({ pageSize: pageSize, page: current }, () => {
            this.loadSelectData('', current, pageSize, 'search');
        });
    }

    //分页切换
    onChange(e) {
        this.setState({ page: e }, () => {
            this.loadSelectData('', e, '');
        });
    }


    // 操作弹框
    onClickRowPublic = (record, index) => {
        return {
            onClick: () => {
                if (Util.isEmpty(record.medStock) && record.orderType == 'D') {
                    message.error('库存不足，无法添加')
                    return
                }
                this.setState({
                    rowMedID: record.medID || record.ordSetID
                }, () => {
                    this.rowClick(record, index)
                })
            }
        }
    }

    setRowClassNamePublic = (record) => {
        return (record.medID || record.ordSetID) === this.state.rowMedID ? 'clickRowStyle' : '';
    }

    //取消弹框显示，焦点问题
    handleFocus() {
        this.setState({
            selectData: []
        }, () => {
            if (!this.state.focusStatus) {
                this.ordMedItemDesc.current.focus()
            }
        })
    }

    hideModal(name) {
        this.setState({
            [name]: false
        })
    }

    onRef = (ref) => {
        this.child = ref;
    }

    onColumnFeeRef = (ref) => {
        this.feeRef = ref;
    }

    handleOnKeydown(e) {
        var e = window.event || e;
        let scroll_height = 50;
        let id = '';
        let oneArr = this.state.selectData && this.state.selectData.length > 0 ? this.state.selectData : [];
        switch (e.keyCode) {
            case 13:
                if (this.state.rowMedID) {
                    let obj = {}
                    for (var i = 0; i < oneArr.length; i++) {
                        if ((oneArr[i].medID || oneArr[i].ordSetID) === this.state.rowMedID) {
                            obj = i > 0 ? oneArr[i] : oneArr[0];
                            break;
                        }
                    }
                    if (Util.isEmpty(obj)) {
                        message.error('无可选择的医嘱项')
                        return
                    }
                    if (Util.isEmpty(obj.medStock) && obj.orderType == 'D') {
                        message.error('库存不足，无法添加！')
                        return
                    }
                    this.rowClick(obj);
                } else {
                    message.info('请选择一条需要操作的数据')
                }
                break;
            case 38: //上
                if (this.state.rowMedID) {
                    for (var i = 0; i < oneArr.length; i++) {
                        if ((oneArr[i].medID || oneArr[i].ordSetID) === this.state.rowMedID) {
                            id = i > 0 ? (oneArr[i - 1].medID || oneArr[i - 1].ordSetID) : (oneArr[0].medID || oneArr[0].ordSetID);
                            break;
                        }
                    }
                    this.setState({ rowMedID: id }, () => {
                        var gundong = document.getElementById("gundong");
                        gundong.scrollTop = gundong.scrollTop - scroll_height;
                    })
                } else {
                    this.setState({ rowMedID: oneArr[0] ? (oneArr[0].medID || oneArr[0].ordSetID) : '' })
                }
                break;
            case 40: //下
                if (this.state.rowMedID) {
                    for (let i = 0; i < oneArr.length; i++) {
                        if ((oneArr[i].medID || oneArr[i].ordSetID) === this.state.rowMedID) {
                            id = i + 1 >= oneArr.length ? (oneArr[oneArr.length - 1].medID || oneArr[oneArr.length - 1].ordSetID)
                                : (oneArr[i + 1].medID || oneArr[i + 1].ordSetID);
                            break;
                        }
                    }
                    this.setState({ rowMedID: id }, () => {
                        var gundong = document.getElementById("gundong");
                        gundong.scrollTop = gundong.scrollTop + scroll_height;
                    })
                } else {
                    this.setState({ rowMedID: oneArr[0] ? (oneArr[0].medID || oneArr[0].ordSetID) : '' })
                }
                break;
        }
    }

    rowClick = (record, index) => {
        this.setState({
            visible: false,
            rowData: record.medDesc,
            ordSpecID: record.ordSpecID,
            selectData: [],
            focusStatus: true,//焦点标识
            visStatus: false,
        }, () => {
            this.child.handleCancel();
            this.loadTableData('', record, index)
        })
    }

    //鼠标移动添加样式
    addMouseOverStyle(name, e) {
        this.setState({
            addMouseStyle: true,
            indexStyle: name
        })
    }
    addMouseLeaveStyle() {
        this.setState({
            addMouseStyle: false,
            indexStyle: ''
        })
    }

    addMechansim() {
        var { dataSource } = this.state;
        //加入添加限制，只有录入了医嘱信息之后才能再次添加
        for (var i = 0; i < dataSource.length; i++) {
            if (Util.isEmpty(dataSource[i].medID) && Util.isEmpty(dataSource[i].ordMedItemDesc)) {
                message.error('	请先录入医嘱信息再添加！')
                return
            }
        }
        this.state.dataSource.push({ tabIndex: this.state.dataSource.length + 1 });
        this.setState({
            visible: true,
            changeValue: undefined,
            indexTable: this.state.dataSource.length - 1
        }, () => {
            this.ordMedItemDesc.current.focus()
            // this.selectFocus.current.props.onChange(this.state.dataSource.length-1)
        })
    }

    delMechansim() {
        // this.loadDeleteTemplate()
        var flag = true;
        var that = this;
        var { dataSource, ids } = this.state;
        var test = []
        if (ids.length < 1) {
            message.error('请选择您需要删除的数据！')
            return
        }
        for (var i = 0; i < dataSource.length; i++) {
            if (!Util.isEmpty(dataSource[i].ordID)) {
                test.push(dataSource[i])
            }
        }

        confirm({
            title: '删除',
            content: '你确定要删除当前选中的数据吗？',
            okText: that.state.userData.language == 'EN' ? 'Ok' : '确定',
            cancelText: that.state.userData.language == 'EN' ? 'cancel' : '取消',
            onOk() {
                for (var i = 0; i < ids.length; i++) {
                    for (var j = 0; j < dataSource.length; j++) {
                        if (dataSource[j].tabIndex && ids[i] == dataSource[j].tabIndex) {
                            that.state.dataSource.splice(j, 1);
                        }
                    }
                }
                that.setState({ ids: [], changeValue: '' })
            },
            onCancel() {
                console.log('Cancel');
            },
        });
    }
    render() {
        var { patientMsg, ids, top, left, selectData, page, total } = this.state;

        //医嘱录入
        const rowSelection = {
            ids,
            onChange: this.onSelectChange.bind(this),
            selectedRowKeys: ids,
        }

        var moduleDataOption = []
        if (this.state.moduleData) {
            this.state.moduleData && this.state.moduleData.map((item, index) => {
                moduleDataOption.push(
                    <Radio value={item.id} key={item.id}>{item.descripts}</Radio>
                )
            })
        }

        var patientMsgOption = []
        patientMsg && patientMsg.map((item, index) => {
            patientMsgOption.push(<Tag closable key={index} style={{ height: 30, margin: '5px', lineHeight: '30px', fontSize: '14px' }}
                onClose={this.handleOnCloseTag.bind(this, item)} value={item.admID}>
                {<span>
                    <span style={{ color: '#1999db', fontWeight: 'bold' }}>
                        {item.bedNo ? (item.bedNo + '床/') : '--/'}</span>
                    <span>{item.patName} </span>
                </span>}
            </Tag>)
        })

        let param = {
            width: '96vh',
            // height: 300,
            top: top,
            left: left,
            columns: this.sunColumns,
            data: selectData,
            isOnRow: true,
            TableClassName: 'tab-placeholder',
            rowClassName: true,
            showPagination: true,
            page,
            total,
        }

        return <div>
            <div className='doctor-advice-common-modal'
                tabIndex={1}
                onKeyDown={this.handleOnKeydown.bind(this)}
                style={{ outline: 'none' }}
            >

                <ShowCommonTableModal onClickRowPublic={this.onClickRowPublic}
                    setRowClassNamePublic={this.setRowClassNamePublic}
                    isShow={this.state.isShowCommonTableModal}
                    handleFocus={this.handleFocus.bind(this)}
                    onHide={this.hideModal.bind(this, 'isShowCommonTableModal')}
                    param={param}
                    onRef={this.onRef}
                    search={this.state.search}
                    id="gundong"
                    compilePage={this.changePageSize.bind(this)}>
                </ShowCommonTableModal></div>
            <ColumnAuthoritysModel
                clientWidth='800px'
                compontName="addAdviceListPage"
                configType="C"
                onRef={this.onColumnFeeRef}
                reloadData={this.initData}
            />
            <Modal className='collection-modal-body'
                title={'批量录入费用'}
                maskClosable={false}
                visible={this.props.isShow}
                onOk={this.onSubmit.bind(this)}
                onCancel={this.closeModal.bind(this)}>
                <div>
                    <Card style={{ float: 'left', width: '25%', height: '63vh' }} title={<span>
                        <span className='collection-list-left'></span>
                        <span style={{ color: '#1999db', marginLeft: 10 }}>模板</span>
                    </span>}>
                        <div>
                            <Input style={{ width: '90%', marginLeft: '5%' }}
                                prefix={<Icon type="search" style={{ color: 'rgba(0,0,0,.25)' }} />}
                                placeholder={'输入模板/医嘱项名称/别名回车查询'} onChange={Util.commonInputChange.bind(this, 'templateInput')}
                                onKeyDown={this.onSearchTemplate.bind(this)} />
                            <div className='doctor-left-radio-modal'>
                                <Radio.Group value={this.state.radioValue}
                                    onChange={this.changeRadio.bind(this)}>
                                    {moduleDataOption}
                                </Radio.Group>
                            </div>
                            <Tree checkable defaultExpandAll={true} defaultExpandedKeys={['1']} style={{ height: '48vh', overflow: 'auto' }}
                                checkedKeys={this.state.newCode}
                                onCheck={this.checkTreeNode.bind(this)}
                                onDoubleClick={this.loadTempTableData.bind(this, 'temp')}>
                                {this.loadTemplate()}
                            </Tree>
                        </div>
                    </Card>

                    <Card style={{ width: '74.5%', float: 'right', height: '63vh' }}
                        title={<span>
                            <span className='collection-list-left'></span>
                            <span style={{ color: '#1999db', marginLeft: 10 }}>已选择患者</span>
                        </span>}>
                        <div style={{ height: '9vh', overflowY: 'auto', borderBottom: '1px solid #e8e8e8' }}>
                            {patientMsgOption}
                        </div>
                        <div>
                            <div style={{ height: 30, lineHeight: '30px', background: '#F6FCF9' }}>
                                <span className='collection-list-left' style={{ marginTop: 5 }}></span>
                                <span style={{ color: '#1999db', marginLeft: 10 }}>补录费用</span>
                            </div>
                            <div style={{ height: 30, lineHeight: '30px' }}>
                                <div style={{ float: 'left' }}>
                                    <span style={{ cursor: 'pointer' }} className={this.state.indexStyle == '1' ? 'mouse-add-Style' : ''}
                                        onClick={this.addMechansim.bind(this)}
                                        onMouseLeave={this.addMouseLeaveStyle.bind(this)}
                                        onMouseOver={this.addMouseOverStyle.bind(this, '1')}
                                    >
                                        <Icon className='doctor-add-splice' style={{ marginTop: 3 }} />
                                        <span>添加</span>
                                    </span><Divider type="vertical" />
                                    <span style={{ cursor: 'pointer' }} className={this.state.indexStyle == '2' ? 'mouse-add-Style' : ''}
                                        onClick={this.delMechansim.bind(this)}
                                        onMouseLeave={this.addMouseLeaveStyle.bind(this)}
                                        onMouseOver={this.addMouseOverStyle.bind(this, '2')}>
                                        <Icon className='doctor-delete-splice' style={{ marginTop: 3 }} />
                                        <span>删除</span>
                                    </span>
                                </div>
                            </div>
                            <Table columns={this.state.feeColumns} dataSource={this.state.dataSource}
                                style={{ height: '38vh' }}
                                scroll={{ y: '38vh' }}
                                rowSelection={rowSelection}
                                onHeaderRow={column => {
                                    return {
                                        onDoubleClick: () => {
                                            this.feeRef.onOpenModel();
                                        },
                                    };
                                }} bordered
                                rowKey={(data) => {
                                    return data.tabIndex
                                }}
                                pagination={false}></Table>
                        </div>
                    </Card>
                </div>
            </Modal>
        </div>
    }
}

export default ShowCollectionCostModal;