<!-- 搜索表单 -->
<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, getCurrentInstance } from 'vue';
    import ProSearch from '@/components/CommonPage/ProSearch2.vue';

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

    const formItems = reactive([
        { type: 'input', label: '书名', prop: 'bookName' },
        { type: 'input', label: '作者', prop: 'author' },
        { type: 'input', label: '出版社', prop: 'publish' },
        {
            type: 'inputNumberRange',
            prop: 'priceRange',
            keys: ['productPriceMin', 'productPriceMax'],
            label: '售价',
            props: {
                label: '售价',
                onChange: (val) => {
                    searchRef.value?.setData({
                        productPriceMin: val.min,
                        productPriceMax: val.max
                    });
                }
            }
        },
        {
            type: 'inputNumberRange',
            prop: 'stockRange',
            keys: ['stockMin', 'stockMax'],
            label: '库存',
            props: {
                label: '库存',
                onChange: (val) => {
                    searchRef.value?.setData({
                        stockMin: val.min,
                        stockMax: val.max
                    });
                }
            }
        },
        {
            type: 'daterange',
            label: '价格变动',
            prop: 'updateTimeRange',
            keys: ['updateTimeStart', 'updateTimeEnd'],
            props: {
                format: 'YYYY-MM-DD',
                valueFormat: 'YYYY-MM-DD',
                startPlaceholder: '开始时间',
                endPlaceholder: '结束时间',
                onChange: (val) => {
                    searchRef.value?.setData({
                        updateTimeStart: val && val.length > 0 ? val[0] : '',
                        updateTimeEnd: val && val.length > 0 ? val[1] : ''
                    });
                }
            }
        },
        {
            type: 'daterange',
            label: '上架时间',
            prop: 'onSaleTimeRange',
            keys: ['onSaleTimeStart', 'onSaleTimeEnd'],
            props: {
                format: 'YYYY-MM-DD',
                valueFormat: 'YYYY-MM-DD',
                startPlaceholder: '开始时间',
                endPlaceholder: '结束时间',
                onChange: (val) => {
                    searchRef.value?.setData({
                        onSaleTimeStart: val && val.length > 0 ? val[0] : '',
                        onSaleTimeEnd: val && val.length > 0 ? val[1] : ''
                    });
                }
            }
        },
        {
            type: 'input',
            label: 'ISBN',
            prop: 'isbnStr',
            props: { placeholder: 'ISBN (英文逗号分割)' },
            colProps: { span: 8 }
        },
        {
            type: 'select',
            label: '销售状态',
            prop: 'sellStatus',
            options: [
                { label: '全部', value: '' },
                { label: '待上架', value: '0' },
                { label: '出售中', value: '1' },
                { label: '已下架(仓库中)', value: '2' },
                { label: '黑名单', value: '3' }
            ]
        }
    ]);

    const initKeys = reactive({
        bookName: '',
        author: '',
        publish: '',
        productPriceMin: '',
        productPriceMax: '',
        stockMin: '',
        stockMax: '',
        updateTimeStart: '',
        updateTimeEnd: '',
        updateTimeRange: [],
        onSaleTimeStart: '',
        onSaleTimeEnd: '',
        onSaleTimeRange: [],
        isbnStr: '',
        sellStatus: ''
    });

    const searchRef = ref(null);
    /** 搜索 */
    const search = (data) => {
        let params = JSON.parse(JSON.stringify(data));
        delete params.updateTimeRange;
        delete params.onSaleTimeRange;
        delete params.priceRange;
        delete params.stockRange;

        // 处理 ISBN
        if (params.isbnStr) {
            // 按逗号(中英文)、空格、换行符分割，然后用英文逗号连接
            params.isbnStr = params.isbnStr
                .split(/[,，\s\n]+/)
                .filter((item) => item.trim() !== '')
                .join(',');
        }

        emit('search', params);
    };

    // 暴露 reset 方法给父组件
    const reset = () => {
        searchRef.value?.reset();
    };

    defineExpose({
        reset
    });
</script>
