<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="[
          selectStatus
            ? {}
            : {
                text: '导出Excel',
                action: () => deriveExcel(),
              },
        ]"
        @get-list="getList"
      >
        <template #btn="{ item }">
          <div style="width: 100%; text-align: center">
            <el-button
              type="primary"
              @click="checkTheFlow(item)"
              v-if="!selectStatus"
              text
              >查看流水</el-button
            >
            <el-button type="primary" @click="clickSelect(item)" v-else text
              >选择</el-button
            >
          </div>
        </template>
      </byTable>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from "vue";
import byTable from "@/components/byTable/index";
import useUserStore from "@/store/modules/user";

const { proxy } = getCurrentInstance();
const props = defineProps({
  selectStatus: Boolean,
  warehouseId: String,
});
const warehouseList = ref([]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    keyword: "",
    id: "",
  },
});
const productUnit = ref([]);
const definition = ref([
  {
    label: "产品",
    value: "1",
  },
  {
    label: "物料",
    value: "2",
  },
]);
const loading = ref(false);
const selectConfig = computed(() => {
  if (props.warehouseId) {
    return [];
  }
  return [
    {
      label: "仓库名称",
      prop: "id",
      data: warehouseList.value,
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "仓库名称",
        prop: "warehouseId",
        width: 220,
      },
      render(type) {
        return proxy.dictValueLabel(type, warehouseList.value);
      },
    },
    {
      attrs: {
        label: "物品类型",
        prop: "productDefinition",
        width: 140,
      },
      render(type) {
        return proxy.dictValueLabel(type, definition.value);
      },
    },
    {
      attrs: {
        label: "所属分类",
        prop: "productClassifyName",
        width: 140,
      },
    },
    {
      attrs: {
        label: "物品编码",
        prop: "productCode",
        width: 160,
      },
    },
    {
      attrs: {
        label: "物品名称",
        prop: "productName",
        "min-width": 180,
      },
    },
    {
      attrs: {
        label: "规格型号",
        prop: "productSpec",
        width: 160,
      },
    },
    {
      attrs: {
        label: "单位",
        prop: "productUnit",
        width: 120,
      },
      render(unit) {
        return proxy.dictDataEcho(unit, productUnit.value);
      },
    },
    {
      attrs: {
        label: "库存数量",
        prop: "quantity",
        width: 140,
      },
    },
    {
      attrs: {
        label: "操作",
        slot: "btn",
        width: 120,
        fixed: "right",
        align: "center",
      },
    },
  ];
});
const getDict = () => {
  proxy.post("/warehouse/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    if (res.rows && res.rows.length > 0) {
      warehouseList.value = res.rows.map((item) => {
        return {
          label: item.name,
          value: item.id,
        };
      });
    }
  });
  proxy.getDict(["unit"]).then((res) => {
    productUnit.value = res["unit"];
  });
  // proxy
  //   .post("/dictTenantData/page", {
  //     pageNum: 1,
  //     pageSize: 999,
  //     dictCode: "product_type",
  //     tenantId: useUserStore().user.tenantId,
  //   })
  //   .then((res) => {
  //     if (res.rows && res.rows.length > 0) {
  //       productType.value = res.rows.map((item) => {
  //         return {
  //           label: item.dictValue,
  //           value: item.dictKey,
  //         };
  //       });
  //     }
  //   });
};
const getList = async (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  if (props.warehouseId) {
    sourceList.value.pagination.id = props.warehouseId;
  }
  proxy.post("/stock/page", sourceList.value.pagination).then((res) => {
    sourceList.value.data = res.rows;
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
getDict();
getList();
const checkTheFlow = (item) => {
  proxy.$router.replace({
    path: "/purchaseSales/outAndInWarehouse/record",
    query: {
      productId: item.productId,
    },
  });
};
const deriveExcel = () => {
  console.log("deriveExcel");
};
const clickSelect = (item) => {
  proxy.$emit("select", item);
};
</script>

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