<!-- 搜索表单 -->
<template>
    <ele-card :body-style="{ paddingBottom: '8px' }">
        <ProSearch
            :items="formItems"
            ref="searchRef"
            @search="search"
            :initKeys="initKeys"
        />
    </ele-card>
</template>

<script setup>
    import { reactive, ref, computed, defineEmits, onMounted } from 'vue';
    import ProSearch from '@/components/CommonPage/ProSearch2.vue';
    import request from '@/utils/request';

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

    const warehouseOptions = ref([]);

    const formItems = computed(() => [
        {
            type: 'select',
            label: '请选择仓库',
            prop: 'repositoryId',
            options: warehouseOptions.value,
            props: {
                clearable: true,
                filterable: true
            }
        },
        { type: 'input', label: 'ISBN', prop: 'isbn' },
        { type: 'input', label: '书名', prop: 'bookName' }
    ]);

    const initKeys = reactive({
        repositoryId: '',
        isbn: '',
        bookName: ''
    });

    function loadWarehouseOptions() {
        request
            .get('/activation/bookActivationCodeRepository/pagelist', {
                params: {
                    status: '1',
                    pageNum: 1,
                    pageSize: 500
                }
            })
            .then((res) => {
                if (res.data.code === 200) {
                    const list = res.data.rows || res.data.data || [];
                    warehouseOptions.value = list.map((item) => ({
                        label: item.repositoryName,
                        value: item.id
                    }));
                }
            });
    }

    onMounted(() => {
        loadWarehouseOptions();
    });

    const searchRef = ref(null);
    /** 搜索 */
    const search = (data) => {
        const opt = warehouseOptions.value.find(
            (o) => o.value == data.repositoryId
        );
        emit('search', {
            ...data,
            repositoryName: opt?.label || ''
        });
    };
</script>
