<template>
    <el-date-picker
        v-model="content"
        :type="pickerType"
        range-separator="-"
        :start-placeholder="startPlaceholder"
        :end-placeholder="endPlaceholder"
        :value-format="valueFormat"
        clearable
        @change="emit('change', $event)"
    ></el-date-picker>
</template>

<script lang="ts" setup>
const props = withDefaults(
    defineProps<{
        startTime?: string
        endTime?: string
        pickerType?: 'daterange' | 'datetimerange'
        valueFormat?: string
        startPlaceholder?: string
        endPlaceholder?: string
    }>(),
    {
        startTime: '',
        endTime: '',
        pickerType: 'datetimerange',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        startPlaceholder: '开始时间',
        endPlaceholder: '结束时间'
    }
)
const emit = defineEmits(['update:startTime', 'update:endTime', 'change'])

const content = computed<any>({
    get: () => {
        return [props.startTime, props.endTime]
    },
    set: (value: Event | any) => {
        if (value === null) {
            emit('update:startTime', '')
            emit('update:endTime', '')
        } else {
            emit('update:startTime', value[0])
            emit('update:endTime', value[1])
        }
    }
})
</script>
