<!-- 搜索表单 -->
<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: 'input',
            label: 'ISBN',
            prop: 'isbn',
            placeholder: '请输入ISBN'
        },
        {
            type: 'input',
            label: '书名',
            prop: 'bookName',
            placeholder: '请输入书名'
        },
        {
            type: 'select',
            label: '请选择操作类型',
            prop: 'type',
            props: {
                placeholder: '请选择操作类型',
                clearable: true
            },
            options: [
                { label: '入库', value: '1' },
                { label: '出库', value: '2' }
            ]
        },
        {
            type: 'select',
            label: '请选择仓库',
            prop: 'repositoryId',
            options: warehouseOptions.value,
            props: {
                placeholder: '请选择仓库',
                clearable: true,
                filterable: true
            }
        },
        {
            type: 'input',
            label: '请输入备注信息',
            prop: 'remark',
            placeholder: '请输入备注信息'
        },
        {
            type: 'daterange',
            label: '时间',
            prop: 'timeRange',
            props: {
                valueFormat: 'YYYY-MM-DD',
                format: 'YYYY-MM-DD',
                startPlaceholder: '开始时间',
                endPlaceholder: '结束时间',
                shortcuts: [
                    {
                        text: '最近7天',
                        value: () => {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(
                                start.getTime() - 3600 * 1000 * 24 * 7
                            );
                            return [start, end];
                        }
                    },
                    {
                        text: '最近15天',
                        value: () => {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(
                                start.getTime() - 3600 * 1000 * 24 * 15
                            );
                            return [start, end];
                        }
                    },
                    {
                        text: '最近30天',
                        value: () => {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(
                                start.getTime() - 3600 * 1000 * 24 * 30
                            );
                            return [start, end];
                        }
                    }
                ],
                onChange: (value) => {
                    searchRef.value?.setData({
                        startTime: value ? value[0] : '',
                        endTime: value ? value[1] : ''
                    });
                }
            }
        }
    ]);

    const initKeys = reactive({
        startTime: '',
        endTime: '',
        timeRange: [],
        isbn: '',
        bookName: '',
        type: '',
        repositoryId: '',
        remark: ''
    });

    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) => {
        let params = { ...data };
        delete params.timeRange;
        emit('search', params);
    };
</script>
