<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({
    email: props.data.email ?? '',
    email_finance: props.data.email_finance ?? '',
    postcode: props.data.postcode ?? '',
    city: props.data.city ?? '',
})

async function save() {
    const saveUrl = props.data.saveEndpoint ?? '/api/v1/admin/tenant/contact'
    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>Email:</th><td>{{ form.email }}</td></tr>
            <tr><th>Email Schatzmeister*in:</th><td>{{ form.email_finance }}</td></tr>
            <tr><th>Postleitzahl:</th><td>{{ form.postcode }}</td></tr>
            <tr><th>Ort:</th><td>{{ form.city }}</td></tr>
        </table>
        <button class="btn-edit" @click="editing = true">Bearbeiten</button>
    </div>

    <div v-else>
        <table class="data-table">
            <tr>
                <th>Email:</th>
                <td><input type="email" v-model="form.email" class="form-input" /></td>
            </tr>
            <tr>
                <th>Email Schatzmeister*in:</th>
                <td><input type="email" v-model="form.email_finance" class="form-input" /></td>
            </tr>
            <tr>
                <th>Postleitzahl:</th>
                <td><input type="text" v-model="form.postcode" class="form-input" /></td>
            </tr>
            <tr>
                <th>Ort:</th>
                <td><input type="text" v-model="form.city" class="form-input" /></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;
}

.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>
