<!-- 操作日志搜索表单 -->
<template>
    <ProSearch
        :items="formItems"
        ref="searchRef"
        @search="search"
        :initKeys="initKeys"
    />
</template>

<script setup>
    import { reactive, ref, defineEmits } from 'vue';
    import ProSearch from '@/components/CommonPage/ProSearch2.vue';

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

    const formItems = reactive([
        { type: 'input', label: '任务ID', prop: 'id', colProps: { span: 6 } },
        {
            type: 'select',
            label: '任务类型',
            prop: 'taskType',
            colProps: { span: 6 },
            options: [
                { label: '更新价格', value: 1 },
                { label: '上下架', value: 2 }
            ]
        },
        {
            type: 'select',
            label: '任务状态',
            prop: 'taskStatus',
            colProps: { span: 6 },
            options: [
                { label: '进行中', value: 0 },
                { label: '已完成', value: 1 },
                { label: '失败', value: 2 }
            ]
        },
        {
            type: 'input',
            label: '操作人',
            prop: 'createName',
            colProps: { span: 6 }
        },
        {
            type: 'datetimerange',
            label: '创建时间',
            prop: 'createTimeRange',
            colProps: { span: 8 },
            keys: ['createTimeStart', 'createTimeEnd'],
            props: {
                valueFormat: 'YYYY-MM-DD HH:mm:ss',
                startPlaceholder: '开始时间',
                endPlaceholder: '结束时间',
                onChange: (val) => {
                    searchRef.value?.setData({
                        createTimeStart: val && val.length > 0 ? val[0] : '',
                        createTimeEnd: val && val.length > 0 ? val[1] : ''
                    });
                }
            }
        },
        {
            type: 'datetimerange',
            label: '完成时间',
            prop: 'finishTimeRange',
            colProps: { span: 8 },
            keys: ['finishTimeStart', 'finishTimeEnd'],
            props: {
                valueFormat: 'YYYY-MM-DD HH:mm:ss',
                startPlaceholder: '开始时间',
                endPlaceholder: '结束时间',
                onChange: (val) => {
                    searchRef.value?.setData({
                        finishTimeStart: val && val.length > 0 ? val[0] : '',
                        finishTimeEnd: val && val.length > 0 ? val[1] : ''
                    });
                }
            }
        }
    ]);

    const initKeys = reactive({
        id: '',
        taskType: '',
        taskStatus: '',
        createName: '',
        createTimeRange: [],
        createTimeStart: '',
        createTimeEnd: '',
        finishTimeRange: [],
        finishTimeStart: '',
        finishTimeEnd: ''
    });

    const searchRef = ref(null);

    const search = (data) => {
        let params = JSON.parse(JSON.stringify(data));
        delete params.createTimeRange;
        delete params.finishTimeRange;
        emit('search', params);
    };

    const reset = () => {
        searchRef.value?.reset();
    };

    defineExpose({
        reset,
        setData: (data) => searchRef.value?.setData(data)
    });
</script>
