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

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

    const formItems = reactive([
        { type: 'input', label: '书名', prop: 'bookName' },
        { type: 'input', label: 'ISBN', prop: 'isbn' },
        {
            type: 'dictSelect',
            label: '图书类型',
            prop: 'bookTag',
            props: {
                code: 'book_tag'
            }
        },
        {
            type: 'inputNumberRange',
            label: '库存周转率',
            prop: 'turnoverRate',
            keys: ['minTurnoverRate', 'maxTurnoverRate'],
            props: {
                label: '库存周转率',
                onChange: (val) => {
                    searchRef.value?.setData({
                        minTurnoverRate: val.min,
                        maxTurnoverRate: val.max
                    });
                }
            }
        },
        {
            type: 'select',
            label: '回收状态',
            prop: 'recycleStatus',
            options: [
                { label: '正在回收', value: 1 },
                { label: '暂停回收', value: 2 },
                { label: '未回收', value: 0 }
            ]
        },
        {
            type: 'inputNumberRange',
            label: '销量',
            prop: 'sales',
            keys: ['minSales', 'maxSales'],
            props: {
                label: '销量',
                onChange: (val) => {
                    searchRef.value?.setData({
                        minSales: val.min,
                        maxSales: val.max
                    });
                }
            }
        },
        {
            type: 'select',
            label: '计算周期',
            prop: 'calcPeriod',
            options: [
                { label: '7天', value: 7 },
                { label: '15天', value: 15 },
                { label: '30天', value: 30 }
            ]
        },
        {
            type: 'inputNumberRange',
            label: '回收折扣',
            prop: 'discount',
            keys: ['minDiscount', 'maxDiscount'],
            props: {
                label: '回收折扣',
                onChange: (val) => {
                    searchRef.value?.setData({
                        minDiscount: val.min,
                        maxDiscount: val.max
                    });
                }
            }
        },
        {
            type: 'inputNumberRange',
            label: '剩余可卖天数',
            prop: 'remainingDays',
            keys: ['minRemainingDays', 'maxRemainingDays'],
            props: {
                label: '剩余可卖天数',
                onChange: (val) => {
                    searchRef.value?.setData({
                        minRemainingDays: val.min,
                        maxRemainingDays: val.max
                    });
                }
            }
        },
        {
            type: 'inputNumberRange',
            label: '库存',
            prop: 'stock',
            keys: ['minStock', 'maxStock'],
            props: {
                label: '库存',
                onChange: (val) => {
                    searchRef.value?.setData({
                        minStock: val.min,
                        maxStock: val.max
                    });
                }
            }
        },
        {
            type: 'select',
            label: '单个订单多本回收',
            prop: 'multiRecycle',
            options: [
                { label: '是', value: 1 },
                { label: '否', value: 0 }
            ]
        },
        {
            type: 'daterange',
            label: '价格变动',
            prop: 'priceChangeTime',
            keys: ['priceChangeStartTime', 'priceChangeEndTime'],
            props: {
                format: 'YYYY-MM-DD',
                valueFormat: 'YYYY-MM-DD',
                onChange: (val) => {
                    searchRef.value?.setData({
                        priceChangeStartTime:
                            val && val.length > 0 ? val[0] : '',
                        priceChangeEndTime: val && val.length > 0 ? val[1] : ''
                    });
                }
            }
        },
        {
            type: 'inputNumberRange',
            label: '价格',
            prop: 'price',
            keys: ['minPrice', 'maxPrice'],
            props: {
                label: '价格',
                onChange: (val) => {
                    searchRef.value?.setData({
                        minPrice: val.min,
                        maxPrice: val.max
                    });
                }
            }
        }
    ]);

    const initKeys = reactive({
        bookName: '',
        isbn: '',
        bookTag: '',
        minTurnoverRate: void 0,
        maxTurnoverRate: void 0,
        recycleStatus: '',
        minSales: void 0,
        maxSales: void 0,
        calcPeriod: '',
        minDiscount: void 0,
        maxDiscount: void 0,
        minRemainingDays: void 0,
        maxRemainingDays: void 0,
        minStock: void 0,
        maxStock: void 0,
        multiRecycle: '',
        priceChangeStartTime: '',
        priceChangeEndTime: '',
        minPrice: void 0,
        maxPrice: void 0
    });

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