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

import React from 'react';
import {
    Card, Table, Input, Form, Select, Row, Col, Checkbox, Modal,
    DatePicker, Button, Popconfirm, Icon, Divider, message, Pagination,
    Spin
} 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 patientData = JSON.parse(sessionStorage.getItem('patientData'))
        var newDate = date.setMonth(date.getMonth() - 1);
        this.state = {
            dataSource: [],
            total: 0,
            page: 1,
            pageSize: 10,
            size: 'small',
            userData: userData,
            patientData: patientData,
            showLoading:false
        }
    }
    componentWillReceiveProps(nextProp) {
        if (nextProp.isShow === true) {
            this.loadCommonModal()
        } else if (this.props.isShow === false) {
            this.cleanInput();
        }
    }

    componentDidMount() {
        var titleData = JSON.parse(sessionStorage.getItem('titleData'))
        this.loadWaitCallMsg()
        this.loadCommonModal()
        this.loadHeaderData();
        this.getBindInfo()
        this.loadNextPatientMsg()
        if(titleData && titleData.typeCode == 'VIDEO'){
            this.handleCheckMedical()//校验病历是否填写
        }
    }

    handleCheckMedical(status) {
        this.setState({
            showLoading:true
        })
        var params = {
            params: [{
                admID: this.state.patientData && this.state.patientData.admID,
                flag: status == 'again' ? 'N' : (status == 'click' ? 'Y' : 'N')
            }]
        }
        $http.post('urlDeault', this, {
            code: "03100116",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        showLoading:false,
                        descripts: res.result.descripts,
                        statusCode: res.result.code,
                        statusFlag: res.result.flag,
                        prompt: res.result.prompt,
                        status: status,
                        buttomText: res.result.button,
                    }, () => {
                        if (this.state.statusCode == '3') {
                            this.handleEndOfReceive()
                        }
                    })
                }else{
                    this.setState({
                        showLoading:false,
                    })
                }
            },error:function(){
                this.setState({
                    showLoading:false,
                })
            }
        });
    }

    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] : ''
    }

    //待呼叫
    loadWaitCallMsg() {
        let data = {
            params: [{
                locID: this.state.userData.locID,
                userID: this.state.userData.userID,
            }]
        }
        $http.post('urlDeault', this, {
            code: "03050128",
            data: data,
            success: (res) => {
                if (res.errorCode == '0') {
                    this.setState({
                        watiMsg: res.result,
                    })
                }
            }
        })
    }

    getBindInfo() {
        let data = {
            params: [{
                dataID: "8",
                hospID: this.state.userData.hospID,
                purCode: "purCode",
            }]
        }
        $http.post('urlDeault', this, {
            code: "04150007",
            data: data,
            success: (res) => {
                if (res.errorCode == '0') {
                    if (res.result.serverIP != undefined) {
                        callNumberSocket.init(res.result.serverIP, res.result.serverPort);
                        this.setState({
                            bindInfo: res.result,
                        })
                    }

                }
            }
        })
    }

    loadHeaderData() {
        var params = {
            params: [{}]
        }
        $http.post('urlDeault', this, {
            code: "03050005",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        sumData: res.data
                    })
                }
            }
        });
    }


    closeModal(name) {
        this.setState({
            status: ''
        }, () => {
            if (this.state.statusCode == '3') {
                this.handleEndOfReceive()
            }
        })
        let { onHide } = this.props;
        onHide && onHide('isShowTriageSystemModal');
    }

    handleBackPatient() {
        this.loadFinnalPatient('finish')
    }

    //结束就诊，返回列表
    loadFinnalPatient(status) {
        var params = {
            params: [{
                patAdmID: this.state.patientData.admID,
            }]
        }
        $http.post('urlDeault', this, {
            code: "03050008",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {

                    var { loadPatientPage } = this.props;
                    loadPatientPage && loadPatientPage(status)
                }
            }
        });
    }

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

    //叫号
    loadCallNum() {
        var params = {
            params: [{
                locID: this.state.userData.locID,
                userID: this.state.userData.userID,
                docID: this.state.userData.userID
            }]
        }
        $http.post('urlDeault', this, {
            code: "03050006",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    // this.loadData()
                    this.setState({
                        callMsg: res.result
                    }, () => {
                        this.sendMessage()
                    })
                }
            }
        });
    }

    /** 发送消息 */
    sendMessage = () => {
        if (Util.isEmpty(this.state.bindInfo)) {
            message.warn(this.setMessage('DoctorAdviceModalele', 'descripts'),this.state.userData && this.state.userData.messageTime);
            return;
        }
        let msg = {};
        let ip = this.state.bindInfo.ip;
        callNumberSocket.sendMsg(this.state.callMsg, ip)
    }

    //接诊，先调用结束就诊，再进行接诊
    handleAccepts() {
        var params = {
            params: [{
                patAdmID: this.state.patientData.admID,
            }]
        }
        $http.post('urlDeault', this, {
            code: "03050008",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    // message.success('操作成功')
                    // this.props.history.push('./03050001')
                    // sessionStorage.removeItem('patientData')
                    this.handleAcceptAction()
                }
            }
        });
    }

    acceptAction() {
        var params = {
            params: [{
                patAdmID: this.state.patientData ? this.state.patientData.admID : '',
            }]
        }
        $http.post('urlDeault', this, {
            code: "03050010",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    // message.success('操作成功')
                    // this.props.history.push('./03050001')
                    // sessionStorage.removeItem('patientData')
                    // this.handleAcceptAction()
                }
            }
        });
    }

    handleAcceptAction() {
        if (!Util.isEmpty(this.state.watiMsg)) {
            var data = {
                admID: this.state.watiMsg.admID,
                patID: this.state.watiMsg.patID
            }
        }
        sessionStorage.setItem('patientData', JSON.stringify(data))
        var params = {
            params: [{
                patAdmID: this.state.watiMsg ? this.state.watiMsg.admID : ''
            }]
        }
        $http.post('urlDeault', this, {
            code: "03050010",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    // message.success('接诊成功')
                    this.closeModal()
                    var { loadPatientMsg, loadTitleData } = this.props;
                    loadPatientMsg && loadPatientMsg()
                    loadTitleData && loadTitleData()
                }
            }
        });
    }

    loadPassNum() {
        var params = {
            params: [{
                locID: this.state.userData.locID,
                userID: this.state.userData.userID,
                docID: this.state.userData.userID
            }]
        }
        $http.post('urlDeault', this, {
            code: "03050007",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.loadWaitCallMsg()
                }
            }
        });
    }

    //视频图文结束就诊
    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)
                    this.setState({
                        endReceive: true//结束就诊，用于点击返回的时候去结束就诊
                    })
                    if (status == 'next') {
                        this.loadVideoCall()
                    }
                    if (status == 'success') {
                        var { loadPatientPage } = this.props;
                        loadPatientPage && loadPatientPage(status)
                    }
                }
            }
        });
    }

    //结束就诊
    handleEndOfReceiveMsg() {
        if(this.state.endReceive !== true){
            this.handleEndOfReceive()
        }
        var { loadPatientPage } = this.props;
        loadPatientPage && loadPatientPage('success')
    }

    //视频呼叫
    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()
                }
            }
        });
    }

    render() {
        var { buttomText } = this.state;
        var titleData = JSON.parse(sessionStorage.getItem('titleData'))

        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)}
                ><Spin tip="加载中..." spinning={this.state.showLoading} >
                 </Spin>
                    {titleData && titleData.typeCode == 'VIDEO' ? <div>
                    {/* <h3 style={{ textAlign: 'center', marginTop: 10 }}>是否完成本次问诊？</h3>
                    <Row style={{ textAlign: 'center', marginTop: 10 }}>
                        <Button onClick={this.handleReturnMsg.bind(this)}>{'否>>继续问诊'}</Button>
                        <Button style={{ marginLeft: 10 }} onClick={this.handleEndOfReceive.bind(this)}>{'是>>结束问诊'}</Button>
                        <Button style={{ marginLeft: 10 }} disabled={Util.isEmpty(this.state.nextPatientMsg)}
                            onClick={this.handleEndOfReceive.bind(this, 'next')}>{'是>>结束并呼叫下一位'}</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> */}
                    {this.state.statusCode == '1' ?
                        <div style={{ textAlign: 'center' }}>
                            <h3 style={{ marginTop: 20, marginLeft: 20 }}>{this.state.descripts}</h3>
                            <Button type='primary' style={{ marginTop: 50 }} onClick={this.handleReturnMsg.bind(this)}>返回继续填写</Button>
                        </div> :
                        (this.state.statusCode == '2' ? <div style={{ textAlign: 'center' }}>
                            {this.state.status == 'click' ? <h3 style={{ color: 'red', marginTop: 10 }}>{this.state.prompt}</h3> : null}
                            <h3 style={{ marginTop: 20, marginLeft: 20 }}>{this.state.descripts}</h3>
                            {/* <Button style={{ marginTop: 50 }} onClick={this.handleReturnMsg.bind(this)}>暂不签名</Button> */}
                            <Button type='primary' style={{ marginTop: 50, marginLeft: 10 }}
                                onClick={this.handleCheckMedical.bind(this, 'click')}>我已完成签名</Button>
                        </div> :
                            (this.state.statusCode == '3' ? <div style={{ textAlign: 'center' }}>
                                <h3 style={{ marginTop: 20, marginLeft: 20 }}>{this.state.descripts}</h3>
                                <Button type='primary' style={{ marginTop: 50 }} onClick={this.handleEndOfReceiveMsg.bind(this)}>返回病人列表</Button>
                            </div> :
                                (this.state.statusCode == '6' ? <div style={{ textAlign: 'center' }}>
                                    {this.state.status == 'again' ? <h3 style={{ color: 'red', marginTop: 10 }}>{this.state.prompt}</h3> : null}
                                    <h3 style={{ marginTop: 20, marginLeft: 20 }}>{this.state.descripts}</h3>
                                    <Button style={{ marginTop: 50 }} onClick={this.handleReturnMsg.bind(this)}>取消</Button>
                                    <Button type='primary' style={{ marginTop: 50, marginLeft: 10 }}
                                        onClick={this.handleCheckMedical.bind(this, 'again')}>重新上传</Button>
                                </div> : null)))}
                </div> :
                    (titleData && titleData.typeCode == 'IMAGE' ? <div>
                        <h3 style={{ textAlign: 'center', marginTop: 10 }}>是否完成本次问诊？</h3>
                        <Row style={{ textAlign: 'center', marginTop: 10 }}>
                            <Button onClick={this.handleReturnMsg.bind(this)}>{'否>>继续问诊'}</Button>
                            <Button style={{ marginLeft: 10 }} onClick={this.handleEndOfReceive.bind(this,'success')}>{'是>>结束问诊'}</Button>
                        </Row>
                    </div> :
                        <div>
                            <div style={{ display: 'flex', borderBottom: '1px solid #64A982', paddingLeft: '10%' }}>
                                <span>{this.setMessage('DoctorAdviceModalTotal', 'descripts')}{this.state.sumData ? this.state.sumData.totalCount : 0}</span>
                                <span style={{ marginLeft: 10 }}>{this.setMessage('DoctorAdviceModalxcv', 'descripts')}{this.state.sumData ? this.state.sumData.finishCount : 0}</span>
                                <span style={{ marginLeft: 10 }}>{this.setMessage('DoctorAdviceModalxc', 'descripts')}{this.state.sumData ? this.state.sumData.overCount : 0}</span>
                                <span style={{ marginLeft: 10 }}>{this.setMessage('DoctorAdviceModalPatients', 'descripts')}{this.state.sumData ? this.state.sumData.waitCount : 0}</span>
                            </div>
                            <div style={{ paddingLeft: 20, borderBottom: '1px solid #64A982' }}>
                                <div style={{ display: 'flex' }}><div className='border-radius-registr'></div>
                                    <div style={{ marginLeft: 10, fontWeight: 'bold' }}>{this.setMessage('DoctorAdviceModalvc', 'descripts')}</div></div>

                                <div style={{ display: 'flex', paddingLeft: '15%', margin: '20px 0' }}>
                                    <Button type='primary' onClick={this.handleBackPatient.bind(this)}>{this.setMessage('DoctorAdviceModalcvb', 'descripts')}</Button>
                                    <Button style={{ marginLeft: 20 }} type='danger' onClick={this.handleReturnMsg.bind(this)}>{this.setMessage('DoctorAdviceModalcbe', 'descripts')}</Button>
                                </div>
                            </div>
                            <div style={{ paddingLeft: 20, marginTop: 10 }}>
                                <div style={{ display: 'flex' }}><div className='border-radius-fee'></div>
                                    <div style={{ marginLeft: 10, fontWeight: 'bold' }}>{this.setMessage('DoctorAdviceModalrr', 'descripts')}</div></div>
                                <span>
                                    <span>{this.state.watiMsg ? this.state.watiMsg.patName : ''}</span><Divider type="vertical" />
                                    <span>{this.state.watiMsg ? this.state.watiMsg.patNo : ''}</span><Divider type="vertical" />
                                    <span>{this.state.watiMsg ? this.state.watiMsg.patSex : ''}</span><Divider type="vertical" />
                                    <span>{this.state.watiMsg ? this.state.watiMsg.patAge : ''}</span><Divider type="vertical" />
                                    <span>{this.state.watiMsg ? this.state.watiMsg.patSocial : ''}</span><Divider type="vertical" />
                                    <span>{this.state.watiMsg ? this.state.watiMsg.admReason : ''}</span>
                                </span>
                                <div style={{ textAlign: 'center', display: 'flex', paddingLeft: '40%' }}>
                                    <div onClick={this.loadCallNum.bind(this)}><div><Icon type='bell' /></div>{this.setMessage('DoctorAdviceModalrrq', 'descripts')}</div>
                                    <div style={{ marginLeft: 40 }} onClick={this.handleAccepts.bind(this)}><div><Icon type='user' /></div>{this.setMessage('DoctorAdviceModalAdmission', 'descripts')}</div>
                                </div>
                                <div style={{ display: 'flex', paddingLeft: '20%', marginTop: 20 }}>
                                    <Button onClick={this.loadCallNum.bind(this)}><Icon type='bell' />{this.setMessage('DoctorAdviceModalRecall', 'descripts')}</Button>
                                    <Button style={{ marginLeft: 10 }} onClick={this.loadPassNum.bind(this)}><Icon type='folder-add' />{this.setMessage('DoctorAdviceModalht', 'descripts')}</Button>
                                </div>
                            </div>
                        </div>)}
              
                   
                {/**titleData.typeCode == 'VIDEO' 时，表示为视频问诊页签、为3时是图文问诊页签、为1时是门诊 */}
                
            </Modal>
            </div>
    }
}

export default Form.create()(TriageSystemModal);