<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>
    <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 style="height: 5px"></div>
      <van-row justify="end">
        <van-col span="4">
          <div style="text-align: right">总金额：</div>
        </van-col>
        <van-col span="5">
          <span style="color: orangered">￥ {{ makePrice(params.totalAmt) }}</span>
        </van-col>
      </van-row>
      <div style="height: 5px"></div>
    </div>
    <div style="height: 10px"></div>
    <van-button block type="primary" @click="wechatPay">微信支付（￥ {{ makePrice(params.totalAmt) }}）</van-button>
  </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'
export default {
  name: 'UnPaidDetail',
  setup() {
    const router = useRouter()
    const params = router.currentRoute.value.params
    const fees = ref([])
    const wechatPay = () => {
      let bodyText = '门诊缴费'
      if (fees.value.length === 1) {
        bodyText = fees.value[0].itemName
      }
      const param = {
        body: bodyText,
        orderType: 2,
        totalFee: params.totalAmt / 100,
        openId: getLocalOpenId(),
        patientId: params.patientId,
        hisOrdNum: params.hisOrdNum,
        yjReqNo: store.state.yjReqNo,
      }
      wxPay(param)
    }
    onMounted(() => {
      getUnPaidDetail(params.patientId, params.hisOrdNum).then((res) => {
        fees.value = res
      })
    })
    return {
      params,
      fees,
      makePrice,
      wechatPay,
    }
  },
}

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