<script setup>
import {computed, ref} from 'vue';
import FullScreenModal from "../../../../Views/Components/FullScreenModal.vue";
import TextEditor from "../../../../Views/Components/TextEditor.vue";
import RichSelectBox from "../../../../Views/Components/RichSelectBox.vue";
import {PAYMENT_METHOD_ICONS} from "../../../../../resources/js/constants/paymentMethodIcons.js";
import {useAjax} from "../../../../../resources/js/components/ajaxHandler.js";
import {toast} from "vue3-toastify";

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

const { request } = useAjax()

const paymentMethods = ref(props.data.paymentMethods ?? [])
const editing = ref(null)
const form = ref({ name: '', description: '', active: true, configuration: {} })

// Sind alle Pflichtfelder des aktuell bearbeiteten Moduls befüllt? Der Server ist die
// eigentliche Autorität (Guard), das hier dient nur der Bedienführung.
const requiredComplete = computed(() => {
    if (!editing.value) return true
    return (editing.value.optionsSchema ?? [])
        .filter(option => option.required)
        .every(option => {
            const value = form.value.configuration[option.name]
            return value !== undefined && value !== null && value !== ''
        })
})

function edit(paymentMethod) {
    editing.value = paymentMethod
    const configuration = {}
    for (const option of paymentMethod.optionsSchema ?? []) {
        configuration[option.name] = paymentMethod.configuration?.[option.name] ?? ''
    }
    form.value = {
        name: paymentMethod.name,
        description: paymentMethod.description ?? '',
        active: paymentMethod.active,
        configuration,
    }
}

function close() {
    editing.value = null
}

async function save() {
    const response = await request('/api/v1/admin/tenant/payment-methods/' + editing.value.id, {
        method: 'POST',
        body: form.value,
    })

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

<template>
    <table class="payment-method-table">
        <thead>
            <tr>
                <th>Name</th>
                <th>Status</th>
                <th class="empty"></th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="paymentMethod in paymentMethods" :key="paymentMethod.id">
                <td>{{ paymentMethod.name }}</td>
                <td>
                    <span :class="paymentMethod.active ? 'badge-active' : 'badge-inactive'">
                        {{ paymentMethod.active ? 'Aktiv' : 'Inaktiv' }}
                    </span>
                </td>
                <td>
                    <button class="btn-edit" @click="edit(paymentMethod)">Bearbeiten</button>
                </td>
            </tr>
        </tbody>
    </table>

    <FullScreenModal :show="editing !== null" @close="close">
        <template v-if="editing">
            <h2>Zahlungsweg bearbeiten</h2>
            <table class="data-table">
                <tr><th>Name</th><td><input type="text" v-model="form.name" class="form-input" /></td></tr>
                <tr><th>Beschreibung</th><td><textarea v-model="form.description" class="form-input"></textarea></td></tr>
                <tr v-for="option in editing.optionsSchema ?? []" :key="option.name">
                    <th>{{ option.label }}<span v-if="option.required" class="required-marker">*</span></th>
                    <td>
                        <RichSelectBox v-if="option.type === 'icon'" v-model="form.configuration[option.name]"
                                       :options="PAYMENT_METHOD_ICONS" placeholder="Symbol wählen…"/>
                        <TextEditor v-else-if="option.type === 'richtext'" v-model="form.configuration[option.name]"/>
                        <input v-else type="text" v-model="form.configuration[option.name]" class="form-input"/>
                    </td>
                </tr>
                <tr>
                    <th>Aktiv</th>
                    <td>
                        <input type="checkbox" v-model="form.active" :disabled="!requiredComplete && !form.active" />
                        <span v-if="!requiredComplete" class="hint">Bitte zuerst alle Pflichtfelder (*) ausfüllen, um zu aktivieren.</span>
                    </td>
                </tr>
            </table>
            <div class="btn-group">
                <button class="btn-save" @click="save">Speichern</button>
                <button class="btn-cancel" @click="close">Abbrechen</button>
            </div>
        </template>
    </FullScreenModal>
</template>

<style scoped>
.payment-method-table {
    width: 100%;
    border-collapse: collapse;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    overflow: hidden;
}

.payment-method-table thead th {
    text-align: left;
    padding: 10px 16px;
    background-color: #f9fafb;
    color: #374151;
    font-weight: bold;
    border-bottom: 2px solid #d1d5db;
}

.payment-method-table tbody td {
    padding: 10px 16px;
    border-bottom: 1px solid #e5e7eb;
}

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

.badge-inactive {
    display: inline-block;
    padding: 3px 12px;
    border-radius: 12px;
    font-size: 0.85rem;
    font-weight: bold;
    color: #991b1b;
    background-color: #fee2e2;
    border: 1px solid #ef4444;
}

.btn-edit {
    padding: 6px 16px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-weight: bold;
    font-size: 0.9rem;
    background-color: #1d4899;
    color: #ffffff;
}

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

.data-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 15px;
}

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

.required-marker {
    color: #ef4444;
    margin-left: 2px;
}

.hint {
    display: block;
    margin-top: 4px;
    font-size: 0.8rem;
    color: #b45309;
}

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

.btn-save, .btn-cancel {
    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-cancel {
    background-color: #e5e7eb;
    color: #374151;
}

.btn-cancel:hover {
    background-color: #d1d5db;
}
</style>
