<template>
  <div>
    <div id="pdfDom"  style="font-family: 'msyh';">
      <div style="padding: 60px 30px; font-size: 12px !important; color: black">
        <div style="font-size: 16px; text-align: center; padding: 8px">
          <span>{{ dictValueLabel(printDetails.type, fundsType) }}审批单</span>
        </div>
        <div style="padding: 8px 0">
          <span>{{ printDetails.corporationName }}</span>
          <span style="padding-left: 32px">创建时间: {{ printDetails.createTime }}</span>
        </div>
        <div style="border: 1px solid black">
          <div style="display: flex; border-bottom: 1px solid black">
            <div style="width: 80px; border-right: 1px solid black; padding: 8px">创建人</div>
            <div style="flex: 1; padding: 8px">
              {{ dictValueLabel(printDetails.createUser, userList) }}
            </div>
          </div>
          <div style="display: flex; border-bottom: 1px solid black">
            <div style="width: 80px; border-right: 1px solid black; padding: 8px">创建人部门</div>
            <div style="flex: 1; padding: 8px">
              {{ printDetails.deptName }}
            </div>
          </div>
          <div style="display: flex; border-bottom: 1px solid black" v-if="printDetails.type !== '3'">
            <div style="width: 80px; border-right: 1px solid black; padding: 4px 8px; display: flex; align-items: center">费用明细</div>
            <div style="flex: 1">
              <div style="border-bottom: 1px solid black; display: flex">
                <div style="width: 65px; padding: 4px 8px; border-right: 1px solid black; text-align: center">费用类型</div>
                <div style="width: 65px; padding: 4px 8px; border-right: 1px solid black; text-align: center">关联合同</div>
                <div style="width: calc(100% - 270px); padding: 4px 8px; border-right: 1px solid black; text-align: center">款项说明</div>
                <div style="width: 65px; padding: 4px 8px; border-right: 1px solid black; text-align: center">货币</div>
                <div style="width: 75px; padding: 4px 8px; text-align: center">付款金额</div>
              </div>
              <template v-if="printDetails.accountRequestFundsDetailList && printDetails.accountRequestFundsDetailList.length > 0">
                <div v-for="(item, index) in printDetails.accountRequestFundsDetailList" :key="index">
                  <div
                    :style="
                      index + 1 !== printDetails.accountRequestFundsDetailList.length ? 'border-bottom: 1px solid black;  display: flex' : ' display: flex'
                    ">
                    <div style="width: 65px; padding: 4px 8px; border-right: 1px solid black; text-align: center">
                      {{ dictValueLabel(item.costType, fundsCostType) }}
                    </div>
                    <div style="width: 65px; padding: 4px 8px; border-right: 1px solid black; display: flex; align-items: center">
                      {{ item.contractCode }}
                    </div>
                    <div style="width: calc(100% - 270px); padding: 4px 8px; border-right: 1px solid black; display: flex; align-items: center">
                      {{ item.remarks }}
                    </div>
                    <div style="width: 65px; padding: 4px 8px; border-right: 1px solid black; display: flex; align-items: center">
                      {{ dictValueLabel(printDetails.currency, accountCurrency) }}
                    </div>
                    <div style="width: 75px; padding: 4px 8px; display: flex; align-items: center">
                      {{ item.amount }}
                    </div>
                  </div>
                </div>
              </template>
            </div>
          </div>
          <div style="display: flex; border-bottom: 1px solid black" v-else>
            <div style="width: 80px; border-right: 1px solid black; padding: 4px 8px; display: flex; align-items: center">费用明细</div>
            <div style="flex: 1">
              <div style="border-bottom: 1px solid black; display: flex">
                <div style="width: 80px; padding: 4px 8px; border-right: 1px solid black; text-align: center">费用类型</div>
                <div style="width: 120px; padding: 4px 8px; border-right: 1px solid black; text-align: center">关联合同</div>
                <div style="width: calc(100% - 420px); padding: 4px 8px; border-right: 1px solid black; text-align: center">款项说明</div>
                <div style="width: 60px; padding: 4px 8px; border-right: 1px solid black; text-align: center">货币</div>
                <div style="width: 80px; padding: 4px 8px; border-right: 1px solid black; text-align: center">预支金额</div>
                <div style="width: 80px; padding: 4px 8px; text-align: center">付款金额</div>
              </div>
              <template v-if="printDetails.accountRequestFundsDetailList && printDetails.accountRequestFundsDetailList.length > 0">
                <div v-for="(item, index) in printDetails.accountRequestFundsDetailList" :key="index">
                  <div
                    :style="
                      index + 1 !== printDetails.accountRequestFundsDetailList.length ? 'border-bottom: 1px solid black;  display: flex' : ' display: flex'
                    ">
                    <div style="width: 80px; padding: 4px 8px; border-right: 1px solid black; text-align: center">
                      {{ dictValueLabel(item.costType, fundsCostType) }}
                    </div>
                    <div style="width: 120px; padding: 4px 8px; border-right: 1px solid black; display: flex; align-items: center">
                      {{ item.contractCode }}
                    </div>
                    <div style="width: calc(100% - 420px); padding: 4px 8px; border-right: 1px solid black; display: flex; align-items: center">
                      {{ item.remarks }}
                    </div>
                    <div style="width: 60px; padding: 4px 8px; border-right: 1px solid black; display: flex; align-items: center">
                      {{ dictValueLabel(printDetails.currency, accountCurrency) }}
                    </div>
                    <div style="width: 80px; padding: 4px 8px; border-right: 1px solid black; display: flex; align-items: center">
                      {{ item.advanceAmount }}
                    </div>
                    <div style="width: 80px; padding: 4px 8px; display: flex; align-items: center">
                      {{ item.amount }}
                    </div>
                  </div>
                </div>
              </template>
            </div>
          </div>
          <div style="display: flex; border-bottom: 1px solid black" v-if="printDetails.type !== '3'">
            <div style="width: 80px; border-right: 1px solid black; padding: 8px">总报销金额</div>
            <div style="flex: 1; display: flex">
              <div style="width: calc(100% - 100px); padding: 8px; border-right: 1px solid black">
                {{ NumberToChinese(computeMoney("amount")) }}
              </div>
              <div style="width: 100px; padding: 8px">
                {{ computeMoney("amount") }}
              </div>
            </div>
          </div>
          <div v-else>
            <div style="display: flex; border-bottom: 1px solid black">
              <div style="width: 80px; border-right: 1px solid black; padding: 8px">核销总金额</div>
              <div style="flex: 1; display: flex">
                <div style="width: calc(100% - 80px); padding: 8px; border-right: 1px solid black">
                  {{ NumberToChinese(computeMoney("amount")) }}
                </div>
                <div style="width: 80px; padding: 8px">
                  {{ computeMoney("amount") }}
                </div>
              </div>
            </div>
            <div style="display: flex; border-bottom: 1px solid black">
              <div style="width: 80px; border-right: 1px solid black; padding: 8px">预支总金额</div>
              <div style="flex: 1; display: flex">
                <div style="width: calc(100% - 80px); padding: 8px; border-right: 1px solid black">
                  {{ NumberToChinese(computeMoney("advanceAmount")) }}
                </div>
                <div style="width: 80px; padding: 8px">
                  {{ computeMoney("advanceAmount") }}
                </div>
              </div>
            </div>
            <div style="display: flex; border-bottom: 1px solid black">
              <div style="width: 80px; border-right: 1px solid black; padding: 8px">差额 (核销 - 预支)</div>
              <div style="flex: 1; display: flex">
                <div style="width: calc(100% - 80px); padding: 8px; border-right: 1px solid black">
                  {{ NumberToChinese(computeBalance()) }}
                </div>
                <div style="width: 80px; padding: 8px">
                  {{ computeBalance() }}
                </div>
              </div>
            </div>
          </div>
          <div style="display: flex; border-bottom: 1px solid black">
            <div style="width: 80px; border-right: 1px solid black; padding: 8px">单据数量</div>
            <div style="flex: 1; padding: 8px">
              {{ printDetails.quantity }}
            </div>
          </div>
          <div style="display: flex; border-bottom: 1px solid black">
            <div style="width: 80px; border-right: 1px solid black; padding: 0 8px; display: flex; align-items: center">收款信息</div>
            <div style="flex: 1">
              <div style="border-bottom: 1px solid black; display: flex">
                <div style="width: 19%; padding: 0 8px; border-right: 1px solid black; text-align: center">支付方式</div>
                <div style="width: 27%; padding: 0 8px; border-right: 1px solid black; text-align: center">收款方户名</div>
                <div style="width: 27%; padding: 0 8px; border-right: 1px solid black; text-align: center">开户行</div>
                <div style="width: 27%; padding: 0 8px; text-align: center">收款方账号</div>
              </div>
              <div style="display: flex">
                <div style="width: 19%; padding: 0 8px; border-right: 1px solid black; display: flex; align-items: center">
                  {{ dictValueLabel(printDetails.paymentMethod, fundsPaymentMethod) }}
                </div>
                <div style="width: 27%; padding: 0 8px; border-right: 1px solid black; display: flex; align-items: center">
                  {{ printDetails.name }}
                </div>
                <div style="width: 27%; padding: 0 8px; border-right: 1px solid black; display: flex; align-items: center">
                  {{ printDetails.openingBank }}
                </div>
                <div style="width: 27%; padding: 0 8px; display: flex; align-items: center">
                  {{ printDetails.accountOpening }}
                </div>
              </div>
            </div>
          </div>
          <div style="display: flex; border-bottom: 1px solid black">
            <div style="width: 80px; border-right: 1px solid black; padding: 8px">电子发票(PDF/JPG)</div>
            <div style="flex:1; padding: 8px">
              {{ printDetails.electronicInvoiceText }}
            </div>
          </div>
          <div style="display: flex">
            <div style="width: 80px; border-right: 1px solid black; padding: 4px 8px; display: flex; align-items: center">审批流程</div>
            <div style="flex: 1">
              <template v-if="printDetails.recordList && printDetails.recordList.length > 0">
                <div v-for="(item, index) in printDetails.recordList" :key="index">
                  <div
                    :style="
                      index + 1 !== printDetails.recordList.length
                        ? 'border-bottom: 1px solid black; padding: 4px 8px; display: flex'
                        : 'padding: 4px 8px; display: flex'
                    ">
                    <div style="width: calc(100% - 120px); word-wrap: break-word">
                      <span>{{ item.nodeName }}: </span>
                      <span style="padding-left: 4px">{{ item.processedUser }}</span>
                      <span style="padding-left: 4px">{{ item.remark }}</span>
                    </div>
                    <div style="width: 120px">{{ item.processedDate }}</div>
                  </div>
                </div>
              </template>
            </div>
          </div>
        </div>
        <div style="padding-top: 16px">
          <span>打印时间: {{ presentTime }}</span>
          <span style="padding-left: 32px">打印人: {{ useUserStore().user.nickName }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import useUserStore from "@/store/modules/user";
import { ref } from "vue";
import { NumberToChinese } from "@/utils/util.js";
import moment from "moment";

const { proxy } = getCurrentInstance();
const fundsType = ref([]);
const userList = ref([]);
const companyData = ref([]);
const accountCurrency = ref([]);
const fundsCostType = ref([]);
const fundsPaymentMethod = ref([]);
const getDictData = () => {
  proxy.getDictOne(["founds_type", "funds_payment_method", "account_currency", "funds_cost_type"]).then((res) => {
    fundsType.value = res["founds_type"].map((x) => ({
      label: x.dictValue,
      value: x.dictKey,
    }));
    fundsPaymentMethod.value = res["funds_payment_method"].map((x) => ({
      label: x.dictValue,
      value: x.dictKey,
    }));
    accountCurrency.value = res["account_currency"].map((x) => ({
      label: x.dictValue,
      value: x.dictKey,
    }));
    fundsCostType.value = res["funds_cost_type"].map((x) => ({
      label: x.dictValue,
      value: x.dictKey,
    }));
  });
  proxy
    .get("/tenantUser/list", {
      pageNum: 1,
      pageSize: 10000,
      tenantId: useUserStore().user.tenantId,
    })
    .then((res) => {
      if (res.rows && res.rows.length > 0) {
        userList.value = res.rows.map((item) => {
          return {
            deptId: item.deptId,
            label: item.nickName,
            value: item.userId,
          };
        });
      }
    });
  proxy.post("/corporation/page", { pageNum: 1, pageSize: 9999 }).then((res) => {
    if (res.rows && res.rows.length > 0) {
      companyData.value = res.rows.map((item) => {
        return {
          label: item.name,
          value: item.id,
        };
      });
    }
  });
};
getDictData();
const printDetails = ref({});
const presentTime = ref(moment().format("yyyy-MM-DD HH:mm:ss"));
const computeMoney = (label) => {
  let amount = 0;
  if (printDetails.value.accountRequestFundsDetailList && printDetails.value.accountRequestFundsDetailList.length > 0) {
    for (let i = 0; i < printDetails.value.accountRequestFundsDetailList.length; i++) {
      if (printDetails.value.accountRequestFundsDetailList[i][label]) {
        amount = Number(parseFloat(Number(amount) + Number(printDetails.value.accountRequestFundsDetailList[i][label])).toFixed(2));
      }
    }
  }
  return amount;
};
const computeBalance = () => {
  let balance = 0;
  let advanceAmount = computeMoney("advanceAmount");
  let amount = computeMoney("amount");
  if (amount) {
    balance = Number(amount);
  }
  if (advanceAmount) {
    balance = Number(parseFloat(Number(balance) - Number(advanceAmount)).toFixed(2));
  }
  return balance;
};
const props = defineProps({
  rowData: Object,
});
onMounted(() => {
  if (props.rowData && props.rowData.id) {
    proxy.post("/accountRequestFunds/detail", { id: props.rowData.id }).then((res) => {
      printDetails.value = res;
      proxy.post("/fileInfo/getList", { businessIdList: [props.rowData.id] }).then((resFile) => {
        let electronicInvoiceText = "";
        if (resFile[props.rowData.id] && resFile[props.rowData.id].length > 0) {
          for (let i = 0; i < resFile[props.rowData.id].length; i++) {
            if (i === 0) {
              electronicInvoiceText = resFile[props.rowData.id][0].fileName;
            } else {
              electronicInvoiceText = electronicInvoiceText + ", " + resFile[props.rowData.id][i].fileName;
            }
          }
        }
        printDetails.value.electronicInvoiceText = electronicInvoiceText;
      });
      if (res.flowInfoId) {
        proxy.post("/flowExample/getApprovalRecord", { id: res.flowInfoId }).then((record) => {
          printDetails.value.recordList = record.recordList;
        });
      }
    });
  }
});
</script>

<style lang="scss" scoped></style>
