<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({
    account_iban: props.data.account_iban ?? '',
    account_bic: props.data.account_bic ?? '',
    account_name: props.data.account_name ?? '',
})

async function save() {
    const saveUrl = props.data.saveEndpoint ?? '/api/v1/admin/tenant/payment'
    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>IBAN:</th><td>{{ form.account_iban }}</td></tr>
            <tr><th>BIC:</th><td>{{ form.account_bic }}</td></tr>
            <tr><th>Name Kontoinhaber:</th><td>{{ form.account_name }}</td></tr>
        </table>
        <button class="btn-edit" @click="editing = true">Bearbeiten</button>
    </div>

    <div v-else>
        <table class="data-table">
            <tr>
                <th>IBAN:</th>
                <td><input type="text" v-model="form.account_iban" class="form-input" /></td>
            </tr>
            <tr>
                <th>BIC:</th>
                <td><input type="text" v-model="form.account_bic" class="form-input" /></td>
            </tr>
            <tr>
                <th>Name Kontoinhaber:</th>
                <td><input type="text" v-model="form.account_name" 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>
