<template>
  <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(),
        },
      ]"
      @get-list="getList"
      @clickReset="clickReset">
    </byTable>
  </el-card>
</template>

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

const { proxy } = getCurrentInstance();
const type = ref([
  {
    dictKey: "1",
    dictValue: "制作损坏",
  },
  {
    dictKey: "2",
    dictValue: "裸垫质量不良",
  },
  {
    dictKey: "3",
    dictValue: "生产错误",
  },
  {
    dictKey: "4",
    dictValue: "丢件",
  },
  {
    dictKey: "5",
    dictValue: "补单",
  },
]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    type: "",
    orderCode: "",
    orderWlnCode: "",
    skuSpecCode: "",
    skuSpecName: "",
    beginTime: "",
    endTime: "",
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "select",
      prop: "type",
      data: type.value,
      label: "超领类型",
    },
    {
      type: "input",
      prop: "orderCode",
      label: "订单号",
    },
    {
      type: "input",
      prop: "orderWlnCode",
      label: "万里牛单号",
    },
    {
      type: "input",
      prop: "skuSpecCode",
      label: "SKU品号",
    },
    {
      type: "input",
      prop: "skuSpecName",
      label: "SKU品名",
    },
    {
      type: "date",
      propList: ["beginTime", "endTime"],
      label: "日期",
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "订单号",
        prop: "orderCode",
        width: 200,
      },
    },
    {
      attrs: {
        label: "万里牛单号",
        prop: "orderWlnCode",
        width: 150,
      },
    },
    {
      attrs: {
        label: "SKU品号",
        prop: "skuSpecCode",
        width: 160,
      },
    },
    {
      attrs: {
        label: "SKU品名",
        prop: "skuSpecName",
        "min-width": 220,
      },
    },
    {
      attrs: {
        label: "超领类型",
        prop: "typeValue",
        width: 130,
      },
    },
    {
      attrs: {
        label: "仓库名称",
        prop: "warehouseName",
        width: 150,
      },
    },
    {
      attrs: {
        label: "超领数量",
        prop: "quantity",
        width: 120,
      },
    },
    {
      attrs: {
        label: "超领时间",
        prop: "exceedReceiveTime",
        width: 160,
      },
    },
  ];
});
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("/productionExceedReceive/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("/productionExceedReceive/exceedReceiveExportExcel", 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, "SKU报价看板-" + moment().format("yyyy-MM-DD") + ".xlsx");
        }
      });
    })
    .catch(() => {});
};
</script>

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