<template>
  <window-size>
    <van-cell title="就诊人" :value="routeParams.patientName"></van-cell>
    <van-cell title="医院名称" value="长沙泰和医院"></van-cell>
    <van-cell title="开方科室" :value="routeParams.deptName"></van-cell>
    <van-cell title="开方医生" :value="routeParams.doctorName"></van-cell>
    <van-cell title="总金额">
      <template #default>
        <span style="color: orangered">{{ makeMoney(routeParams.totalAmt) }}</span>
      </template>
    </van-cell>
    <div v-if="routeParams.fundPay && routeParams.fundPay !== 'undefined'">
      <van-cell title="医保统筹支付">
        <template #default>
          <span style="color: orangered">{{ makeMoney(routeParams.fundPay) }}</span>
        </template>
      </van-cell>
      <van-cell v-if="routeParams.acctPay" title="个人账户支付">
        <template #default>
          <span style="color: orangered">{{ makeMoney(routeParams.acctPay) }}</span>
        </template>
      </van-cell>
      <van-cell title="个人现金支付">
        <template #default>
          <span style="color: orangered">{{ makeMoney(routeParams.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 chargeList" :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="toCashier">提交订单</van-button>
  </window-size>
</template>

<script setup>
import {useRouter} from 'vue-router'
import {getUnPaidDetail} from '@/api/pay-mz-fee'
import {onMounted, ref} from 'vue'
import store from '@/store'

const router = useRouter()
const routeParams = router.currentRoute.value.params
const chargeList = ref([])

function toCashier() {
  const createOrderRequest = {
    body: '门诊缴费',
    orderType: 2,
    totalFee: routeParams.totalAmt / 100,
    fundpayAmt: routeParams.fundPay / 100,
    acctpayAmt: routeParams.acctPay / 100,
    cashpayAmt: routeParams.selfAmt / 100,
    couponAmt: 0,
    patientId: routeParams.patientId,
    hisOrdNum: routeParams.hisOrdNum,
    yjReqNo: store.state.yjReqNo,
    doctorCode: routeParams.doctorCode,
    doctorName: routeParams.doctorName,
    deptName: routeParams.deptName,
    patientName: routeParams.patientName,
    chargeList: chargeList.value
  }
  store.dispatch({
    type: 'storeCreateOrderRequest',
    createOrderRequest,
  }).then(() => {
    router.push('/cashier')
  })
}

onMounted(() => {
  getUnPaidDetail(routeParams.patientId, routeParams.hisOrdNum).then((res) => {
    chargeList.value = res
  })
})

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

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