<template>
  <div class="home">
    <nav-bar url="/Member_wdzd"></nav-bar>


    <van-list v-model="loading" :finished="finished" @load="onLoad">
      <div class="time_box" v-for="item in prescriptionList">
        <van-cell-group>
          <van-cell title="药品名称" :value="item.COSTNAME" />
          <van-cell title="金额" :value="item.COSTAMOUNT" />
          <van-cell title="付数" :value="item.COSTSUM" />
          <van-cell title="单位" :value="item.UNIT" />
          <van-cell title="单价" :value="calculateUnitPrice(item)" />
          <van-cell title="规格厂家" :value="item.SPEC" />
        </van-cell-group>
      </div>
      <!--    <van-list v-for="item in prescriptionList">-->

      <!--        <ul>-->
      <!--          <div class="cellbody">-->

      <!--            <li v-for="item in prescriptionList"-->
      <!--            >-->
      <!--              <van-row>-->
      <!--                <van-col span="5" class="colgray">药品名称：</van-col>-->
      <!--                <van-col span="6" class="colblack">{{ item.COSTName }}</van-col>-->
      <!--                <van-col span="5" class="colgray">金额：</van-col>-->
      <!--                <van-col span="6" class="colblack">{{ item.COSTAMOUNT }}</van-col>-->
      <!--              </van-row>-->
      <!--              <van-row>-->
      <!--                <van-col span="5" class="colgray">付数：</van-col>-->
      <!--                <van-col span="6" class="colblack">{{ item.COSTSUM }}</van-col>-->
      <!--                <van-col span="5" class="colgray">单位：</van-col>-->
      <!--                <van-col span="6" class="colblack">{{ item.UNIT }}</van-col>-->
      <!--              </van-row>-->
      <!--              <van-row>-->
      <!--                <van-col span="5" class="colgray">单价：</van-col>-->
      <!--                <van-col span="6" class="colblack">{{ item.AMOUNT }}</van-col>-->
      <!--                <van-col span="5" class="colgray">规格厂家：</van-col>-->
      <!--                <van-col span="6" class="colblack">{{ item.SPEC }}</van-col>-->
      <!--              </van-row>-->
      <!--            </li>-->
      <!--          </div>-->
      <!--        </ul>-->
      <!--      <van-cell-group>-->
      <!--        <van-cell title="药品名称" :value="item.COSTName"/>-->
      <!--        <van-cell title="金额" :value="item.COSTAMOUNT"/>-->
      <!--        <van-cell title="付数" :value="item.COSTSUM"/>-->
      <!--        <van-cell title="单位" :value="item.UNIT"/>-->
      <!--        <van-cell title="单价" :value="item.AMOUNT"/>-->
      <!--        <van-cell title="规格厂家" :value="item.SPEC"/>-->
      <!--      </van-cell-group>-->
    </van-list>
  </div>
</template>


<script>
// @ is an alias to /src
import { apiopLBQuery, getQueuing } from "@/request/api.js";
import Vue from 'vue';
import { Popup, Toast } from 'vant';

Vue.use(Popup);
export default {
  components: {},

  data() {
    return {
      PatientId: '',
      FlowNo: '',
      prescriptionList: [],
      loading: false,
      finished: false,
    };
  },
  mounted() {
    this.PatientId = this.$route.query.PatientId;
    this.FlowNo = this.$route.query.FlowNo;
    // this.getPrescriptionDetail()
  },
  methods: {
    calculateUnitPrice(item) {
    // 如果 AMOUNT 存在且不为空，直接返回
    if (item.AMOUNT && item.AMOUNT !== '') {
      return item.AMOUNT;
    }
    
    // 如果 COSTAMOUNT 和 COSTSUM 都存在，计算单价
    if (item.COSTAMOUNT && item.COSTSUM && item.COSTAMOUNT !== '' && item.COSTSUM !== '') {
      const amount = parseFloat(item.COSTAMOUNT);
      const sum = parseFloat(item.COSTSUM);
      
      // 避免除零错误
      if (sum !== 0) {
        return (amount / sum).toFixed(2);
      }
    }
    
    // 都不满足条件，返回空字符串
    return '';
  },
    onLoad() {

      this.loading = true
      let queryData = {
        FlowNo: this.FlowNo,
        PatientId: this.PatientId,
        InsuranceType: '0'
      }
      apiopLBQuery(queryData).then(res => {
        console.log(res)
        let jsonObj = this.$x2js.xml2js(res.data);
        console.log(jsonObj);
        let jsonObj2 = this.$x2js.xml2js(
          jsonObj.Envelope.Body.MOP_OutpBillsDetailQueryResponse.MOP_OutpBillsDetailQueryResult
        );
        
        let dataRow = jsonObj2.response.data.data_row;
        console.log(dataRow)
        // 确保 data_row 始终是数组格式
        if (!Array.isArray(dataRow)) {
          this.prescriptionList = [dataRow];
        } else {
          this.prescriptionList = dataRow;
        }
        this.finished = true
      }).catch(err => {
        this.finished = true
        Toast(err.message)
      })

    },


    // getPrescriptionDetail() {
    //   let queryData = {
    //     FlowNo: this.FlowNo,
    //     PatientId: this.PatientId,
    //     InsuranceType: '0'
    //   }
    //   apiopLBQuery(queryData).then(res => {
    //     let jsonObj = this.$x2js.xml2js(res.data);
    //     let jsonObj2 = this.$x2js.xml2js(
    //         jsonObj.Envelope.Body.MOP_OutpBillsDetailQueryResponse.MOP_OutpBillsDetailQueryResult
    //     );
    //     this.prescriptionList = jsonObj2.response.data.data_row
    //   }).catch(err => {
    //
    //   })
    // }
  },

};
</script>


<style scoped lang="scss">
.time_box {
  background-color: #fff;
  margin: 0.25rem;
  padding: 0.25rem;
  height: auto;
  border-radius: 0.375rem;
  box-shadow: 0 0 0.25rem #ccc;
}
</style>
