<template>
  <window-size>
    <van-cell title="核酸检测自助开单" is-link @click="showDiaglog"></van-cell>
    <van-cell title="查询检测结果" is-link :to="'/checkCovidExamResult/' + patientId"></van-cell>

    <van-dialog v-model:show="showInputIdCard" title="填写身份证" show-cancel-button @confirm="confirmChangeIdCard">
      <div style="font-size: 12px; color: red; padding: 5px 10px 10px 15px">
        您在我院绑定的身份证号不是有效的身份证号，请重新填写：
      </div>
      <van-field v-model="socialNo" label="身份证号"></van-field>
    </van-dialog>
  </window-size>
</template>

<script>
import { Dialog, Toast } from 'vant'
import { useRouter } from 'vue-router'
import { hasDoneCovidAssessment, savePrescription, updateIdCard } from '../../../api/order-covid-exam'
import { ref } from 'vue'
import { isValidIdcard } from '../../../utils/validate'
export default {
  setup() {
    const router = useRouter()
    const patientId = router.currentRoute.value.params.patientId
    const showInputIdCard = ref(false)
    const socialNo = ref(null)
    const confirmChangeIdCard = () => {
      if (!isValidIdcard(socialNo.value)) {
        Toast.fail('您输入的身份证不合法！')
      } else {
        updateIdCard(patientId, socialNo.value).then(() => {
          Toast.success('更新身份证成功，您可以继续核酸检测自助开单了。')
        })
      }
    }
    const showDiaglog = () => {
      Dialog.confirm({
        message: '是否确认预定新冠核酸检测？',
        title: '提示',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      })
        .then(() => {
          hasDoneCovidAssessment(patientId).then((res) => {
            if (res === 'no') {
              router.push('/assessments/covid/' + patientId)
            } else {
              savePrescription(patientId)
                .then(() => {
                  router.push('/unPaidList/' + patientId)
                })
                .catch(() => {
                  showInputIdCard.value = true
                })
            }
          })
        })
        .catch(() => {})
    }
    return {
      patientId,
      showDiaglog,
      showInputIdCard,
      socialNo,
      confirmChangeIdCard,
    }
  },
}
</script>