<template>
  <window-size>
    <van-cell title="就诊人" :value="params.patName"></van-cell>
    <van-cell title="医院名称" value="长沙泰和医院"></van-cell>
    <van-cell title="开方科室" :value="params.deptName"></van-cell>
    <van-cell title="开方医生" :value="params.doctorName"></van-cell>
    <van-cell title="总金额">
      <template #default>
        <span style="color: orangered">{{ makeMoney(params.totalAmt) }}</span>
      </template>
    </van-cell>
    <div v-if="params.fundPay && params.fundPay !== 'undefined'">
      <van-cell title="医保统筹支付">
        <template #default>
          <span style="color: orangered">{{ makeMoney(params.fundPay) }}</span>
        </template>
      </van-cell>
      <van-cell v-if="params.acctPay" title="个人账户支付">
        <template #default>
          <span style="color: orangered">{{ makeMoney(params.acctPay) }}</span>
        </template>
      </van-cell>
      <van-cell title="个人现金支付">
        <template #default>
          <span style="color: orangered">{{ makeMoney(params.selfAmt) }}</span>
        </template>
      </van-cell>
    </div>
    <div style="height: 5px"></div>
    <div style="background: white; text-align: center; font-size: 12px">
      <div style="height: 5px"></div>
      <van-row>
        <van-col span="10">项目名称</van-col>
        <van-col span="5">单价（元）</van-col>
        <van-col span="4">数量</van-col>
        <van-col span="5">金额（元）</van-col>
      </van-row>
      <div style="height: 5px"></div>
      <div v-for="(fee, index) in fees" :key="index">
        <div style="height: 5px"></div>
        <van-row>
          <van-col span="10">{{ fee.itemName }}</van-col>
          <van-col span="5">{{ makePrice(fee.itemPrice) }}</van-col>
          <van-col span="4">{{ fee.itemNumber }}</van-col>
          <van-col span="5">{{ makePrice(fee.itemTotalFee) }}</van-col>
        </van-row>
        <div style="height: 5px"></div>
      </div>
    </div>
    <div style="height: 10px"></div>
    <van-button block type="primary" @click="wechatPay">微信支付（￥ {{ makePrice(params.selfAmt) }}）</van-button>

    <van-dialog
      v-model:show="showBillInfo"
      title="缴费成功提醒"
      show-confirm-button
      :show-cancel-button="false"
      @closed="router.push('/hospitalService')"
    >
      <div style="height: 12px"></div>
      <van-cell title="用户姓名" :value="params.patName" />
      <van-cell title="订单号" :value="params.hisOrdNum" />
      <van-cell title="缴费金额" :value="makePrice(params.selfAmt)" />
      <van-cell title="医生" :value="params.doctorName" />
      <van-cell title="科室" :value="params.deptName" />
      <van-cell title="医院" value="长沙泰和医院" />
      <van-cell title="缴费时间" :value="params.paytime" />
    </van-dialog>
  </window-size>
</template>

<script>
import { useRouter } from 'vue-router'
import { getUnPaidDetail } from '../../../api/pay-mz-fee'
import { onMounted, ref } from 'vue'
import { getLocalOpenId } from '../../../utils/check-patient-id'
import { wxPay } from '../../../utils/wx-pay'
import store from '../../../store'
import { Toast } from 'vant'
export default {
  name: 'UnPaidDetail',
  setup() {
    const router = useRouter()
    const params = router.currentRoute.value.params
    const fees = ref([])
    const showBillInfo = ref(false)
    const wechatPay = () => {
      let bodyText = '门诊缴费'
      if (fees.value.length === 1) {
        bodyText = fees.value[0].itemName
      }
      const param = {
        body: bodyText,
        orderType: 2,
        totalFee: makePrice(params.selfAmt),
        openId: getLocalOpenId(),
        patientId: params.patientId,
        hisOrdNum: params.hisOrdNum,
        yjReqNo: store.state.yjReqNo,
      }
      wxPay(param)
        .then((result) => {
          params.paytime = result.payDatetime
          showBillInfo.value = true
        })
        .catch((result) => {
          Toast.success(result)
          router.push('/hospitalService')
        })
    }
    onMounted(() => {
      getUnPaidDetail(params.patientId, params.hisOrdNum).then((res) => {
        fees.value = res
      })
    })
    return {
      router,
      params,
      fees,
      showBillInfo,
      makePrice,
      makeMoney,
      wechatPay,
    }
  },
}

function makePrice(num) {
  return (num / 100).toFixed(2)
}

function makeMoney(money) {
  const m = money / 100
  return '￥ ' + m.toFixed(2)
}
</script>
