<!-- 招募规则设置 -->
<template>
    <div class="partner-rule">
        <!-- 左侧手机预览 -->
        <div class="mobile-preview">
            <div class="preview-content">
                <div
                    class="content-preview"
                    v-html="form.content"
                    v-if="form.content"
                ></div>
            </div>
        </div>

        <!-- 右侧编辑区域 -->
        <div class="edit-area">
            <el-form :model="form" ref="formRef" label-width="80px">
                <el-form-item label="banner图:">
                    <ImageUpload
                        v-model="form.banner"
                        :width="400"
                        :height="200"
                        :limit="1"
                    />
                </el-form-item>
                <el-form-item label="标题:">
                    <el-input v-model="form.title" placeholder="请输入标题" />
                </el-form-item>
                <el-form-item label="详情:">
                    <div style="height: 600px">
                        <TinymceEditor v-model="form.content" />
                    </div>
                </el-form-item>
                <el-form-item>
                    <el-button
                        type="primary"
                        @click="handleSave"
                        style="min-width: 120px"
                        >保存</el-button
                    >
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script setup>
    import { ref, onMounted } from 'vue';
    import { ElMessage } from 'element-plus';
    import TinymceEditor from '@/components/TinymceEditor/index.vue';
    import ImageUpload from '@/components/ImageUpload/index.vue';
    import request from '@/utils/request';

    const formRef = ref(null);
    const form = ref({
        banner: '',
        title: '',
        content: ''
    });

    // 获取规则信息
    const getRule = async () => {
        try {
            const res = await request.get('/user/userPartnerRule/getInfo');
            if (res.data.code === 200) {
                let { data } = res.data;
                form.value = {
                    banner: data.banner,
                    title: data.title,
                    content: data.content
                };
            }
        } catch (error) {
            ElMessage.error('获取规则信息失败');
        }
    };

    // 保存规则
    const handleSave = async () => {
        try {
            const res = await request.post(
                '/user/userPartnerRule/update',
                form.value
            );
            console.log(res, 'xxx');
            if (res.data.code === 200) {
                ElMessage.success('保存成功');
            } else {
                ElMessage.error(res.msg || '保存失败');
            }
        } catch (error) {
            ElMessage.error('保存失败');
        }
    };

    onMounted(() => {
        getRule();
    });
</script>

<style lang="scss" scoped>
    .partner-rule {
        padding: 16px;
        display: flex;

        :deep(.tox-tinymce) {
            height: 100% !important;
        }

        .mobile-preview {
            background: url('@/assets/mobile.png') no-repeat center center;
            background-size: 100% 100%;
            width: 545px;
            min-width: 545px;
            height: 900px;
            border-radius: 20px;
            overflow: hidden;

            .preview-content {
                padding: 16px;
                h3 {
                    margin: 16px 0;
                    font-size: 18px;
                }

                .content-preview {
                    font-size: 14px;
                    line-height: 1.6;
                    color: #333;
                    overflow-y: auto;
                    position: absolute;
                    width: 357px;
                    height: 742px;
                    border-radius: 0 0 30px 30px;
                    top: 110px;
                    left: 110px;
                    right: 0;
                    bottom: 0;
                    &::-webkit-scrollbar {
                        display: none;
                    }
                }
            }
        }

        .edit-area {
            background: #fff;
            padding: 20px;
            border-radius: 4px;
        }
    }
</style>
