/**
 * 分诊叫号系统
 */

import React from 'react';
import {
    Card, Table, Input, Form, Select, Row, Col, Checkbox, Modal,
    DatePicker, Button, Popconfirm, Icon, Divider, message, Pagination,
} from 'antd';
import { Util, dayFormat } from 'tools/index';
import { $http } from 'containers/config/https';
import { callNumberSocket } from 'tools/callNumber.js';
import moment from 'moment';
import store from 'store';
import '../style/index.less';

const { RangePicker } = DatePicker;
const Search = Input.Search;
const Option = Select.Option;

class TriageSystemModal extends React.Component {

    constructor(props) {
        super(props);
        var date = new Date();
        var userData = JSON.parse(sessionStorage.getItem('userData'));
        var titleData = JSON.parse(sessionStorage.getItem('titleData'))
        var patientData = JSON.parse(sessionStorage.getItem('patientData'))
        var activeKey = JSON.parse(sessionStorage.getItem('activeKey'))
        this.state = {
            dataSource: [],
            total: 0,
            page: 1,
            pageSize: 10,
            size: 'small',
            userData: userData,
            patientData: patientData,
            activeKey: activeKey
        }
    }
    componentWillReceiveProps(nextProp) {
        if (nextProp.isShow === true) {
            this.loadCommonModal()
            this.handleCheckMedical()//校验病历是否填写
        } else if (this.props.isShow === false) {
            // this.cleanInput();
        }
    }

    componentDidMount() {
        this.loadCommonModal()
        this.loadNextPatientMsg()

    }

    handleCheckMedical(){
        var params = {
            params: [{
                admID:this.state.patientData && this.state.patientData .admID
            }]
        }
        $http.post('urlDeault', this, {
            code: "03050177",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        descripts: res.result.descripts,
                        statusCode: res.result.code,
                        statusFlag:res.result.flag,
                    })
                }
            }
        });
    }

    loadNextPatientMsg() {
        var params = {
            params: [{
                locID: this.state.userData.locID,
                userID: this.state.userData.userID,
                hospID: this.state.userData.hospID,
            }]
        }
        $http.post('urlDeault', this, {
            code: "03100074",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        nextPatientMsg: res.result,
                    })
                }
            }
        });
    }

    loadCommonModal() {
        let data = {
            params: [{
                compontName: "DoctorAdviceModal",
                type: "C",
                reactCode: ['DoctorAdviceModal']
            }]
        }
        $http.post('urlDeault', this, {
            code: "01040073",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    this.setState({
                        localeProvider: this.state.userData.language == 'EN' ? res.result.language.EN : res.result.language.CN
                    })
                }
            }
        })
    }

    //国际化
    setMessage(name, message) {
        return this.state.localeProvider && this.state.localeProvider[name] ? this.state.localeProvider[name][message] : ''
    }


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

    handleReturnMsg() {
        this.closeModal()
        var { handleEndOperation } = this.props;
        handleEndOperation && handleEndOperation()
    }

    //视频图文结束就诊
    handleEndOfReceive(status) {
        var params = {
            params: [{
                admID: this.state.patientData.admID,
                userID: this.state.userData.userID,
                hospID: this.state.userData.hospID,
                locID: this.state.userData.locID,
            }]
        }
        $http.post('urlDeault', this, {
            code: "03100061",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    message.success('接诊完成',this.state.userData && this.state.userData.messageTime)
                    if (status == 'next') {
                        this.loadVideoCall()
                    }
                    var { loadPatientPage } = this.props;
                    loadPatientPage && loadPatientPage()
                }
            }
        });
    }

    //视频呼叫
    loadVideoCall() {
        var params = {
            params: [{
                admID: !Util.isEmpty(this.state.nextPatientMsg) ? this.state.nextPatientMsg.admID : '',
                userID: this.state.userData.userID,
                hospID: this.state.userData.hospID,
                locID: this.state.userData.locID,
            }]
        }
        $http.post('urlDeault', this, {
            code: "03100058",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    message.success('呼叫下一位患者成功！',this.state.userData && this.state.userData.messageTime)
                    var { loadPatientPage } = this.props;
                    loadPatientPage && loadPatientPage()
                }
            }
        });
    }

    //关闭图文暂时对话
    handleCloseChatModal() {
        var { handleCloseChatModal } = this.props;
        handleCloseChatModal && handleCloseChatModal()
    }

    //结束图文问诊
    handleCloseChatReceiveModal(){
        var { handleCloseChatReceiveModal } = this.props;
        handleCloseChatReceiveModal && handleCloseChatReceiveModal()
    }

    //暂时关闭视频对话
    handleCloseVideoModal(){
        var {handleCloseVideoModal} = this.props;
        handleCloseVideoModal && handleCloseVideoModal()
    }

    //结束视频就诊
    handleCloseVideoReceiveModal(){
        var {handleCloseVideoReceiveModal} = this.props;
        handleCloseVideoReceiveModal && handleCloseVideoReceiveModal()
    }

    //过号呼叫下一位
    handleEndOfReceiveCallNext(){
        var {handleEndOfReceiveCallNext} = this.props;
        handleEndOfReceiveCallNext && handleEndOfReceiveCallNext(!Util.isEmpty(this.state.nextPatientMsg) ? this.state.nextPatientMsg.roomNo : '')
    }

    render() {
        var activeKey = JSON.parse(sessionStorage.getItem('activeKey'))
        return <div>
            <Modal className='triage-system-modal'
                title={this.setMessage('DoctorAdviceModalafd', 'descripts')}
                maskClosable={false}
                visible={this.props.isShow}
                footer={null}
                onCancel={this.closeModal.bind(this)}>
                {/**activeKey为2时，表示为视频问诊页签、为3时是图文问诊页签、为1时是门诊 */}
                {activeKey == '2' ? <div>
                    <h3 style={{ textAlign: 'center', marginTop: 10 }}>是否完成本次问诊？</h3>
                    <Row style={{ textAlign: 'center', marginTop: 10 }}>
                        <Button onClick={this.closeModal.bind(this)}>{'否>>继续问诊'}</Button>
                        <Button type='primary' style={{ marginLeft: 10 }} onClick={this.handleCloseVideoModal.bind(this)}>暂时关闭对话</Button>
                        <Button type='danger' style={{ marginLeft: 10 }} onClick={this.handleCloseVideoReceiveModal.bind(this)}>{'是>>结束问诊'}</Button>
                        <Button style={{ marginLeft: 10 }} disabled={Util.isEmpty(this.state.nextPatientMsg)}
                            onClick={this.handleEndOfReceiveCallNext.bind(this)}>{'是>>结束并呼叫下一位'}</Button>
                    </Row>

                    <div style={{ padding: '20px 0', textAlign: 'center' }}>
                        <span style={{ color: '#999999' }}>下一位患者：</span>
                        <span>{!Util.isEmpty(this.state.nextPatientMsg) ? this.state.nextPatientMsg.patName : '--'}</span><Divider type='vertical' />
                        <span>{!Util.isEmpty(this.state.nextPatientMsg) ? this.state.nextPatientMsg.patSexDesc : '--'}</span><Divider type='vertical' />
                        <span>{!Util.isEmpty(this.state.nextPatientMsg) ? this.state.nextPatientMsg.patAge : '--'}</span><Divider type='vertical' />
                        <span>{!Util.isEmpty(this.state.nextPatientMsg) ? this.state.nextPatientMsg.patNo : '--'}</span>
                    </div>
                </div> :
                    (activeKey == '3' ? <div>
                        <h3 style={{ textAlign: 'center', marginTop: 10 }}>是否完成本次问诊？</h3>
                        <Row style={{ textAlign: 'center', marginTop: 10 }}>
                            <Button onClick={this.closeModal.bind(this)}>{'否>>继续问诊'}</Button>
                            <Button type='primary' style={{ marginLeft: 10 }} onClick={this.handleCloseChatModal.bind(this)}>暂时关闭对话</Button>
                            <Button type='danger' style={{ marginLeft: 10 }} onClick={this.handleCloseChatReceiveModal.bind(this)}>{'是>>结束问诊'}</Button>
                        </Row>
                    </div> :
                        null)}
            </Modal></div>
    }
}

export default Form.create()(TriageSystemModal);