<template>
  <el-card v-loading="loading">
    <div style="height: calc(100vh - 235px); overflow-y: auto; overflow-x: hidden">
      <el-table :data="tableData" border :row-style="{ height: '35px' }" header-row-class-name="tableHeader" show-summary :summary-method="getSummaries">
        <el-table-column align="center">
          <template #header>
            <div style="text-align: center; font-size: 30px; padding: 8px">{{ props.rowData.departmentName }}-胜德体育对账单</div>
            <div style="text-align: center; font-size: 18px; padding-bottom: 8px">( 对账时间: {{ rowData.timePeriod }} )</div>
          </template>
          <el-table-column label="SKU品号" prop="skuSpecCode" width="180" />
          <el-table-column label="SKU品名" prop="skuSpecName" min-width="220" />
          <el-table-column label="数量（PCS）" align="center" prop="quantity" width="160" />
          <el-table-column label="SKU单价" align="center" prop="unitPrice" width="160" />
          <el-table-column label="小计" align="center" width="180">
            <template #default="{ row }">
              {{ moneyFormat(row.subtotal) }}
            </template>
          </el-table-column>
          <el-table-column label="合计" align="center" width="180">
            <template #default="{ row }">
              {{ moneyFormat(row.total) }}
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>
    </div>
    <div style="padding: 4px; text-align: center">
      <el-button @click="clickCancel">关 闭</el-button>
      <el-button type="primary" @click="deriveExcel()" v-preReClick>导 出</el-button>
    </div>
  </el-card>
</template>

<script setup>
import { ElMessage } from "element-plus";

const { proxy } = getCurrentInstance();
const props = defineProps({
  rowData: Object,
  tabValues: Object,
});
const loading = ref(false);
const tableData = ref([]);
const tabsCard = ref();
watch(
  () => props.tabValues,
  (val) => {
    if (val.activeName === "sku" && val.tabsCard != tabsCard.value) {
      tabsCard.value = val.tabsCard;
      if (props.rowData && props.rowData.id) {
        loading.value = true;
        proxy.post("/statementOfAccount/getDocumentBySku", { id: props.rowData.id, orderClassify: props.tabValues.tabsCard }).then(
          (res) => {
            tableData.value = Object.freeze(res);
            loading.value = false;
          },
          (err) => {
            console.log(err);
            loading.value = false;
          }
        );
      }
    }
  },
  {
    deep: true,
    immediate: true,
  }
);
const labelList = ref({
  2: "quantity",
  5: "total",
});
const textList = ref({
  1: "胜德体育总经理:",
  2: "申请日期:",
});
const getSummaries = ({ columns, data }) => {
  const sums = [];
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = h("div", { class: "" }, [
        h("div", {
          style: { "text-align": "center", "border-style": "solid", "border-width": "0 1px 1px 0", "border-color": "#ebeef5", padding: "0 12px" },
          innerHTML: "总计:",
        }),
        h("div", {
          style: { "text-align": "center", "border-style": "solid", "border-width": "0 1px 1px 0", "border-color": "#ebeef5", padding: "0 12px" },
          innerHTML: "备注:",
        }),
        h("div", {
          style: { "text-align": "center", "border-style": "solid", "border-width": "0 1px 1px 0", "border-color": "#ebeef5", padding: "0 12px" },
          innerHTML: "交货地点:",
        }),
        h("div", { style: { "font-size": "16px", "font-weight": 700, padding: "0 12px" }, innerHTML: "申请人:" }),
      ]);
      return;
    } else {
      sums[index] = h("div", { class: "" }, [
        [1, 3, 4].includes(index)
          ? h("div", {
              style: {
                "text-align": "center",
                "border-style": "solid",
                "border-width": "0 1px 1px 0",
                "border-color": "#ebeef5",
                color: "#ebeef5",
                padding: "0 12px",
                height: "35px",
              },
              innerHTML: ".",
            })
          : h("div", {
              style: {
                "text-align": "center",
                "border-style": "solid",
                "border-width": "0 1px 1px 0",
                "border-color": "#ebeef5",
                padding: "0 12px",
                height: "35px",
              },
              innerHTML: getTotal(labelList.value[index]),
            }),
        h("div", {
          style: {
            "text-align": "center",
            "border-style": "solid",
            "border-width": "0 0 1px 0",
            "border-color": "#ebeef5",
            color: "#ebeef5",
            padding: "0 12px",
            height: "35px",
          },
          innerHTML: ".",
        }),
        h("div", {
          style: {
            "text-align": "center",
            "border-style": "solid",
            "border-width": "0 0 1px 0",
            "border-color": "#ebeef5",
            color: "#ebeef5",
            padding: "0 12px",
            height: "35px",
          },
          innerHTML: ".",
        }),
        [3, 4, 5].includes(index)
          ? h("div", {
              style: {
                "text-align": "center",
                "border-style": "solid",
                "border-width": "0 0 1px 0",
                "border-color": "#ebeef5",
                color: "#ebeef5",
                padding: "0 12px",
                height: "35px",
              },
              innerHTML: ".",
            })
          : h("div", { style: { "font-size": "16px", "font-weight": 700, padding: "0 12px", "text-align": "left" }, innerHTML: textList.value[index] }),
      ]);
      return;
    }
  });
  return sums;
};
const getTotal = (label) => {
  let list = tableData.value.map((item) => item[label]);
  return Number(Math.round(list.reduce((acc, curr) => acc + curr, 0) * 100) / 100);
};
const emit = defineEmits(["clickCancel"]);
const clickCancel = () => {
  emit("clickCancel", "");
};
const deriveExcel = () => {
  ElMessage("导出文件中,请稍后");
  loading.value = true;
  proxy.getFile("/statementOfAccount/exportDocumentBySku", { id: props.rowData.id }).then(
    (res) => {
      proxy.downloadFile(res, "SKU对账单.xlsx");
      loading.value = false;
    },
    (err) => {
      console.log(err);
      loading.value = false;
    }
  );
};
</script>

<style lang="scss" scoped>
::v-deep(.el-table thead.is-group th.el-table__cell) {
  background-color: white !important;
}
::v-deep(.el-table__footer-wrapper tbody td.el-table__cell) {
  background-color: white !important;
}
::v-deep(.el-table .el-table__cell) {
  padding: 0;
}
::v-deep(.el-table__footer .cell) {
  padding: 0;
}
::v-deep(.el-table__footer .el-table__cell) {
  border-right: 0px;
}
::v-deep(.el-card__body) {
  padding: 10px !important;
}
</style>
