<script setup lang="ts">
import { Events } from "~/dim/event";
const props = defineProps<{ shopId: number |undefined}>();
const emit = defineEmits([Events.input, Events.change, Events.reset]);
import { DefaultQuery, pageSizes } from "~/domain/query";
import { useShop } from "~/api/shop/shopService";
const { shopWarehouseList } = useShop();
import {type warehouseList} from "~/domain/Shop";
import {type warehouseListQuery} from "~/api/shop/shopQuery";
const defaultQuery = () => {
  const q = {} as warehouseListQuery;
  q.sellerShopId = props.shopId;
  q.type = 'SALES_WAREHOUSE';
  q.forceUpdate = "yes";
  return q;
};
const query: Ref<warehouseListQuery> = ref(defaultQuery());
const warehouseList: Ref<warehouseList[]> = ref([]);
function handleChange(val: any) {
  emit(Events.input, val);
  emit(Events.change, val);
}

async function shopWarehouseListFun() {
  let res: any = await shopWarehouseList(query.value);
  if (res.code == 200) {
    let rows = res.data;
    warehouseList.value = rows;
    emit(Events.reset, warehouseList.value);
  }
}
watch(
  () => props.shopId,
  async (first: any, second: any) => {
    query.value.sellerShopId = props.shopId
    shopWarehouseListFun();
  }
);

onMounted(() => {
  setTimeout(() => {
    if(props.shopId) shopWarehouseListFun();
  }, 30);
});
</script>

<template>
  <el-select filterable placeholder="请选择仓库" @change="handleChange">
    <el-option v-for="item in warehouseList" :key="item.warehouseId" :label="item.name" :value="item.warehouseId">
      {{ item.name }}
    </el-option>
  </el-select>
</template>
