<template>
    <div class="page_computerCalling">
        <div class="cell clearfix">
            <label class="title"><span class="name">叫号状态</span> : </label>
            <div class="time ">
                <ul class="">
                    <li v-for="option in patient_state" :key="option.value" @click="handleStateChange(option.value)" :class="patientStateVal == option.value ? 'active' : ''" >{{option.label}}
                    </li>
                </ul>
            </div>
        </div>
        <div style="display:flex;justify-content: space-between;">
            <div class="callingArea">
                <called v-if="patientStateVal == 0" :index='1' :called='waitingCalled'></called>
                <called v-if="patientStateVal == 1" :called="called"></called>
            </div>
            <div class="nowCalling" v-if="patientStateVal == 0">
                <p class="nowCallingTitle">当前叫号</p>
                <p class="nowCallingName">{{ fisrtQueueInfo.patient_name }}</p>
                <p class="nowCallingTime">签到时间：{{ fisrtQueueInfo.create_time }}</p>
                <p><el-button type="primary" @click="call(fisrtQueueInfo.patient_id)" style="margin-left:0;margin: 30px 0 20px 0;">&ensp;叫号&ensp;</el-button></p>
                <el-button style="margin:0 auto;" @click="next(fisrtQueueInfo.patient_id)">下一位</el-button>
            </div>
        </div>
    </div>
</template>

<script>
const moment = require('moment')
import axios from 'axios'
import called from './called'
export default {
    components:{
       called 
    },
    data(){
        return{
            patient_state:[
                {value: 0,label: '待叫号'},
                {value: 1,label: '已叫号'},
            ],
            patientStateVal: 0,
            waitingCalled:[],
            called:[],
            fisrtQueueInfo:{}
        }
    },
    computed: {
        websocket() {
            return this.$store.state.user.websocket;
        },
    },
    created(){
        this.initData = {
            cmd: "queue/join",
            data: {type:4,page:0,size:0},
        };
        this.websocketSend(this.initData)
    },
    beforeMount() {
        this.websocketMess();
    },
    methods:{
        websocketSend(data) {
            try {
                this.websocket.send(JSON.stringify(data))
                
            } catch (error) {
                this.showError = true;
                this.showIndex = 4;
                this.errorInfo = "网络异常，请稍后退出重试！";
            }
        },
        websocketMess() {
            this.websocket.onmessage = e => {
                let res = JSON.parse(e.data);
                // let res = re.data;
                console.log('res',res)
                if(res.channel == 'queue/join'){
                    res.data.fisrtQueueInfo.create_time = moment(res.data.fisrtQueueInfo.create_time * 1000).format('HH:mm')
                    this.fisrtQueueInfo = res.data.fisrtQueueInfo
                    let arr = res.data.patientQueueList.data
                    let waitingCalledArr = []
                    let calledArr = []
                    arr.map(item => {
                        if(item.status == 2){
                            item.create_time = moment(item.create_time * 1000).format('HH:mm:ss')
                            waitingCalledArr.push(item)
                        }else if(item.status == 3){
                            item.create_time = moment(item.create_time * 1000).format('HH:mm:ss')
                            calledArr.push(item)
                        }
                    })
                    console.log('waitingCalledArr',waitingCalledArr)
                    console.log('waitingCalledArr',calledArr)
                    this.waitingCalled = waitingCalledArr
                    this.called = calledArr
                }else if(res.channel == 'allQueueList'){
                    let arr = res.data.queue_list.data
                    let waitingCalledArr = []
                    let calledArr = []
                    arr.map(item => {
                        if(item.status == 2){
                            item.create_time = moment(item.create_time * 1000).format('HH:mm:ss')
                            waitingCalledArr.push(item)
                        }else if(item.status == 3){
                            item.create_time = moment(item.create_time * 1000).format('HH:mm:ss')
                            calledArr.push(item)
                        }
                    })
                    this.waitingCalled = waitingCalledArr
                    this.called = calledArr
                }else if(res.channel == 'patientCallInfo'){
                    res.data.patientInfo.create_time = moment(res.data.patientInfo.create_time * 1000).format('HH:mm')
                    this.fisrtQueueInfo = res.data.patientInfo
                }
            }
        },
        handleStateChange: function(index) {
            this.patientStateVal = index
        },
        call(patient_id){
            console.log('patient_id',patient_id)
            let org_id =  parseInt(sessionStorage.getItem("org_id"));
            let admin_user_id = parseInt(sessionStorage.getItem("admin_user_id"));
            axios.get('/api/index/callpatient?org_id=' + org_id + '&patient_id=' + patient_id + '&admin_user_id=' + admin_user_id).then(res => {
                console.log(res)
                // let patientArr = res.data.queue_list.data
                // this.patientArr = patientArr
                // this.$emit('child-event',this.patientArr)
                if(res.data.code == 200){
                    this.$message({
                        message: res.data.msg,
                        type: 'success'
                    });
                }
            })
        },
        next(patient_id){
            console.log('patient_id',patient_id)
            let org_id =  parseInt(sessionStorage.getItem("org_id"));
            let admin_user_id = parseInt(sessionStorage.getItem("admin_user_id"));
            axios.get('/api/index/nextupcall?org_id=' + org_id + '&patient_id=' + patient_id + '&admin_user_id=' + admin_user_id).then(res => {
                console.log(res)
                // let patientArr = res.data.queue_list.data
                // this.patientArr = patientArr
                // this.$emit('child-event',this.patientArr)
                if(res.data.code == 200){
                    this.$message({
                        message: res.data.msg,
                        type: 'success'
                    });
                }
            })
        },
    }
}
</script>


<style lang="scss" scoped>
.callingArea{
    display: flex;
    flex-wrap: wrap;
    width: 86%;
}
.nowCalling{
    height: 351px;
    border: 1px solid #e5e5ee;
    // flex: 1;
    width: 14%;
    text-align: center;
    .nowCallingTitle{
        font-size: 20px;
        font-weight: 600;
        margin-top: 20px;
        text-align: center;
    }
    .nowCallingName{
        color: #338AFB;
        font-size: 18px;
        margin-top: 10px;
        font-weight: 600;
    }
    .nowCallingTime{
        font-size: 16px;
        margin-top: 30px;
    }
}
</style>
