<!-- 新增/编辑加价营销活动弹窗 -->
<template>
    <ele-modal form :width="560" v-model="visible" :title="title">
        <SimpleForm
            :items="formItems"
            labelWidth="120px"
            ref="formRef"
            :initKeys="formData"
        />
        <template #footer>
            <el-button @click="handleCancel">关闭</el-button>
            <el-button type="primary" @click="handleSubmit">确定</el-button>
        </template>
    </ele-modal>
</template>

<script setup>
    import { reactive, ref, computed, nextTick, getCurrentInstance } from 'vue';
    import SimpleForm from '@/components/CommonPage/SimpleForm.vue';
    import request from '@/utils/request';
    import { EleMessage } from 'ele-admin-plus/es';

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

    /** 弹窗是否打开 */
    const visible = defineModel({ type: Boolean });

    /** 表单标题 */
    const title = ref('新增活动');

    /** 表单引用 */
    const formRef = ref();

    /** 关闭弹窗 */
    const handleCancel = () => {
        visible.value = false;
        nextTick(() => {
            formRef.value?.resetForm();
        });
    };

    /** 表单数据 */
    const formData = reactive({
        activityName: '',
        startTime: '',
        endTime: '',
        limitFirst: '1',
        inviteNum: '',
        timeRange: [],
        activityId: ''
    });

    /** 表单项配置 */
    const formItems = computed(() => {
        return [
            {
                type: 'input',
                label: '活动名称',
                prop: 'activityName',
                required: true
            },
            {
                type: 'datetimerange',
                label: '活动时间',
                required: true,
                prop: 'timeRange',
                props: {
                    format: 'YYYY-MM-DD HH:mm:ss',
                    valueFormat: 'YYYY-MM-DD HH:mm:ss',
                    rangeSeparator: '至'
                }
            },
            {
                type: 'dictRadio',
                label: '是否限制首单',
                prop: 'limitFirst',
                props: { code: 'limit_first' },
                required: true
            },
            {
                type: 'input',
                label: '加价助力人数',
                prop: 'inviteNum',
                required: true
            }
        ];
    });

    /** 弹窗打开事件 */
    function handleOpen(data = {}) {
        visible.value = true;
        title.value = data && data.id ? '编辑活动' : '新增活动';

        // 重置表单数据
        for (const key in formData) {
            formData[key] = '';
        }

        // 设置默认值
        formData.limitFirst = '1';

        // 如果是编辑模式，合并数据
        if (data && data.id) {
            Object.assign(formData, data);
            formData.activityId = data.id;
        }

        formData.timeRange = [formData.startTime, formData.endTime];
        console.log(data, formData, 'xxxx');

        nextTick(() => {
            formRef.value?.setData(formData);
        });
    }

    /** 提交表单 */
    function handleSubmit() {
        formRef.value?.submitForm().then((data) => {
            const url = formData.activityId
                ? '/activity/activityUpsellInfo/update'
                : '/activity/activityUpsellInfo/add';

            let params = {
                ...data,
                status: 1,
                activityId: formData.activityId,
                startTime: data.timeRange[0],
                endTime: data.timeRange[1]
            };
            delete params.timeRange;

            request.post(url, params).then((res) => {
                if (res.data.code === 200) {
                    EleMessage.success('保存成功');
                    visible.value = false;
                    emit('success');
                } else {
                    EleMessage.error(res.data.msg || '保存失败');
                }
            });
        });
    }

    /** 获取省市列表 */
    function getProvinceList(id = 1) {
        return proxy.$http.get(`/baseinfo/districtInfo/findInfo/${id}`);
    }

    defineExpose({ handleOpen });
</script>
