<!-- 搜索表单 -->
<template>
    <simple-form-modal
        title="设置仓库"
        :items="formItems"
        ref="editRef"
        :baseUrl="baseUrl"
        isMerge
        idKey="sysUserId"
    />
</template>

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

    const emit = defineEmits(['success']);

    const formItems = computed(() => {
        return [
            {
                type: 'select',
                label: '默认仓库',
                prop: 'godownIds',
                required: true,
                props: {
                    multiple: true,
                    filterable: true
                },
                options: godownList.value
                    .filter((v) => v.useStatus == 1)
                    .map((d) => {
                        return { label: d.godownName, value: d.id };
                    })
            }
        ];
    });

    const godownList = ref([]);
    async function getGodownList() {
        return proxy.$http.post('/baseinfo/godown/searchGodown?name=');
    }

    //默认值
    const baseUrl = reactive({
        add: '/baseinfo/godown/setSysUserGodown',
        update: '/baseinfo/godown/setSysUserGodown'
    });
    const formData = ref({
        godownIds: '',
        sysUserId: ''
    });
    //获取用户已经设置的仓库
    async function getUserGodown(userId) {
        return proxy.$http.get(
            '/baseinfo/godown/getSysUserGodown?userId=' + userId
        );
    }

    const editRef = ref(null);

    async function handleOpen(data) {
        formData.value.sysUserId = data.userId;
        let res = await getUserGodown(data.userId);
        if (res.data.code == 200) {
            let item = res.data.data;
            formData.value.godownIds =
                item && item.length > 0 ? item.map((v) => v.id) : '';
        }
        editRef.value?.handleOpen(formData.value);

        getGodownList().then((res) => {
            if (res.data.code == 200) {
                godownList.value = res.data.data;
            }
        });
    }

    defineExpose({ handleOpen });
</script>
