<!-- 搜索表单 -->
<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';
    import dayjs from 'dayjs';

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

    const formItems = reactive([
        { type: 'input', label: '书名', prop: 'bookName' },
        { type: 'input', label: 'ISBN', prop: 'isbn' },
        { type: 'input', label: '作者', prop: 'author' },
        { type: 'input', label: '出版社', prop: 'publish' },
        {
            type: 'daterange',
            label: '回收时间',
            prop: 'recycleDate',
            keys: ['rcNumStartTime', 'rcNumEndTime'],
            props: {
                format: 'YYYY-MM-DD',
                valueFormat: 'YYYY-MM-DD',
                onChange: (val) => {
                    searchRef.value?.setData({
                        rcNumStartTime: val && val.length > 0 ? val[0] : '',
                        rcNumEndTime: val && val.length > 0 ? val[1] : ''
                    });
                }
            }
        }
    ]);

    let rcNumStartTime = dayjs().subtract(3, 'day').format('YYYY-MM-DD');
    let rcNumEndTime = dayjs().format('YYYY-MM-DD');
    let recycleDate = [rcNumStartTime, rcNumEndTime];

    const initKeys = reactive({
        bookName: '',
        isbn: '',
        author: '',
        publish: '',
        rcNumStartTime,
        rcNumEndTime,
        recycleDate: recycleDate
    });

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