<template>
  <div>
    <el-card class="box-card">
      <byTable
        :source="sourceList.data"
        :pagination="sourceList.pagination"
        :config="config"
        :loading="loading"
        :searchConfig="searchConfig"
        highlight-current-row
        :action-list="[
          {
            text: '导出Excel',
            action: () => deriveExcel(),
          },
        ]"
        :tableHeight="'calc(100vh - 297px)'"
        @get-list="getList"
        @clickReset="clickReset"
        :cellClassName="cellClassName">
        <template #purchaseCode="{ item }">
          <div>
            <a style="color: #409eff; cursor: pointer; word-break: break-all" @click="clickPurchaseCode(item)">{{ item.purchaseCode }}</a>
          </div>
        </template>
      </byTable>
    </el-card>
  </div>
</template>

<script setup>
import byTable from "/src/components/byTable/index";
import moment from "moment";
import { ElMessage, ElMessageBox } from "element-plus";

const { proxy } = getCurrentInstance();
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    purchaseCode: "",
    bomSpecCode: "",
    bomSpecName: "",
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "purchaseCode",
      label: "采购单号",
    },
    {
      type: "input",
      prop: "bomSpecCode",
      label: "品号",
    },
    {
      type: "input",
      prop: "bomSpecName",
      label: "品名",
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "品号",
        prop: "bomSpecCode",
        width: 140,
      },
    },
    {
      attrs: {
        label: "品名",
        prop: "bomSpecName",
        "min-width": 220,
      },
    },
    {
      attrs: {
        label: "采购单号",
        slot: "purchaseCode",
        width: 160,
      },
    },
    {
      attrs: {
        label: "E10采购单号",
        prop: "purchaseErpCode",
        width: 160,
      },
    },
    {
      attrs: {
        label: "供应商",
        prop: "supplierName",
        width: 220,
      },
    },
    {
      attrs: {
        label: "采购数量",
        prop: "purchaseQuantity",
        width: 90,
      },
    },
    {
      attrs: {
        label: "在途数量",
        prop: "inTransitQuantity",
        width: 90,
      },
    },
    {
      attrs: {
        label: "交期",
        prop: "deliveryDate",
        width: 160,
        align: "center",
      },
    },
    {
      attrs: {
        label: "30天销量",
        prop: "salesQuantityThirtyDays",
        width: 90,
      },
    },
    {
      attrs: {
        label: "60天销量",
        prop: "salesQuantitySixtyDays",
        width: 90,
      },
    },
    {
      attrs: {
        label: "90天销量",
        prop: "salesQuantity",
        width: 90,
      },
    },
    {
      attrs: {
        label: "90天周转次数",
        prop: "turnoverRate",
        width: 150,
      },
    },
    {
      attrs: {
        label: "库存数量",
        prop: "inventoryQuantity",
        width: 90,
      },
    },
  ];
});
const getList = async (req, status) => {
  if (status) {
    sourceList.value.pagination = {
      pageNum: sourceList.value.pagination.pageNum,
      pageSize: sourceList.value.pagination.pageSize,
    };
  } else {
    sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  }
  loading.value = true;
  proxy.post("/purchaseInTransitBom/page", sourceList.value.pagination).then((res) => {
    sourceList.value.data = res.rows;
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
getList();
const clickReset = () => {
  getList("", true);
};
const deriveExcel = () => {
  ElMessageBox.confirm("你是否确认此操作", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      proxy.postFile("/purchaseInTransitBom/export", sourceList.value.pagination).then((res) => {
        if (res.type === "application/json") {
          const fileReader = new FileReader();
          fileReader.onloadend = () => {
            const jsonData = JSON.parse(fileReader.result);
            ElMessage({ message: jsonData.msg, type: "error" });
          };
          fileReader.readAsText(res);
        } else {
          proxy.downloadFile(res, "在途物料-" + moment().format("yyyy-MM-DD") + ".xlsx");
        }
      });
    })
    .catch(() => {});
};
const cellClassName = (val) => {
  if (val.row.deliveryDate < moment().format("yyyy-MM-DD HH:mm:ss")) {
    return "colorDim";
  }
};
const clickPurchaseCode = (item) => {
  proxy.$router.replace({
    path: "/platform_manage/process/processApproval",
    query: {
      flowKey: "purchase",
      flowName: "采购流程",
      processType: "20",
      id: item.purchaseId,
      flowId: item.flowId,
      random: proxy.random(),
    },
  });
};
</script>

<style lang="scss" scoped>
::v-deep(.colorDim) {
  color: red;
}
</style>
