<script setup>
import { ref } from 'vue';
import TextEditor from "../../../../Views/Components/TextEditor.vue";
import { useAjax } from "../../../../../resources/js/components/ajaxHandler.js";
import { toast } from "vue3-toastify";
import gdprTemplate from "../../../../../resources/templates/gdpr-template.html?raw";

const props = defineProps({
    data: {
        type: Object,
        default: () => ({})
    },
})

const { request } = useAjax()

const content = ref(props.data.gdpr_text ?? '')

function autoGenerate() {
    if (content.value && content.value.trim() !== '') {
        toast.error('Der Editor ist nicht leer. Bitte leere den Inhalt zuerst, um die Vorlage zu verwenden.')
        return
    }

    const today = new Date().toLocaleDateString('de-DE', { day: '2-digit', month: '2-digit', year: 'numeric' })
    content.value = gdprTemplate.replace('[Datum]', today)
}

async function save() {
    const response = await request('/api/v1/admin/tenant/gdpr', {
        method: 'POST',
        body: { gdpr_text: content.value },
    })

    if (response && response.status === 'success') {
        toast.success(response.message)
    } else {
        toast.error(response?.message ?? 'Fehler beim Speichern')
    }
}
</script>

<template>
    <div>
        <TextEditor v-model="content" />
        <div class="btn-group">
            <button class="btn-save" @click="save">Speichern</button>
            <button class="btn-generate" @click="autoGenerate">Auto-generieren</button>
        </div>
    </div>
</template>

<style scoped>
.btn-group {
    display: flex;
    gap: 10px;
    margin-top: 15px;
}

.btn-save, .btn-generate {
    padding: 8px 20px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-weight: bold;
    font-size: 0.9rem;
}

.btn-save {
    background-color: #16a34a;
    color: #ffffff;
}

.btn-save:hover {
    background-color: #15803d;
}

.btn-generate {
    background-color: #1d4899;
    color: #ffffff;
}

.btn-generate:hover {
    background-color: #163a7a;
}
</style>
