<!-- 新建营销书籍弹窗 -->
<template>
    <ele-modal
        form
        :width="460"
        v-model="visible"
        title="新建营销书籍"
        @open="handleOpen"
    >
        <SimpleForm
            :items="items"
            labelWidth="80px"
            ref="formRef"
            :initKeys="form"
        />
        <template #footer>
            <el-button @click="handleCancel">取消</el-button>
            <el-button type="primary" @click="handleSumbit">确定</el-button>
        </template>
    </ele-modal>
</template>

<script setup>
    import { ref, reactive, nextTick } from 'vue';
    import SimpleForm from '@/components/CommonPage/SimpleForm.vue';
    import request from '@/utils/request';
    import { ElMessage } from 'element-plus';
    /** 弹窗是否打开 */
    const visible = defineModel({ type: Boolean });

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

    const form = ref({
        isbn: ''
    });

    /** 弹窗打开事件 */
    const handleOpen = () => {
        visible.value = true;
        nextTick(() => {
            formRef.value?.resetForm();
        });
    };

    const items = reactive([
        {
            label: 'ISBN',
            prop: 'isbn',
            type: 'input',
            required: true,
            placeholder: '请输入ISBN'
        }
    ]);

    const formRef = ref();

    const emit = defineEmits(['refresh']);
    const handleSumbit = () => {
        formRef.value?.submitForm().then((data) => {
            request
                .post(
                    '/activity/activityUpsellBook/activity/upsell/book/add',
                    data
                )
                .then((res) => {
                    if (res.data.code === 200) {
                        visible.value = false;
                        ElMessage.success('添加成功');
                        emit('done');
                    } else {
                        ElMessage.error(res.data.msg);
                    }
                });
        });
    };

    defineExpose({
        handleOpen
    });
</script>
