<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 { ElMessageBox } from "element-plus";

const { proxy } = getCurrentInstance();
const departmentList = ref([{ dictKey: "0", dictValue: "胜德体育" }]);
const warehouseList = ref([]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    type: "",
    code: "",
    bomSpecCode: "",
    bomSpecName: "",
    departmentId: "",
    warehouseName: "",
    detailType: "",
    beginTime: "",
    endTime: "",
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "code",
      label: "单号",
    },
    {
      type: "input",
      prop: "bomSpecCode",
      label: "品号",
    },
    {
      type: "input",
      prop: "bomSpecName",
      label: "品名",
    },
    {
      type: "select",
      prop: "departmentId",
      data: departmentList.value,
      label: "事业部",
    },
    {
      type: "select",
      prop: "warehouseId",
      data: warehouseList.value,
      label: "仓库",
    },
    {
      type: "select",
      prop: "type",
      data: [
        {
          dictKey: 1,
          dictValue: "入库",
        },
        {
          dictKey: 0,
          dictValue: "出库",
        },
      ],
      label: "出入库",
    },
    sourceList.value.pagination.type === 1
      ? {
          type: "select",
          prop: "detailType",
          dictKey: "put_stock_type",
          label: "入库类型",
        }
      : {},
    sourceList.value.pagination.type === 0
      ? {
          type: "select",
          prop: "detailType",
          dictKey: "come_stock_type",
          label: "出库类型",
        }
      : {},
    {
      type: "date",
      propList: ["beginTime", "endTime"],
      label: "日期",
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "出入库单号",
        prop: "code",
        width: 160,
      },
    },
    {
      attrs: {
        label: "归属事业部",
        prop: "departmentName",
        width: 120,
      },
    },
    {
      attrs: {
        label: "仓库类型",
        prop: "warehouseType",
        width: 120,
      },
      render(val) {
        return proxy.dictKeyValue(val, proxy.useUserStore().allDict["warehouse_type"]);
      },
    },
    {
      attrs: {
        label: "出入库类型",
        prop: "detailType",
        width: 120,
      },
      render(val, row) {
        if (row.type === 1) {
          return proxy.dictKeyValue(val, proxy.useUserStore().allDict["put_stock_type"]);
        } else {
          return proxy.dictKeyValue(val, proxy.useUserStore().allDict["come_stock_type"]);
        }
      },
    },
    {
      attrs: {
        label: "品号",
        prop: "bomSpecCode",
        width: 160,
      },
    },
    {
      attrs: {
        label: "品名",
        prop: "bomSpecName",
        "min-width": 240,
      },
    },
    {
      attrs: {
        label: "出入库数量",
        prop: "quantity",
        width: 100,
      },
    },
    {
      attrs: {
        label: "库存数量",
        prop: "balanceInventoryQuantity",
        width: 120,
      },
    },
    {
      attrs: {
        label: "操作时间",
        prop: "createTime",
        width: 160,
        align: "center",
      },
    },
    {
      attrs: {
        label: "操作人",
        prop: "applicant",
        width: 150,
        align: "center",
      },
    },
  ];
});
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("/inOutStorageBom/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 getDemandData = () => {
  proxy.post("/department/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    if (res.rows && res.rows.length > 0) {
      departmentList.value = departmentList.value.concat(
        res.rows.map((item) => {
          return {
            dictKey: item.id,
            dictValue: item.name,
          };
        })
      );
    }
  });
  proxy.post("/warehouse/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    if (res.rows && res.rows.length > 0) {
      warehouseList.value = res.rows.map((item) => {
        return {
          dictKey: item.id,
          dictValue: item.name,
        };
      });
    }
  });
};
getDemandData();
const deriveExcel = () => {
  ElMessageBox.confirm("你是否确认此操作", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      proxy.postFile("/inOutStorageBom/exportExcelSummary", sourceList.value.pagination).then((res) => {
        proxy.downloadFile(res, "出入库流水.xlsx");
      });
    })
    .catch(() => {});
};
</script>

<style lang="scss" scoped>
::v-deep(.el-input-number .el-input__inner) {
  text-align: left;
}
</style>
