<template>
  <van-nav-bar
    title="库存查询"
    left-text=""
    left-arrow
    @click-left="onClickLeft"
  >
  </van-nav-bar>
  <van-search
    v-model="req.keyword"
    placeholder="请输入搜索关键词"
    @search="onRefresh"
  />
  <van-tabs v-model:active="active" @change="handleChangeTab">
    <van-tab title="全部" name="all"></van-tab>
    <van-tab
      v-for="(item, index) in warehouseData"
      :title="item.name"
      :name="item.id"
    >
    </van-tab>
  </van-tabs>
  <van-pull-refresh v-model="loading" @refresh="onRefresh">
    <div class="list">
      <van-list
        v-model:loading="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        style="margin-bottom: 60px"
      >
        <commonList
          :data="listData"
          @onClick="toDtl"
          :config="listConfig"
        ></commonList>
      </van-list>
    </div>
  </van-pull-refresh>
</template>
<script setup>
import { ref, getCurrentInstance, onMounted } from "vue";
import commonList from "@/components/common-list.vue";
import { useRoute } from "vue-router";
const loading = ref(false);
const router = useRoute();
const req = ref({
  pageNum: 1,
  id: "",
  keyword: null,
});
const finished = ref(false);
const proxy = getCurrentInstance().proxy;
const listData = ref([]);
const warehouseData = ref([]);

const active = ref("all");
const listConfig = ref([
  {
    label: "物品类型",
    prop: "type",
  },
  {
    label: "物品名称",
    prop: "productName",
  },
  {
    label: "库存数量",
    prop: "quantity",
  },
]);
const onRefresh = () => {
  req.value.pageNum = 1;
  finished.value = false;
  getList("refresh");
};
const onLoad = () => {
  getList();
};

const onClickLeft = () => proxy.$router.push("/main/working");

const onClickRight = () => {
  proxy.$router.push("/main/manualInboundAdd");
};

const toDtl = (row) => {
  proxy.$router.push({
    path: "outInList",
    query: {
      productId: row.productId,
      warehouseId: row.warehouseId,
    },
  });
};

const getList = (type) => {
  loading.value = true;
  proxy
    .post("/stock/page", req.value)
    .then((res) => {
      listData.value =
        type === "refresh"
          ? res.data.rows
          : listData.value.concat(res.data.rows);
      if (req.value.pageNum * 10 >= res.data.total) {
        finished.value = true;
      }
      req.value.pageNum++;
      loading.value = false;
      console.log(listData.value, "wssa");
    })
    .catch((err) => {
      loading.value = false;
    });
};

const getWarehouseData = () => {
  proxy.post("/warehouse/page", { pageNum: 1, pageSize: 9999 }).then((res) => {
    warehouseData.value = res.data.rows;
  });
};

const handleChangeTab = () => {
  req.value.id = active.value === "all" ? "" : active.value;
  req.value.pageNum = 1;
  listData.value = [];
  getList();
};
getWarehouseData();
getList();
</script>

<style lang="scss" scoped>
.list {
  min-height: 70vh;
}
</style>