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

        <slot></slot>
    </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: 'orderId' },
        { type: 'input', label: '客户昵称', prop: 'nickName' },
        {
            type: 'dictSelect',
            label: '交易类型',
            prop: 'changeType',
            props: { code: 'account_change_type' }
        },
        {
            type: 'datetimerange',
            label: '创建时间',
            prop: 'createTime',
            props: {
                valueFormat: 'YYYY-MM-DD HH:mm:ss',
                format: 'YYYY-MM-DD HH:mm:ss',
                startPlaceholder: '开始日期',
                endPlaceholder: '结束日期',
                onChange: (value) => {
                    searchRef.value?.setData({
                        createTimeStart: value ? value[0] : '',
                        createTimeEnd: value ? value[1] : ''
                    });
                }
            }
        }
    ]);

    const initKeys = reactive({
        orderId: '',
        nickName: '',
        changeType: '',
        type: '',
        createTimeStart: '',
        createTimeEnd: ''
    });

    const searchRef = ref(null);
    /** 搜索 */
    const search = (data) => {
        // 处理日期范围
        const searchData = { ...data };
        if (data.createTime && data.createTime.length === 2) {
            searchData.createTimeStart = data.createTime[0];
            searchData.createTimeEnd = data.createTime[1];
            delete searchData.createTime;
        }
        emit('search', searchData);
    };
</script>
