<template>
  <div class="tenant">
    <div class="content">
      <byTable
        :source="sourceList.data"
        :pagination="sourceList.pagination"
        :config="config"
        :loading="loading"
        :selectConfig="selectConfig"
        highlight-current-row
        :action-list="[
          {
            text: '导出Excel',
            action: () => deriveExcel(),
          },
        ]"
        @get-list="getList"
      >
        <template #warehouseName="{ item }">
          <div>
            <!-- <span v-if="item.opType == 1">{{ item.toWarehouseName }}</span>
            <span v-else>{{ item.warehouseName }}</span> -->
            <span>{{ item.warehouseName }}</span>
          </div>
        </template>
      </byTable>
    </div>
  </div>
</template>

<script setup>
import { ElMessage, ElMessageBox } from "element-plus";
import { computed, ref } from "vue";
import byTable from "@/components/byTable/index";
import { getToken } from "@/utils/auth";
const actionUrl = import.meta.env.VITE_APP_BASE_API;
const { proxy } = getCurrentInstance();
const deptList = ref([
  {
    label: "入库",
    value: "1",
  },
  {
    label: "出库",
    value: "2",
  },
]);
const productType = ref([]);
const warehouseList = ref([]);
const productUnit = ref([]);
const isCombination = ref([
  {
    label: "是",
    value: "1",
  },
  {
    label: "否",
    value: "0",
  },
]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    keyword: "",
    isCombination: "",
    deptId: "",
    productType: "",
  },
});
const loading = ref(false);
const selectConfig = computed(() => {
  return [
    {
      label: "项目组",
      prop: "deptId",
      data: deptList.value,
    },
    {
      label: "产品类型",
      prop: "productType",
      data: productType.value,
    },
    {
      label: "是否组合",
      prop: "isCombination",
      data: isCombination.value,
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "商品类型",
        prop: "productType",
        fixed: "left",
        width: 100,
      },
      render(type) {
        return proxy.dictValueLabel(type, productType.value);
      },
    },
    {
      attrs: {
        label: "商品编码",
        prop: "productCustomCode",
        fixed: "left",
        width: 140,
      },
    },
    {
      attrs: {
        label: "商品名称",
        prop: "productName",
        fixed: "left",
      },
    },
    {
      attrs: {
        label: "规格",
        prop: "productSpec",
        fixed: "left",
      },
    },
    {
      attrs: {
        label: "是否组合",
        prop: "isCombination",
        width: 100,
      },
      render(isCombination) {
        return isCombination == 1 ? "是" : "否";
      },
    },
    {
      attrs: {
        label: "管理部门",
        prop: "deptName",
        width: 160,
      },
    },
    {
      attrs: {
        label: "待采购数量",
        prop: "purchaseWaitQuantity",
        width: 120,
      },
    },
    {
      attrs: {
        label: "待入库数量",
        prop: "stockWaitInQuantity",
        width: 120,
      },
    },
    {
      attrs: {
        label: "待出库数量",
        prop: "stockWaitOutQuantity",
        width: 120,
      },
    },
    {
      attrs: {
        label: "可支配库存",
        prop: "disposableQuantity",
        width: 120,
      },
    },
    {
      attrs: {
        label: "可用库存",
        prop: "quantity",
        width: 120,
      },
    },
    {
      attrs: {
        label: "冻结库存",
        prop: "frozenQuantity",
        width: 120,
      },
    },
    {
      attrs: {
        label: "次品库存",
        prop: "defectiveQuantity",
        width: 120,
      },
    },
    {
      attrs: {
        label: "京东在途",
        prop: "jdInTransitQuantity",
        fixed: "right",
        width: 120,
      },
    },
    {
      attrs: {
        label: "总库存",
        prop: "totalQuantity",
        fixed: "right",
        width: 120,
      },
    },

    {
      attrs: {
        label: "结存单价",
        prop: "unitPrice",
        fixed: "right",
        width: 120,
      },
    },
    {
      attrs: {
        label: "库存金额",
        prop: "amount",
        fixed: "right",
        width: 120,
      },
    },
  ];
});
const getDict = () => {
  proxy.getDictOne(["product_type"]).then((res) => {
    productType.value = res["product_type"].map((x) => ({
      label: x.dictValue,
      value: x.dictKey,
    }));
  });
  proxy.get("/logisticsInfos/getDepts").then((res) => {
    deptList.value = res.data.map((x) => ({
      ...x,
      label: x.deptName,
      value: x.deptId,
    }));
  });
};
const getList = async (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy
    .post("/stock/stockAnalysis", sourceList.value.pagination)
    .then((res) => {
      sourceList.value.data = res.rows;
      sourceList.value.pagination.total = res.total;
      setTimeout(() => {
        loading.value = false;
      }, 200);
    });
};
getDict();
getList();

const deriveExcel = () => {
  ElMessage({
    message: "请稍后",
    type: "success",
  });
  proxy
    .postTwo("/stock/stockAnalysis/exportExcel", sourceList.value.pagination)
    .then((res) => {
      const content = res;
      const blob = new Blob([content], { type: "application/ms-excel" });
      const fileName = "库存分析.xlsx";
      if ("download" in document.createElement("a")) {
        // 非IE下载
        const elink = document.createElement("a");
        elink.download = fileName;
        elink.style.display = "none";
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        URL.revokeObjectURL(elink.href); // 释放URL 对象
        document.body.removeChild(elink);
      } else {
        navigator.msSaveBlob(blob, fileName);
      }
    });
};
</script>

<style lang="scss" scoped>
.tenant {
  padding: 20px;
}
</style>
