<template>
  <window-size>
    <van-cell title="医生姓名" :value="appointment.doctorName"/>
    <van-cell title="医生号别" :value="appointment.doctorTitle"/>
    <van-cell title="就诊科室" :value="appointment.deptName"/>
    <van-cell title="就诊日期">
      <template #default>
        <div style="color: orangered">
          {{ appointment.week }}&nbsp;&nbsp;{{ appointment.date }}
        </div>
      </template>
    </van-cell>
    <van-cell title="就诊时段">
      <template #default>
        <div style="color: orangered">
          {{ appointment.ampm }}&nbsp;&nbsp;{{ appointment.apValue || '' }}
        </div>
      </template>
    </van-cell>
    <div style="height: 5px"></div>
    <van-cell title="挂号金额">
      <template #default>
        <div style="color: orangered">￥{{ appointment.fee }}</div>
      </template>
    </van-cell>
    <div style="height: 5px"></div>
    <van-radio-group v-model="appointment.patientId">
      <van-cell-group>
        <div v-for="item in patientCards" :key="item.patientId">
          <van-cell center icon="user-o" :label="item.patientId" clickable
                    @click="changePatient(item.patientId)">
            <template #title>
              <span class="custom-title">{{ item.name }}</span>
              &nbsp;
              <van-tag type="primary" plain v-if="item.isDefault === 1">默认</van-tag>
            </template>
            <template #right-icon>
              <van-radio :name="item.patientId"/>
            </template>
          </van-cell>
        </div>
      </van-cell-group>
    </van-radio-group>
    <van-cell
        v-if="showAddCard"
        icon="add-o"
        title="添加就诊人"
        :value="addCardText"
        is-link
        to="/addElectronicHealthCard"
    ></van-cell>
    <div style="height: 10px"></div>
    <van-button type="primary" block @click="confirmAppointment" :disabled="disableGhBtn">确认挂号</van-button>
  </window-size>
</template>

<script setup>
import store from '@/store'
import {computed, onMounted, ref} from 'vue'
import {checkAppointmentRequirements, getGhFee} from '@/api/appointment'
import router from '@/router'
import {showConfirmDialog, showToast} from 'vant'

const disableGhBtn = ref(true)
const appointment = store.state.appointmentInfo
const patientCards = computed(() => {
  return store.state.patientCards
})

const showAddCard = computed(() => {
  return patientCards.value.length < 5
})
const addCardText = computed(() => {
  return '还可添加' + (5 - patientCards.value.length) + '人'
})
const changePatient = (patientId) => {
  appointment.patientId = patientId
  queryAppointmentCost()
}

const queryAppointmentCost = () => {
  getGhFee(appointment).then((res) => {
    appointment.fee = res.fee
    disableGhBtn.value = false
    if (res.message) {
      showToast({
        message: res.message,
        position: 'top',
        duration: 3000,
      })
    }
  }).catch(() => {
    appointment.fee = '获取失败'
    disableGhBtn.value = true
  })
}

const confirmAppointment = () => {
  const message = appointment.ampm === '夜间门诊'
      ? '夜间门诊就诊时间为17:30-21:00，如您疼痛难忍或病情紧急，请至一楼急诊科就诊。'
      : '实际看诊序号以至分诊台到诊登记为准。当天未就诊，不予退号、换号、转科，敬请谅解。'
  checkAppointmentRequirements(appointment.patientId, appointment.deptCode).then(() => {
    showConfirmDialog({
      title: '温馨提示',
      message,
      confirmButtonText: '确认挂号',
      cancelButtonText: '取消挂号'
    }).then(() => {
      fetchPatientName()
    }).catch(() => {
    })
  })
}

function fetchPatientName() {
  patientCards.value.forEach((item) => {
    if (item.patientId === appointment.patientId) {
      appointment.patientName = item.name
      toCashier()
    }
  })
}

function toCashier() {
  const createOrderRequest = {...appointment}
  createOrderRequest.body = '挂号费'
  createOrderRequest.orderType = 1
  createOrderRequest.totalFee = appointment.fee
  createOrderRequest.fundpayAmt = 0
  createOrderRequest.acctpayAmt = 0
  createOrderRequest.couponAmt = 0
  createOrderRequest.cashpayAmt = appointment.fee
  store.dispatch({
    type: 'storeCreateOrderRequest',
    createOrderRequest,
  }).then(() => {
    router.push('/cashier')
  })
}

onMounted(() => {
  appointment.patientId = store.getters.getDefaultCard.patientId
  queryAppointmentCost()
})
</script>
