<template>
    <simple-form-modal
        ref="modalRef"
        :items="formItems"
        :baseUrl="baseUrl"
        @success="handleSuccess"
        :title="title"
        width="520px"
        labelWidth="100px"
        :formatData="formatData"
    />
</template>

<script setup>
    import { ref, computed } from 'vue';
    import SimpleFormModal from '@/components/CommonPage/SimpleFormModal.vue';

    const emit = defineEmits(['success']);
    const modalRef = ref(null);
    const title = ref('添加热搜词');

    const baseUrl = {
        add: '/shop/searchHot/setHotWorld',
        update: '/shop/searchHot/setHotWorld'
    };

    const formatData = (data) => {
        if (!data.source) {
            data.source = 1;
        }
        return data;
    };

    const formItems = computed(() => [
        {
            label: '热搜词',
            prop: 'world',
            type: 'input',
            required: true,
            attrs: {
                placeholder: '请输入'
            }
        },
        {
            label: '权重',
            prop: 'priority',
            type: 'input',
            required: true,
            attrs: {
                placeholder: '请输入'
            }
        },
        {
            label: '类型',
            prop: 'type',
            type: 'select',
            required: true,
            options: [
                { label: '热搜词', value: 1 },
                { label: '搜索框默认词', value: 2 }
            ],
            attrs: {
                placeholder: '请选择类型'
            }
        },
        {
            prop: 'source',
            type: 'hidden',
            defaultValue: 1
        }
    ]);

    const handleOpen = (data) => {
        title.value = data && data.id ? '编辑热搜词' : '添加热搜词';
        modalRef.value.handleOpen(data);
    };

    const handleSuccess = (data) => {
        emit('success', data);
    };

    defineExpose({
        handleOpen
    });
</script>
