<template>
    <div style="margin-left: 4px">
        <el-button type="success" icon="Grid" @click="beforeReadCard">读取电子凭证</el-button>
        <el-dialog v-model="showSelectBiztype" title="请选择读卡业务" width="300px">
            <el-radio-group v-model="currentBiztype">
                <el-radio label="01101">门诊挂号</el-radio>
                <el-radio label="01201">门诊问诊</el-radio>
                <div style="height: 16px"></div>
                <el-radio label="01203">门诊检查</el-radio>
                <el-radio label="01204">门诊治疗</el-radio>
                <div style="height: 16px"></div>
                <el-radio label="01301">门诊结算</el-radio>
            </el-radio-group>
            <template #footer>
                <el-button type="primary" @click="handleConfirmBiztype">确定</el-button>
            </template>
        </el-dialog>
    </div>
</template>

<script>
import {ElMessage, ElMessageBox} from 'element-plus'
import axios from 'axios'

export default {
    props: {
        patNo: {
            type: String,
            default: null,
        },
        biztype: {
            type: String,
            default: null,
        },
    },
    emits: ['success'],
    setup(props, ctx) {
        const showSelectBiztype = ref(false)
        const currentBiztype = ref(null)

        const isFuZhen = ref(0)
        const beforeReadCard = () => {
            if (!props.patNo) {
                ElMessage({
                    message: '请先选择患者',
                    type: 'warning',
                    duration: 2500,
                    showClose: true,
                })
                return
            }
            if (props.patNo === 'triage') {
                ElMessageBox.confirm(null, '请选择患者就诊类型', {
                    confirmButtonText: '初诊',
                    cancelButtonText: '复诊',
                    distinguishCancelAndClose: true
                }).then(() => {
                    isFuZhen.value = 0
                    props.biztype ? executeReadCard() : showSelectBiztype.value = true;
                }).catch((action) => {
                    if (action === 'cancel') {
                        isFuZhen.value = 1
                        props.biztype ? executeReadCard() : showSelectBiztype.value = true;
                    }
                })
            } else {
                props.biztype ? executeReadCard() : showSelectBiztype.value = true;
            }
        }

        const handleConfirmBiztype = () => {
            if (!currentBiztype.value) {
                ElMessage({
                    message: '请选择读卡业务！',
                    type: 'warning',
                    duration: 2500,
                    showClose: true,
                })
                return
            }
            showSelectBiztype.value = false
            executeReadCard()
        }

        const executeReadCard = () => {
            const nowbiztype = props.biztype || currentBiztype.value
            const param = `qrcode_${nowbiztype}`
            axios('http://localhost:8321/readcard/entry?param=' + param)
                .then((res) => {
                    if (res.data.code === 200) {
                        const result = {
                            mdtrtCertType: 'qrcode',
                            readCardResult: JSON.stringify(JSON.parse(res.data.data).data),
                            readCardBizType: nowbiztype,
                            isFuZhen: isFuZhen.value,
                        }
                        ctx.emit('success', result)
                        ElMessage({
                            message: res.data.message,
                            type: 'success',
                            duration: 2500,
                            showClose: true
                        })
                    } else {
                        ElMessageBox.alert(res.data.message.trim(), '错误', {
                            type: 'error',
                            showCancelButton: false,
                        }).then(() => {})
                    }
                }).catch((e) => {
                    ElMessageBox.alert(e, '错误', {
                        type: 'error',
                        showCancelButton: false,
                    }).then(() => {})
                })
        }

        return {
            currentBiztype,
            showSelectBiztype,
            beforeReadCard,
            handleConfirmBiztype,
        }
    },
}
</script>
