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

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

    let { proxy } = getCurrentInstance();
    const emit = defineEmits(['search']);

    const formItems = reactive([
        { type: 'input', label: '书名', prop: 'bookName' },
        { type: 'input', label: '作者', prop: 'author' },
        {
            type: 'inputNumberRange',
            label: '售价',
            prop: 'price',
            keys: ['minPrice', 'maxPrice'],
            props: {
                label: '售价',
                onChange: (val) => {
                    searchRef.value?.setData({
                        minPrice: val.min,
                        maxPrice: val.max
                    });
                }
            }
        },
        {
            type: 'inputNumberRange',
            label: '库存',
            prop: 'stock',
            keys: ['minStock', 'maxStock'],
            props: {
                label: '库存',
                onChange: (val) => {
                    searchRef.value?.setData({
                        minStock: val.min,
                        maxStock: val.max
                    });
                }
            }
        },
        {
            type: 'input',
            label: 'ISBN',
            prop: 'isbn',
            props: {
                placeholder: "ISBN'中英文逗号、空格或换行'分割"
            },
            colProps: { span: 7 }
        }
    ]);

    const initKeys = reactive({
        bookName: '',
        isbn: '',
        author: '',
        publish: '',
        pubDateStart: '',
        pubDateEnd: '',
        minPrice: void 0,
        maxPrice: void 0,
        minDiscount: void 0,
        maxDiscount: void 0,
        searchType: 0,
        globalInDiscount: void 0,
        globalNotInDiscount: void 0
    });

    const searchRef = ref(null);
    /** 搜索 */
    const search = (data) => {
        let params = JSON.parse(JSON.stringify(data));
        delete params.price;
        delete params.discount;
        delete params.pubDate;
        emit('search', params);
    };
</script>
