<!-- 搜索表单 -->
<template>
  <simple-form-modal
    :title="title"
    :items="formItems"
    ref="editRef"
    :baseUrl="baseUrl"
    @success="(data) => emit('success', data)"
  ></simple-form-modal>
</template>

<script setup>
  import { reactive, ref, defineEmits, getCurrentInstance } from 'vue';
  import { useFormData } from '@/utils/use-form-data';
  import SimpleFormModal from '@/components/CommonPage/SimpleFormModal.vue';
  const { proxy } = getCurrentInstance();

  //获取仓库
  const godownList = ref([]);
  const title = ref('新增库位');
  const emit = defineEmits(['success']);

  const formItems = computed(() => {
    return [
      { type: 'input', label: '库位编号', prop: 'code', required: true },
      {
        type: 'select',
        label: '仓库名称',
        prop: 'godownId',
        required: true,
        options: godownList.value.map((d) => {
          return { label: d.godownName, value: d.id };
        }),
        props: {
          placeholder: '请选择或输入搜索',
          filterable: true
        }
      },
      {
        type: 'dictRadio',
        label: '状态',
        prop: 'useStatus',
        props: { code: 'use_status' },
        required: true
      },
      { type: 'textarea', label: '备注', prop: 'remark' }
    ];
  });
  //默认值
  const baseUrl = reactive({
    add: '/baseinfo/godownposition/add',
    update: '/baseinfo/godownposition/edit'
  });
  const formData = ref({ useStatus: '1' });

  const editRef = ref(null);

  function handleOpen(data) {
    if (data) {
      title.value = '编辑库位';
      data.useStatus = data.useStatus + '';
    } else {
      title.value = '新增库位';
    }
    let name = data?.godownName || '';
    formData.value = Object.assign(formData.value, data || {});
    getStoreList(name).then((res) => {
      godownList.value = res.data.data;
    });
    editRef.value?.handleOpen(formData.value);
  }

  function getStoreList(name = '') {
    return proxy.$http.post(`/baseinfo/godown/searchGodown?name=${name}`);
  }

  defineExpose({ handleOpen });
</script>
