// 会诊申请
import React, { Component } from 'react';
import { Form, Row, Col, DatePicker, TimePicker, Table, Select, Input, Button, Divider, message } from 'antd';
import FormItem from 'antd/lib/form/FormItem';
import TextArea from 'antd/lib/input/TextArea';
import { $http } from '../../../containers/config/https';
import PatientInfo from '../Patient/PatientInfo';
import Moment from 'moment';
import { Util } from 'tools/index';
import ConsultationCenterModel from './ConsultationCenterModel.jsx';//引用弹框
import { string } from 'prop-types';
const formItemLayout = {    // label 和input宽度
    labelCol: { span: 8 },
    wrapperCol: { span: 16 },
};
const formItemAddLayout = {    // label 和input宽度
    labelCol: { span: 4 },
    wrapperCol: { span: 20 },
};
// 用户数据
let userData;
// 点击编辑时记录当前行数据
let tableRecord = {};
// 创建表单
const ConsultationApply = Form.create()(
    class extends React.Component {
        constructor(props) {
            super(props);
            userData = sessionStorage.userData ? JSON.parse(sessionStorage.userData) : {};
            let patientData = JSON.parse(sessionStorage.getItem('patientData'));
            this.state = {
                conTypeData: [],        //会诊类型下拉数据
                conNatureData: [],      //会诊性质下拉数据
                conUserTypeData: [],    //会诊人员类型下拉数据
                conHospTypeData: [],    //会诊医院类型下拉数据
                cbTitleData: [],        //职称下拉数据
                hospData: [],           //医院下拉数据         
                locData: [],            //会诊科室下拉数据 
                contactUserData: [],    //联系人下拉数据 
                contactTellData: [],    //联系人电话
                conUserData: [],        //会诊人员下拉数据
                conContactTellData: [], //会诊联系人电话
                tableData: [],          //表格数据
                editingKey: '',
                hospID: '',
                clickFlag: false,        //按钮点击标志
                tableLocID: '',          //table科室id
                titleID: '',             //职称id
                tableUserID: '',         //table人员
                consultID: '',
                consultationInfo: {},
                removeConsultLoc: [],
                conStatusCode: '',       //当前会诊申请状态
                conHospTypeDefault: '',  //默认会诊医院类型
                conNatureDefault: '',    //默认会诊性质类型
                conTypeDefault: '',      //默认会诊类型
                disabledAll: (!patientData) || (patientData.admID === '') ? true : false,      //全局禁用
                locType: '',              //可是类型
                patientData: patientData,
                conContactFlag: false,   //是否修改会诊联系人
                visible: false,
                parData:"",              // 与引用界面数据互通
                quoteCode: '',           // 引用code
            };
            this.columns = [{
                key: 1,
                title: '科室',
                dataIndex: 'loc',
                align: "center",
                width: '30%',
                render: (text, record, index) => {
                    const editable = this.isEditing(record);
                    let selectedLocs = [];       //已添加科室
                    const tableData = this.state.tableData;
                    for (let i = 0; i < tableData.length; i++) {
                        if ((tableData[i] !== undefined) && (tableData[i].locID !== '')) {
                            selectedLocs.push(tableData[i].locID)
                        }
                    }
                    return (
                        <Select
                            value={record.locID}
                            allowClear
                            style={{ width: "100%", textAlign: "center" }}
                            onChange={this.handleFieldChange.bind(this, index, 'locID')}
                            disabled={editable ? false : true}
                            showSearch
                            optionFilterProp='role'
                        >
                            {this.state.locData.map(data => (
                                <Select.Option
                                    role={data.code + '^' + data.descripts + '^' + data.descriptsSPCode}
                                    key={data.id}
                                    disabled={selectedLocs.includes(data.id) || (data.id === userData.locID)}
                                >{data.descripts}
                                </Select.Option>
                            )
                            )}
                        </Select>
                    )
                }
            }, {
                key: 2,
                title: '职称',
                dataIndex: 'title',
                align: "center",
                width: '15%',
                render: (text, record, index) => {
                    const editable = this.isEditing(record);
                    return (
                        <Select
                            value={record.conItemRequireTitleID}
                            allowClear
                            style={{ width: "100%", textAlign: "center" }}
                            onChange={this.handleFieldChange.bind(this, index, 'conItemRequireTitleID')}
                            disabled={editable ? false : true}
                            showSearch
                            optionFilterProp='role'
                        >
                            {this.state.cbTitleData.map(data => (
                                <Select.Option
                                    role={data.code + '^' + data.descripts + '^' + data.descriptsSPCode}
                                    key={data.id}
                                >
                                    {data.descripts}
                                </Select.Option>))}
                        </Select>
                    )
                }
            }, {
                key: 3,
                title: '会诊人员',
                dataIndex: 'user',
                align: "center",
                width: '15%',
                render: (text, record, index) => {
                    const editable = this.isEditing(record);
                    return (
                        <Select
                            value={record.conItemRequireUserID}
                            allowClear
                            style={{ width: "100%", textAlign: "center" }}
                            onChange={this.handleFieldChange.bind(this, index, 'conItemRequireUserID')}
                            disabled={editable ? false : true}
                            showSearch
                            optionFilterProp='role'
                        >
                            {this.state.conUserData.map(data => (
                                <Select.Option
                                    role={data.code + '^' + data.descripts + '^' + data.descriptsSPCode}
                                    key={data.id}
                                    conusertel={data.userContactAry}
                                >
                                    {data.descripts}
                                </Select.Option>))}
                        </Select>
                    )
                }
            }, {
                key: 4,
                title: '联系电话',
                dataIndex: 'telephone',
                align: "center",
                width: '20%',
                render: (text, record, index) => {
                    const editable = this.isEditing(record);
                    return (
                        (this.state.conContactTellData.length > 0) ?
                            <Select
                                value={record.conItemRequireContact}
                                allowClear
                                style={{ width: "100%", textAlign: "center" }}
                                onChange={this.handleFieldChange.bind(this, index, 'conItemRequireContact')}
                                disabled={editable ? false : true}
                            >
                                {this.state.conContactTellData.map(data => (
                                    <Select.Option
                                        key={data.userContactID}
                                        value={data.userContactValue}
                                    >
                                        {data.userContactValue}
                                    </Select.Option>
                                ))}
                            </Select> :
                            <Input
                                value={record.conItemRequireContact}
                                style={{ width: "100%", textAlign: "center" }}
                                onChange={this.handleFieldChange.bind(this, index, 'conItemRequireContact')}
                                disabled={editable ? false : true}
                            />
                    )
                }
            }, {
                key: 5,
                title: '操作',
                dataIndex: 'operate',
                align: "center",
                render: (text, record) => {
                    const { editingKey } = this.state;
                    const editable = this.isEditing(record);
                    return editable ? (
                        <span>
                            <a onClick={(e) => this.handleUpdate(record, e)}>确认</a>
                            <Divider type="vertical" />
                            <a onClick={(e) => this.handleCancel(record, e)}>取消</a>
                        </span>
                    ) : (
                            <span>
                                <a disabled={(editingKey !== '') ? true : this.state.disabledAll} onClick={(e) => this.handleEdit(record, e)}>编辑</a>
                                <Divider type="vertical" />
                                <a style={((this.state.disabledAll) || (editingKey !== '')) ? null : { color: 'rgba(240,65,52,1)' }} disabled={(editingKey !== '') ? true : this.state.disabledAll} onClick={(e) => this.handleDelete(record, e)}>删除</a>
                            </span>
                        );
                }
            }]

        }
        componentWillMount() {
            this.getLocTypeByUserType();
        }
        componentDidMount() {
            this.getHospData();
            this.getSelectData();
            this.getLocUserData();
            this.getLocData();
            if ((!this.state.patientData) || (this.state.patientData.admID == '')) {
                message.warning('请选择患者');
            }
            // this.getConsultationInfo();
        }
        // 确认会诊科室
        handleUpdate = (record, e) => {
            e.preventDefault();
            // 未选择科室时提示
            if (record.locID === '') {
                message.warning('请选择会诊科室');
                return;
            }
            this.determineConType();
            this.setState({
                editingKey: '',
                conContactTellData: []
            });
        }
        // 根据table里的科室数量确定会诊类型
        determineConType = () => {
            let tempObj = {};
            let { conTypeData } = this.state;
            for (let i = 0; i < conTypeData.length; i++) {
                tempObj[conTypeData[i]['code']] = conTypeData[i]['id']
            }
            // 过滤空数据
            let tableData = this.state.tableData;            
            if (tableData && tableData.length > 1) {
                this.props.form.setFieldsValue({ conTypeID: tempObj.MDC })
            } else {
                this.props.form.setFieldsValue({ conTypeID: tempObj.CDC })
            }
        }
        // 删除会诊科室
        handleDelete = (record, e) => {
            e.preventDefault();
            let { tableData, removeConsultLoc } = this.state;
            tableData.splice(record.key,1);
            if (record.conItemID !== '') {
                removeConsultLoc.push(record.conItemID);
            }
            // 重新给key赋值
            for (let i=0;i<tableData.length;i++)
            {
                tableData[i].key = i;
            }
            this.setState({
                tableData,
                removeConsultLoc
            }, () => this.determineConType())
        }
        // 编辑会诊科室
        handleEdit = (record, e) => {
            e.preventDefault();
            tableRecord = JSON.parse(JSON.stringify(record));
            this.getLocUserTableData(record.locID, record.conItemRequireTitleID);
            this.setState({
                editingKey: record.key
            });
        }
        // 取消编辑
        handleCancel = (record, e) => {
            e.preventDefault();
            let tableData = this.state.tableData;
            tableData[record.key] = tableRecord;
            if (record.conItemID !== '') {
                this.setState({
                    editingKey: '',
                    conContactTellData: [],
                    tableData
                }, () => {
                    this.getLocUserTableData(tableRecord.locID, tableRecord.conItemRequireTitleID);
                });
            } else {
                this.handleDelete(record, e);
                this.setState({
                    editingKey: ''
                });
            }
        }
        isEditing = (record) => {
            return record.key === this.state.editingKey
        };
        // 表格下拉框选中
        handleFieldChange = (index, code, value, opt) => {
            let { tableData, editingKey } = this.state;
            if (value !== undefined) {
                if ((typeof (value) !== 'string') && (value.target.value) && (/[^0-9-]/g.test(value.target.value))) {
                    message.error('请输入正确的联系电话')
                }
                tableData[editingKey][code] = (typeof (value) == 'string') ? value : value.target.value;
                if ((code === 'conItemRequireUserID') && (opt.props.conusertel.length > 0)) {
                    tableData[editingKey].conItemRequireContact = opt.props.conusertel[0].userContactValue
                }
                this.setState({ tableData });
                if ((code == 'locID') && (value !== '')) {
                    this.setState({
                        tableLocID: value
                    }, () =>
                        this.getLocUserTableData(this.state.tableLocID, this.state.titleID)
                    );
                }
                if ((code == 'conItemRequireTitleID') && (value !== '')) {
                    this.setState({ titleID: value },
                        () => {
                            if (this.state.tableLocID !== '') {
                                this.getLocUserTableData(this.state.tableLocID, this.state.titleID);
                            }
                        });
                }
                if (code == 'conItemRequireUserID') {
                    if (opt.props.conusertel.length > 0) {
                        this.setState({ conContactTellData: opt.props.conusertel });
                    } else {
                        this.setState({ conContactTellData: [] });
                    }
                }
            } else {
                tableData[editingKey][code] = '';
                if (code == 'locID') {
                    tableData[editingKey]['conItemRequireTitleID'] = '';
                    tableData[editingKey]['conItemRequireUserID'] = '';
                    tableData[editingKey]['conItemRequireContact'] = '';
                    this.setState({
                        tableData,
                        conUserData: []
                    });
                }
                if (code == 'conItemRequireTitleID') {
                    this.setState({ tableData });
                }
                if (code == 'conItemRequireUserID') {
                    tableData[editingKey]['conItemRequireContact'] = '';
                    this.setState({
                        tableData,
                        conContactTellData: []
                    });
                }
                if (code == 'conItemRequireContact') {
                    this.setState({ tableData });
                }
            }
        }
        // 获取下拉框数据
        getSelectData = () => {
            let data = {
                "params": [{
                    hospID: userData && userData.hospID ? userData.hospID : '', // 登录医院id       
                    locID: userData.locID,
                    groupID: userData.groupID,
                    userID: userData.userID,
                    type: 'apply'
                }]
            };
            $http.post('urlDeault', this, {
                code: "04090011",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({
                            conTypeData: res.result.conType,
                            conNatureData: res.result.conNature,
                            conUserTypeData: res.result.conUserType,
                            cbTitleData: res.result.title,
                            conHospTypeData: res.result.conHospType,
                            conHospTypeDefault: res.result.conHospTypeDefault,  //默认会诊医院类型
                            conNatureDefault: res.result.conNatureDefault,    //默认会诊性质类型
                            conTypeDefault: res.result.conTypeDefault,      //默认会诊类型
                        }, () => this.props.form.setFieldsValue({ conRequirePlace: res.result.conAddress }))
                    }
                },
                error: function (error) {
                    message.error('失败：' + error);
                }
            });
        }
        // 获取医院信息(获取所有医院)
        getHospData = () => {
            let data = {
                "params": [{}]
            };
            $http.post('urlDeault', this, {
                code: "01040078",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({
                            hospData: res.data
                        })
                    }
                },
                error: function (error) {
                    message.error('失败：' + error);
                }
            });
        }
        // 获取科室信息
        getLocData = () => {
            let data = {
                "params": [{
                    hospID: this.state.hospID ? this.state.hospID : userData.hospID,          // 会诊医院id
                    type: this.state.locType
                }]
            };
            $http.post('urlDeault', this, {
                code: "04020008",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({
                            locData: res.result.Data
                        })
                    }
                },
                error: function (error) {
                    message.error('失败：' + error);
                }
            });
        }
        // 获取申请科室人员
        getLocUserData = () => {
            let data = {
                "params": [{
                    hospID: userData && userData.hospID ? userData.hospID : '', // 登录医院id
                    locID: userData && userData.locID ? userData.locID : '', // 登录科室id
                    power: 'select',
                    deptEffective: 'Y',
                }]
            };
            $http.post('urlDeault', this, {
                code: "01040021",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({
                            contactUserData: res.result.Data
                        })
                    }
                },
                error: function (error) {
                    message.error('失败：' + error);
                }
            });
        }
        // 获取表格科室人员
        getLocUserTableData = (locID, title) => {
            this.setState({
                conUserData: []
            })
            let data = {
                "params": [{
                    hospID: this.state.hospID ? this.state.hospID : userData.hospID,
                    locID,
                    title,
                    power: 'select',
                    deptEffective: 'Y',
                }]
            };
            $http.post('urlDeault', this, {
                code: "01040021",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({
                            conUserData: res.result.Data
                        })
                    }
                },
                error: function (error) {
                    message.error('失败：' + error);
                }
            });
        }
        // 获取申请会诊信息
        getConsultationInfo = () => {
            if (this.state.consultID == '') return
            let data = {
                "params": [{
                    consultID: this.state.consultID,
                    type: 'apply'
                }]
            };
            $http.post('urlDeault', this, {
                code: "04090014",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        let dataInfo = res.Data[0]
                        let conLocInfo = dataInfo.consultLoc
                        this.setState({
                            consultationInfo: dataInfo,
                            tableData: dataInfo.consultLoc,
                            hospID: dataInfo.conHospID,
                        }, () => {
                            this.getLocData();
                            for (let i = 0; i < conLocInfo.length; i++) {
                                this.getLocUserTableData(conLocInfo[i].locID, conLocInfo[i].conItemRequireTitleID);
                            }
                        })
                    }
                },
                error: function (error) {
                    message.error('失败：' + error);
                }
            });
        }
        // 添加行
        addRow = () => {
            const { tableData } = this.state;
            // if ((index > 0) && (tableData[index - 1]) && (tableData[index - 1]['locID'] === '')) {
            //     message.warning('请先选择上一条记录的科室');
            //     return;
            // }
            this.determineConType();
            let newData = tableData;
            newData.push({
                key: tableData.length,
                conItemID: '',
                conItemRequireTitleID: '',
                locID: '',
                conItemRequireUserID: '',
                conItemRequireContact: '',
                editable: true
            });
            this.setState({
                tableData: newData,
                editingKey: newData.length-1
            });
        }
        // 保存
        save = (code) => {
            this.setState({
                clickFlag: true,
                editingKey: '',
                disabledAll: (code === 'SE') ? true : false
            });
            this.props.form.validateFields((err, value) => {
                if (err) {
                    this.setState({
                        clickFlag: false,
                        disabledAll: false
                    });
                }else{
                    const formData = this.props.form.getFieldsValue();
                    // 验证相关下拉框数据是否匹配            
                    // const conTypeCode = this.getCodeByID(formData.conTypeID, this.state.conTypeData)  // 会诊类型的code
                    // const conHospTypeCode = this.getCodeByID(formData.conHospTypeID, this.state.conHospTypeData)  // 会诊医院类型的code
                    // if (((conTypeCode === 'IHC') && (conHospTypeCode !== 'EH')) || ((conTypeCode !== 'IHC') && (conHospTypeCode === 'EH'))) {
                    //     this.setState({
                    //         clickFlag: false,
                    //         disabledAll: false
                    //     }, () => message.error('会诊类型与医院类型不匹配'));
                    //     return;
                    // }
                    // 过滤科室为空的table数据
                    // for (let i = 0; i < this.state.tableData.length; i++) {
                    //     if ((this.state.tableData[i] !== undefined) && (this.state.tableData[i].locID == '')) {
                    //         delete this.state.tableData[i];
                    //     }
                    // }
                    const consultLoc = this.state.tableData.filter(item => item);
                    // if (consultLoc.length === 0) {
                    //     this.setState({
                    //         clickFlag: false,
                    //         disabledAll: false
                    //     }, () => message.error('请添加会诊科室'));
                    //     return;
                    // }
                    // // 多科会诊验证会诊科室不得少于1
                    // if (conTypeCode === 'MDC' && consultLoc.length === 1) {
                    //     this.setState({
                    //         clickFlag: false,
                    //         disabledAll: false
                    //     }, () => message.error('该会诊为多科会诊，请添加会诊科室'));
                    //     return;
                    // }
        
                    let data = {
                        "params": [{
                            consultID: this.props.consultID ? this.props.consultID : this.state.consultID,
                            conHospID: formData.conHospID,
                            hospID: userData.hospID,
                            conDictItemCode: code,
                            admID: !Util.isEmpty(this.state.patientData.admID) ? this.state.patientData.admID : '',
                            conTypeID: formData.conTypeID,
                            conNatureID: formData.conNatureID,
                            conUserTypeID: formData.conUserTypeID,
                            conHospTypeID: formData.conHospTypeID,
                            conApplyLocID: userData.locID,
                            conAppuserID: userData.userID,
                            conPatCondition: formData.conPatCondition,
                            conAims: formData.conAims,
                            conContactUserID: formData.conContactUserID ? formData.conContactUserID : '',
                            conContactTell: formData.conContactTell ? formData.conContactTell : '',
                            conRequireDate: formData.conRequireDate ? Moment(formData.conRequireDate).format('YYYY-MM-DD') : null,
                            conRequireTime: formData.conRequireTime ? Moment(formData.conRequireTime).format('HH:mm:ss') : null,
                            conRequirePlace: formData.conRequirePlace ? formData.conRequirePlace : '',
                            updateUserID: userData.userID,
                            consultLoc: consultLoc,
                            removeConsultLoc: this.state.removeConsultLoc
                        }]
                    };
                    $http.post('urlADS', this, {
                        code: "04090010",
                        data: data,
                        success: function (res) {
                            if (+res.errorCode == '0') {
                                message.success(res.errorMessage);
                                this.determineConType();
                                this.setState({
                                    clickFlag: false,
                                    consultID: res.consultID,
                                    conStatusCode: res.conStatusCode
                                }, () => {
                                    this.getConsultationInfo();
                                });
                            } else {
                                this.setState({ clickFlag: false, disabledAll: false })
                            }
                        },
                        error: function (error) {
                            message.error('失败：' + error);
                            this.setState({ clickFlag: false, disabledAll: false })
                        }
                    });
                }
            });            
        }
        // 取消发送
        cancel = () => {
            this.setState({
                clickFlag: true,
                editingKey: ''
            });
            const formData = this.props.form.getFieldsValue()
            let data = {
                "params": [{
                    conItemID: this.state.consultID,
                    updateUserID: userData.userID,
                    conHospID: formData.conHospID,
                    hospID: userData.hospID,
                    locID: userData.locID,
                    code: "CS"
                }]
            };
            $http.post('urlADS', this, {
                code: "04090016",
                data: data,
                success: function (res) {
                    if (+res.errorCode == '0') {
                        message.success(res.errorMessage);
                        this.setState({
                            clickFlag: false,
                            consultID: res.consultID,
                            conStatusCode: res.conStatusCode,
                            disabledAll: false
                        });
                    } else {
                        message.error('失败：' + res.errorMessage);
                        this.setState({ clickFlag: false })
                    }
                },
                error: function (error) {
                    message.error('失败：' + error);
                    this.setState({ clickFlag: false })
                }
            });
        }
        // 选择会诊类型时默认医院类型
        setHospType = (id, opt) => {
            if ((opt !== undefined) && (opt.props.code == 'IHC')) {
                this.props.form.setFieldsValue({ conHospTypeID: this.getIDByCode('EH', this.state.conHospTypeData), conHospID: '' });
            } else {
                this.props.form.setFieldsValue({ conHospTypeID: this.getIDByCode('OH', this.state.conHospTypeData), conHospID: userData.hospID });
            }
        }
        // 选择医院类型时默认会诊类型
        setConType = (id, opt) => {
            switch (opt.props.code) {
                case 'EH':
                    this.props.form.setFieldsValue({ conTypeID: this.getIDByCode('IHC', this.state.conTypeData), conHospID: '' }, () => this.setState({ locData: [] }));
                    break;
                case 'OH':
                    this.props.form.setFieldsValue({ conHospID: userData.hospID }, () => this.setState({ hospID: '' }, () => this.getLocData()));
                    break;
            }
        }
        hospOnChange = (id, opt) => {
            this.setState({
                hospID: id,
                tableData: [],
            }, () => {
                const { conHospTypeData } = this.state;
                if ((opt.props.title) && (opt.props.title === userData.hospID)) {
                    // 子院                                                        
                    // 循环找医院类型id
                    for (let i = 0; i < conHospTypeData.length; i++) {
                        if (conHospTypeData[i]['code'] === 'SH') {
                            this.props.form.setFieldsValue({ conHospTypeID: conHospTypeData[i]['id'] });
                        }
                    }
                } else if (id === userData.hospID) {
                    // 本院
                    for (let i = 0; i < conHospTypeData.length; i++) {
                        if (conHospTypeData[i]['code'] === 'OH') {
                            this.props.form.setFieldsValue({ conHospTypeID: conHospTypeData[i]['id'] });
                        }
                    }
                } else {
                    // 外院
                    for (let i = 0; i < conHospTypeData.length; i++) {
                        if (conHospTypeData[i]['code'] === 'EH') {
                            this.props.form.setFieldsValue({ conHospTypeID: conHospTypeData[i]['id'] });
                        }
                    }
                }
                this.getLocData();
            })
        }
        // 通过id获取对应code
        getCodeByID = (id, dataSource) => {
            for (let i = 0; i < dataSource.length; i++) {
                if (dataSource[i].id == id) {
                    return dataSource[i].code
                }
            }
        }
        // 通过code获取对应id
        getIDByCode = (code, dataSource) => {
            for (let i = 0; i < dataSource.length; i++) {
                if (dataSource[i].code == code) {
                    return dataSource[i].id
                }
            }
        }
        // 判断当前登录人员类型
        tellUserType = () => {
            if (userData.groupDesc.indexOf('护') > -1) {
                return 'Nur'
            }
            if (userData.groupDesc.indexOf('医') > -1) {
                return 'Doc'
            }
        }
        // 根据登录人员确定科室类型
        getLocTypeByUserType = () => {
            if (userData.groupDesc.indexOf('护') > -1) {
                this.setState({ locType: 'W' })
            }
            if (userData.groupDesc.indexOf('医') > -1) {
                this.setState({ locType: 'O^OI^I' })
            }
        }
        // 调用公共组件的连接事件
        onRefQuote = (ref) => {
            this.QuoteChild = ref
        }
        onSearch(e) {
            this.setState({
                changeValue: e
            })
        }

        //引用结论数据
        addData = (data) => {
            const quoteCode = this.state.quoteCode
            this.props.form.setFieldsValue({
                [quoteCode]: data,

            })
        }
        openModel = (code) => {
            let parData = (code == 'conPatCondition') ? this.props.form.getFieldValue('conPatCondition') : this.props.form.getFieldValue('conAims');
            this.setState({
                visible: true,
                parData,
                quoteCode: code
            })
        }
        render() {
            const { getFieldDecorator } = this.props.form;
            const consultationInfo = this.state.consultationInfo
            const conContactTell = !Util.isEmpty(this.state.conContactTell) ? this.state.conContactTell : (this.state.conContactFlag ? '' : consultationInfo.conContactTell)
            return (
                <div className="WarehouseEntryCheck1">
                    <Row style={{ width: '100%', height: '45px', textAlign: 'center', lineHeight: '45px', borderBottom: '1px solid #cccccc' }}>
                        <PatientInfo admID={(this.state.patientData) && (!Util.isEmpty(this.state.patientData.admID)) ? this.state.patientData.admID : ''} />
                    </Row>
                    <Form>
                        <h1 style={{ background: "rgba(229,246,237,1)", fontSize: '20px' }} align='center'>会诊申请</h1>
                        <Row type="flex" justify="center">
                            <Col span={5}>
                                <FormItem {...formItemLayout} label="会诊类型">
                                    {getFieldDecorator('conTypeID', {
                                        initialValue: consultationInfo.conTypeID ? consultationInfo.conTypeID : this.state.conTypeDefault,
                                        rules: [{ required: true, message: '请选择会诊类型' }]
                                    })(
                                        <Select allowClear
                                            showSearch
                                            optionFilterProp='role'
                                            // onChange={(id, opt) => { this.setHospType(id, opt) }}
                                            disabled={true}
                                        >
                                            {this.state.conTypeData.map(data => (<Select.Option role={data.code + '^' + data.descripts + '^' + data.descriptsSPCode} key={data.id} code={data.code}>{data.descripts}</Select.Option>
                                            ))}
                                        </Select>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5}>
                                <FormItem {...formItemLayout} label="会诊性质">
                                    {getFieldDecorator('conNatureID', {
                                        initialValue: consultationInfo.conNatureID ? consultationInfo.conNatureID : this.state.conNatureDefault,
                                        rules: [{ required: true, message: '请选择会诊性质' }]
                                    })(
                                        <Select allowClear
                                            showSearch
                                            optionFilterProp='role'
                                            disabled={this.state.disabledAll}
                                        >
                                            {this.state.conNatureData.map(data => (
                                                <Select.Option role={data.descripts + '^' + data.descriptsSPCode} key={data.id}>{data.descripts}</Select.Option>
                                            ))}
                                        </Select>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5}>
                                <FormItem {...formItemLayout} label="会诊人员类型">
                                    {getFieldDecorator('conUserTypeID', {
                                        initialValue: consultationInfo.conUserTypeID ? consultationInfo.conUserTypeID : this.getIDByCode(this.tellUserType(), this.state.conUserTypeData),
                                        rules: [{ required: true, message: '请选择会诊人员类型' }]
                                    })(
                                        <Select allowClear
                                            showSearch
                                            optionFilterProp='role'
                                            disabled={this.state.disabledAll}
                                        >
                                            {this.state.conUserTypeData.map(data => (
                                                <Select.Option role={data.descripts + '^' + data.descriptsSPCode} key={data.id}>{data.descripts}</Select.Option>
                                            ))}
                                        </Select>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5}>
                                <FormItem {...formItemLayout} label="申请人">
                                    <Input disabled value={userData.userName} />
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="center">
                            <Col span={5}>
                                <FormItem {...formItemLayout} label="会诊医院类型">
                                    {getFieldDecorator('conHospTypeID', {
                                        initialValue: consultationInfo.conHospTypeID ? consultationInfo.conHospTypeID : this.state.conHospTypeDefault,
                                        rules: [{ required: true, message: '请选择会诊医院类型' }]
                                    })(
                                        <Select allowClear
                                            showSearch
                                            optionFilterProp='role'
                                            // onSelect={(id, opt) => { this.setConType(id, opt) }}
                                            disabled={true}
                                        >
                                            {this.state.conHospTypeData.map(data => (
                                                <Select.Option role={data.descripts + '^' + data.descriptsSPCode} key={data.id} code={data.code}>{data.descripts}</Select.Option>
                                            ))}
                                        </Select>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5}>
                                <FormItem {...formItemLayout} label="会诊医院">
                                    {getFieldDecorator('conHospID', {
                                        initialValue: consultationInfo.conHospID ? consultationInfo.conHospID : userData.hospID,
                                        rules: [{ required: true, message: '请选择会诊医院' }]
                                    })(
                                        <Select
                                            // allowClear
                                            showSearch
                                            optionFilterProp='role'
                                            disabled={this.state.disabledAll}
                                            onSelect={this.hospOnChange}
                                        >
                                            {this.state.hospData.map(data => (
                                                <Select.Option
                                                    role={data.descripts + '^' + data.descriptsSPCode}
                                                    title={data.hospDr}
                                                    key={data.id}
                                                    disabled={this.state.disabledAll}
                                                >
                                                    {data.descripts}
                                                </Select.Option>
                                            ))}
                                        </Select>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5}>
                                <FormItem {...formItemLayout} label="联系人">
                                    {getFieldDecorator('conContactUserID', {
                                        initialValue: consultationInfo.conContactUserID ? consultationInfo.conContactUserID : "",
                                    })(
                                        <Select allowClear
                                            showSearch
                                            optionFilterProp='role'
                                            disabled={this.state.disabledAll}
                                            value={Util.isEmpty(this.state.conContactUserID) ? '' : this.state.conContactUserID}
                                            onChange={(value, option) => {
                                                if (value === undefined) {
                                                    this.setState({
                                                        conContactUserID: '',
                                                        conContactFlag: true,
                                                        contactTellData: [],
                                                        conContactTell: ''
                                                    })
                                                } else {
                                                    this.setState({
                                                        conContactUserID: value,
                                                        conContactFlag: true,
                                                        contactTellData: option.props.contacttell,
                                                        conContactTell: option.props.contacttell[0].userContactValue
                                                    })
                                                }
                                            }}
                                        >
                                            {this.state.contactUserData.map(data => (
                                                <Select.Option role={data.descripts + '^' + data.descriptsSPCode} key={data.id} contacttell={data.userContactAry}>{data.descripts}</Select.Option>
                                            ))}
                                        </Select>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5}>
                                <FormItem {...formItemLayout} label="联系电话">
                                    {getFieldDecorator('conContactTell', {
                                        initialValue: this.state.changeValue ? this.state.changeValue : conContactTell,
                                        // getValueFromEvent: (event) => {
                                        //     if (typeof(event) === 'string'){
                                        //         return event                                          
                                        //     }else{
                                        //         return event.target.value.replace(/[^0-9-]/g, '')
                                        //     }
                                        // },
                                    })(
                                        this.state.contactTellData.length ?
                                            <Select allowClear
                                                showSearch
                                                optionFilterProp='value'
                                                onSearch={this.onSearch.bind(this)}
                                                disabled={this.state.disabledAll}
                                                // value={this.state.changeValue?this.state.changeValue:conContactTell}
                                                onChange={(value) => {
                                                    this.setState({
                                                        conContactTell: value,
                                                        conContactFlag: false,
                                                    })
                                                }}
                                            >
                                                {this.state.contactTellData.map(data => (
                                                    <Select.Option key={data.userContactID} value={data.userContactValue}>{data.userContactValue}</Select.Option>
                                                ))}
                                            </Select> :
                                            <Input disabled={this.state.disabledAll} />
                                    )}
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="center">
                            <Col span={5}>
                                <FormItem {...formItemLayout} label="会诊日期">
                                    {getFieldDecorator('conRequireDate', {
                                        initialValue: consultationInfo.conRequireDate ? Moment(consultationInfo.conRequireDate, 'YYYY-MM-DD') : Moment(Moment(), 'YYYY-MM-DD')
                                    })(
                                        <DatePicker style={{ width: '100%' }}
                                            // disabledDate={(current)=>{
                                            //     return current && current < Moment().endOf('day')
                                            // }} 
                                            disabled={this.state.disabledAll}></DatePicker>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={5}>
                                <FormItem {...formItemLayout} label="会诊时间">
                                    {getFieldDecorator('conRequireTime', {
                                        initialValue: consultationInfo.conRequireTime ? Moment(consultationInfo.conRequireTime, 'HH:mm:ss') : Moment(Moment(), 'HH:mm:ss'),
                                        rules: [{ required: true, message: '请选择会诊时间' }]
                                    })(
                                        <TimePicker style={{ width: '100%' }} disabled={this.state.disabledAll}></TimePicker>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={10}>
                                <FormItem {...formItemAddLayout} label="会诊地址">
                                    {getFieldDecorator('conRequirePlace', {
                                        initialValue: consultationInfo.conRequirePlace ? consultationInfo.conRequirePlace : '',
                                    })(
                                        <Input disabled={this.state.disabledAll} />
                                    )}
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="center">
                            <Col span={18}>
                            <span style={{color:'red'}}>*</span>&nbsp;
                            <span>病情摘要</span>&nbsp;&nbsp;
                            <Button type="primary"  size="small" disabled={this.state.disabledAll} onClick={this.openModel.bind(this,'conPatCondition')}>引用</Button>&nbsp;&nbsp;&nbsp;&nbsp;
                                    <br />
                                <FormItem label="">
                                    {getFieldDecorator('conPatCondition', {
                                        initialValue: consultationInfo.conPatCondition ? consultationInfo.conPatCondition : '',
                                        rules: [{ required: true, message: '请填写患者病情' }]
                                    })(
                                        <TextArea  style={{ marginTop: '10px' }} autosize={{ minRows: 2, maxRows: 6 }} disabled={this.state.disabledAll} />
                                    )}
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="center">
                            <Col span={18}>
                            <span style={{color:'red'}}>*</span>&nbsp;
                            <span>会诊理由及要求</span>&nbsp;&nbsp;
                            <Button type="primary" size="small" disabled={this.state.disabledAll} onClick={this.openModel.bind(this,'conAims')}>引用</Button>&nbsp;&nbsp;&nbsp;&nbsp;
                                          <br />
                                <FormItem label="">
                                    {getFieldDecorator('conAims', {
                                        initialValue: consultationInfo.conAims ? consultationInfo.conAims : '',
                                        rules: [{ required: true, message: '请填写会诊目的' }]
                                    })(
                                        <TextArea  style={{ marginTop: '10px' }}  autosize={{ minRows: 2, maxRows: 6 }} disabled={this.state.disabledAll} />
                                    )}
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="center">
                            <Col span={18}>
                                <Divider orientation="left" style={{ marginTop: '5px', marginBottom: '5px' }}>
                                    会诊科室&nbsp;&nbsp;&nbsp;
                                    <Button
                                        type='primary'
                                        size='small'
                                        onClick={this.addRow}
                                        icon='plus'
                                        disabled={this.state.disabledAll}
                                    >添加科室
                                    </Button>
                                </Divider>
                                <Table
                                    style={{ minHeight: '228px' }}
                                    columns={this.columns}
                                    dataSource={this.state.tableData}
                                    pagination={false}
                                    bordered
                                />
                            </Col>
                        </Row>
                        <Row type="flex" justify="center">
                            <Col span={2}>
                                <FormItem>
                                    <Button
                                        type='primary'
                                        htmlType="submit"
                                        onClick={this.save.bind(this, 'SA')}
                                        icon='save'
                                        disabled={this.state.clickFlag ? true : this.state.disabledAll}
                                    >保存
                                    </Button>
                                </FormItem>
                            </Col>
                            <Col span={2}>
                                <FormItem>
                                    <Button
                                        type='primary'
                                        htmlType="submit"
                                        onClick={this.save.bind(this, 'SE')}
                                        disabled={this.state.clickFlag ? true : this.state.disabledAll}
                                    >发送
                                    </Button>
                                </FormItem>
                            </Col>
                            {/* <Col span={2}>
                                <FormItem>
                                    <Button 
                                        type='primary'
                                        onClick={this.cancel}
                                        disabled={this.state.clickFlag?true:(!this.state.disabledAll)}
                                    >取消发送
                                    </Button>  
                                </FormItem>
                            </Col> */}
                        </Row>
                    </Form>
                    <ConsultationCenterModel
                        visible={this.state.visible}
                        parData={this.state.parData}
                        handleOk={() => this.setState({ visible: false })}
                        handthis={this}
                        onRef={this.onRefQuote}
                        admID={!Util.isEmpty(this.state.patientData) ? this.state.patientData.admID : ''}
                    />
                </div>
            )
        }
    }
)
export default ConsultationApply;