<template>
  <window-size>
    <div style="height: 5px"></div>
    <van-cell title="费用类型">
      <template #default>
        <span>{{ reqStatus }}</span>
      </template>
    </van-cell>
    <van-cell title="费用金额">
      <template #default>
        <span style="color: orangered">{{ '￥' + mzyReq.totalAmount }}</span>
      </template>
    </van-cell>
    <van-cell title="就诊科室" :value="mzyReq.unitCode"></van-cell>
    <van-cell title="医生姓名" :value="mzyReq.doctorCode"></van-cell>
    <van-cell title="就诊日期" :value="mzyReq.requestDayStr"></van-cell>
    <van-cell title="就诊时段" :value="mzyReq.ampm"></van-cell>
    <van-cell title="就诊人" :value="mzyReq.name"></van-cell>
    <van-cell title="就诊卡号" :value="mzyReq.patientId"></van-cell>
    <div style="height: 10px"></div>
    <van-button type="primary" block :disabled="disableRefundButton" @click="revokeMzyReq">退款退号</van-button>
  </window-size>
</template>

<script setup>
import {onMounted, ref} from 'vue'
import {getMzyReqrecInfo, cancelReqrec} from '@/api/appointment.js'
import {showConfirmDialog, showDialog} from 'vant'
import {useRouter} from "vue-router";
import {getDateDiff} from "@/utils/date";

const router = useRouter()
const patientId = router.currentRoute.value.params.patientId
const times = router.currentRoute.value.params.times
const mzyReq = ref({})

const reqStatus = ref('挂号费')

const disableRefundButton = ref(true)

const revokeMzyReq = () => {
  showConfirmDialog({
    title: '温馨提示',
    message: '是否确认退号退费？'
  }).then(() => {
    cancelReqrec(mzyReq.value).then(() => {
      showDialog({
        message: '退号成功，您的资金已原路退回，请留意微信退款信息。',
      }).then(() => {
        router.go(-1)
      })
    })
  }).catch(() => {})
}

onMounted(() => {
  const params = {patientId, times, filterUnpaidReq: false}
  getMzyReqrecInfo(params).then(res => {
    mzyReq.value = res
    disableRefundButton.value = (getDateDiff(res.requestDayStr) > 0 || null === res.paymode
        || res.visitedMark === '1' || res.cancelMark === '1');
    if (res.visitedMark === '1') {
      reqStatus.value = '挂号费（已就诊）'
    }
    if (res.cancelMark === '1') {
      reqStatus.value = '挂号费（已退号）'
    }
  })
})
</script>
