<script setup>
import {reactive, ref, watch} from "vue";
import Api from "../../../../../api/index.js";
import {Message} from "@arco-design/web-vue";
import JumpMethod from "../../../../../components/JumpMethod/index.vue";

const visible = ref(false);
const emits = defineEmits(['success']);
const {detail} = defineProps({
    detail: {
        type: Object,
        default: null,
    }
});
const form = reactive({
    title: null,
    content: null,
    type: null,
    files: [],
    pop_images: [],
    pop_video: [],
    single_id: null,
    pop_type: 1,
    url: null,
    id: null,
});

const success = async () => {
    if (detail?.id) {
        const {code, msg} = await Api.admin.editNotice(form);
        if (code === 1) Message.success(msg);
    } else {
        const {code, msg} = await Api.admin.addNotice(form);
        if (code === 1) Message.success(msg);
    }
    emits('success');
}

watch(
    () => visible.value,
    (val) => {
        if (val) Object.assign(form, detail)
    },
)
</script>

<template>
    <a-link v-if="!$slots.default" :hoverable="false" @click="visible=true">编辑</a-link>
    <view @click="visible=true" v-else>
        <slot></slot>
    </view>

    <a-modal
        :on-before-ok="success"
        :title="`${detail ? '编辑' : '新增'}平台消息`"
        title-align="start"
        v-model:visible="visible">
        <a-form layout="vertical">
            <a-form-item label="标题">
                <a-input v-model:model-value="form.title" placeholder="请输入标题"></a-input>
            </a-form-item>
            <a-form-item label="简介">
                <a-textarea v-model:model-value="form.content" placeholder="请输入简介"></a-textarea>
            </a-form-item>
            <JumpMethod v-model:form="form"></JumpMethod>
        </a-form>
    </a-modal>
</template>

<style scoped lang="scss">

</style>
