<script setup>
import { ref } from 'vue';
import { useAjax } from "../../../../../resources/js/components/ajaxHandler.js";
import { toast } from "vue3-toastify";

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

const { request } = useAjax()

const editing = ref(false)
const form = ref({
    name: props.data.name ?? '',
    slug: props.data.slug ?? '',
    url: props.data.url ?? '',
})

async function save() {
    const saveUrl = props.data.saveEndpoint ?? '/api/v1/admin/tenants/' + props.data.slug + '/general'
    const response = await request(saveUrl, {
        method: 'POST',
        body: form.value,
    })

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

<template>
    <div v-if="!editing">
        <table class="data-table">
            <tr><th>Name:</th><td>{{ form.name }}</td></tr>
            <tr><th>Slug:</th><td>{{ form.slug }}</td></tr>
            <tr><th>URL:</th><td>{{ form.url }}</td></tr>
            <tr><th>Status:</th><td>
                <span class="badge-active">Aktiv</span>
            </td></tr>
        </table>
        <button class="btn-edit" @click="editing = true">Bearbeiten</button>
    </div>

    <div v-else>
        <table class="data-table">
            <tr>
                <th>Name:</th>
                <td><input type="text" v-model="form.name" class="form-input" /></td>
            </tr>
            <tr>
                <th>Slug:</th>
                <td><input type="text" v-model="form.slug" class="form-input" /></td>
            </tr>
            <tr>
                <th>URL:</th>
                <td><input type="text" v-model="form.url" class="form-input" /></td>
            </tr>
            <tr><th>Status:</th><td>
                <span class="badge-active">Aktiv</span>
            </td></tr>
        </table>
        <div class="btn-group">
            <button class="btn-save" @click="save">Speichern</button>
            <button class="btn-cancel" @click="editing = false">Abbrechen</button>
        </div>
    </div>
</template>

<style scoped>
.data-table {
    width: 100%;
    border-collapse: collapse;
}

.data-table th {
    text-align: left;
    padding: 8px 12px;
    width: 200px;
    color: #374151;
    border-bottom: 1px solid #e5e7eb;
}

.data-table td {
    padding: 8px 12px;
    border-bottom: 1px solid #e5e7eb;
}

.form-input {
    width: 100%;
    padding: 6px 10px;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    font-size: 0.95rem;
    box-sizing: border-box;
}

.badge-active {
    display: inline-block;
    padding: 3px 12px;
    border-radius: 12px;
    font-size: 0.85rem;
    font-weight: bold;
    color: #166534;
    background-color: #dcfce7;
    border: 1px solid #22c55e;
}

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

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

.btn-edit:hover {
    background-color: #163a7a;
}

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

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

.btn-cancel {
    background-color: #e5e7eb;
    color: #374151;
    margin-left: 10px;
}

.btn-cancel:hover {
    background-color: #d1d5db;
}

.btn-group {
    display: flex;
    gap: 10px;
}
</style>
