<template>
    <div class="agreement-page">
        <div class="page-header">
            <div class="header-title">
                <span class="header-icon">📄</span>
                <span class="header-text">协议管理</span>
            </div>
            <span class="header-sub">管理小程序端用户协议与隐私政策</span>
        </div>

        <el-card shadow="never" class="agreement-card">
            <el-tabs v-model="activeTab" class="agreement-tabs" @tab-change="handleTabChange">
                <el-tab-pane name="1">
                    <template #label>
                        <span class="tab-label">
                            <el-icon>
                                <Document />
                            </el-icon>
                            用户协议
                        </span>
                    </template>
                </el-tab-pane>
                <el-tab-pane name="2">
                    <template #label>
                        <span class="tab-label">
                            <el-icon>
                                <Lock />
                            </el-icon>
                            隐私政策
                        </span>
                    </template>
                </el-tab-pane>
            </el-tabs>

            <el-form ref="formRef" :model="form" :rules="rules" label-width="90px" class="agreement-form">
                <el-form-item label="协议标题" prop="title">
                    <el-input v-model="form.title" placeholder="请输入协议标题" maxlength="100" />
                </el-form-item>
                <el-form-item label="协议内容" prop="content">
                    <div class="editor-wrapper">
                        <editor v-model="form.content" :min-height="380" />
                    </div>
                </el-form-item>
                <el-form-item>
                    <el-button v-hasPermi="['system:agreement:edit']" type="primary" icon="Check" :loading="saving"
                        @click="handleSave">
                        保存
                    </el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script setup lang="ts">
import { Document, Lock } from '@element-plus/icons-vue';
import { getAgreement, updateAgreement } from '@/api/system/agreement';
import { AgreementForm } from '@/api/system/agreement/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const activeTab = ref<string>('1');
const saving = ref(false);
const formRef = ref<ElFormInstance>();

const form = reactive<AgreementForm>({
    id: 1,
    title: '',
    content: ''
});

const rules = {
    title: [{ required: true, message: '协议标题不能为空', trigger: 'blur' }],
    content: [{ required: true, message: '协议内容不能为空', trigger: 'change' }]
};

const loadAgreement = async (id: number) => {
    const res = await getAgreement(id);
    form.id = res.data.id;
    form.title = res.data.title;
    form.content = res.data.content;
};

const handleTabChange = (name: string | number) => {
    loadAgreement(Number(name));
};

const handleSave = () => {
    formRef.value?.validate(async (valid: boolean) => {
        if (!valid) return;
        saving.value = true;
        try {
            const encoded = btoa(unescape(encodeURIComponent(form.content)));
            await updateAgreement({ ...form, content: encoded });
            proxy?.$modal.msgSuccess('保存成功');
        } finally {
            saving.value = false;
        }
    });
};

onMounted(() => {
    loadAgreement(1);
});
</script>

<style scoped>
.agreement-page {
    width: 100%;
    height: 100%;
    padding: 20px 24px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    gap: 16px;
    background: #f7f8fc;
}

/* ==================== 页头 ==================== */
.page-header {
    display: flex;
    align-items: baseline;
    gap: 14px;
    flex-shrink: 0;
}

.header-title {
    display: flex;
    align-items: center;
    gap: 8px;
}

.header-icon {
    font-size: 22px;
    line-height: 1;
}

.header-text {
    font-size: 18px;
    font-weight: 700;
    color: #1a202c;
    letter-spacing: 0.3px;
}

.header-sub {
    font-size: 13px;
    color: #a0aec0;
}

/* ==================== 卡片 ==================== */
.agreement-card {
    flex: 1;
    display: flex;
    flex-direction: column;
    border-radius: 12px !important;
    border: none !important;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04), 0 4px 16px rgba(0, 0, 0, 0.03) !important;
    overflow: hidden;
}

.agreement-card :deep(.el-card__body) {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 0 24px 24px;
    overflow: hidden;
}

/* ==================== Tabs ==================== */
.agreement-tabs {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
}

.agreement-tabs :deep(.el-tabs__header) {
    margin-bottom: 24px;
    padding: 0 4px;
    border-bottom: 1px solid #edf2f7;
}

.agreement-tabs :deep(.el-tabs__nav-wrap::after) {
    display: none;
}

.agreement-tabs :deep(.el-tabs__item) {
    height: 44px;
    line-height: 44px;
    font-size: 14px;
    color: #718096;
    padding: 0 20px;
    font-weight: 500;
}

.agreement-tabs :deep(.el-tabs__item.is-active) {
    color: #2563eb;
    font-weight: 600;
}

.agreement-tabs :deep(.el-tabs__active-bar) {
    background-color: #2563eb;
    height: 3px;
    border-radius: 3px 3px 0 0;
}

.tab-label {
    display: flex;
    align-items: center;
    gap: 6px;
}

.agreement-tabs :deep(.el-tabs__content) {
    flex: 1;
    overflow-y: auto;
}

/* ==================== 表单 ==================== */
.agreement-form {
    max-width: 100%;
}

.agreement-form :deep(.el-form-item__label) {
    font-weight: 500;
    color: #4a5568;
}

.agreement-form :deep(.el-input__wrapper) {
    border-radius: 8px;
    box-shadow: 0 0 0 1px #e2e8f0 inset;
}

.agreement-form :deep(.el-input__wrapper:hover) {
    box-shadow: 0 0 0 1px #cbd5e1 inset;
}

.agreement-form :deep(.el-input__wrapper.is-focus) {
    box-shadow: 0 0 0 1px #2563eb inset, 0 0 0 3px rgba(37, 99, 235, 0.08);
}

.editor-wrapper {
    width: 100%;
    border: 1px solid #e2e8f0;
    border-radius: 10px;
    overflow: hidden;
    transition: border-color 0.25s, box-shadow 0.25s;
}

.editor-wrapper:focus-within {
    border-color: #2563eb;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.08);
}

.agreement-form :deep(.el-button--primary) {
    border-radius: 8px;
    padding: 8px 28px;
    font-weight: 500;
}
</style>
