<script setup>
import { Head, Link, useForm } from '@inertiajs/vue3';
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout.vue';

const props = defineProps({
    settings: Object,
});

const form = useForm({
    entity_name: props.settings.entity_name || '',
    entity_sub: props.settings.entity_sub || '',
    email: props.settings.email || '',
    telephone: props.settings.telephone || '',
    adresse: props.settings.adresse || '',
});

const submit = () => {
    form.put(route('devis.settings.update'));
};
</script>

<template>
    <Head title="Paramètres des Devis" />

    <AuthenticatedLayout>
        <template #header>
            <div class="flex items-center justify-between">
                <h2 class="text-xl font-bold leading-tight text-slate-800 dark:text-slate-200">
                    Paramètres d'édition des Devis
                </h2>
                <Link
                    :href="route('devis.index')"
                    class="inline-flex items-center px-4 py-2 text-sm font-semibold text-slate-700 bg-white border border-slate-300 rounded-lg hover:bg-slate-50 dark:bg-slate-900 dark:text-slate-300 dark:border-slate-850 dark:hover:bg-slate-800 transition-colors"
                >
                    Retour
                </Link>
            </div>
        </template>

        <div class="py-6">
            <div class="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8">
                
                <!-- Notification flash de succès -->
                <div v-if="$page.props.flash?.success" class="mb-6 p-4 bg-emerald-50 border border-emerald-200 text-emerald-800 rounded-lg dark:bg-emerald-950/40 dark:border-emerald-900 dark:text-emerald-300 flex items-center shadow-sm">
                    <svg class="w-5 h-5 mr-3 shrink-0" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75 11.25 15 15 9.75M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z" />
                    </svg>
                    <span>{{ $page.props.flash?.success }}</span>
                </div>

                <form @submit.prevent="submit" class="space-y-6">
                    <div class="bg-white dark:bg-slate-900 border border-slate-200 dark:border-slate-800 rounded-xl shadow-sm overflow-hidden p-6 space-y-6">
                        <div class="border-b border-slate-100 dark:border-slate-850 pb-3">
                            <h3 class="text-md font-bold text-slate-800 dark:text-slate-200">
                                Coordonnées de l'Émetteur
                            </h3>
                            <p class="text-xs text-slate-400 mt-1">
                                Ces informations apparaîtront dans l'en-tête de tous les devis générés par la DSI.
                            </p>
                        </div>

                        <div class="space-y-4">
                            <!-- Nom de l'entité -->
                            <div>
                                <label class="block text-sm font-bold text-slate-755 dark:text-slate-300 mb-1">Nom de l'entité principale :</label>
                                <input v-model="form.entity_name" type="text" class="w-full text-sm rounded-lg border-slate-300 shadow-sm focus:border-sky-500 focus:ring-sky-500 dark:bg-slate-950 dark:border-slate-800 dark:text-slate-200" required>
                                <div v-if="form.errors.entity_name" class="text-rose-500 text-xs mt-1">{{ form.errors.entity_name }}</div>
                            </div>

                            <!-- Sous-titre / Organisation -->
                            <div>
                                <label class="block text-sm font-bold text-slate-755 dark:text-slate-300 mb-1">Sous-titre / Organisation parente :</label>
                                <input v-model="form.entity_sub" type="text" class="w-full text-sm rounded-lg border-slate-300 shadow-sm focus:border-sky-500 focus:ring-sky-500 dark:bg-slate-950 dark:border-slate-800 dark:text-slate-200" required>
                                <div v-if="form.errors.entity_sub" class="text-rose-500 text-xs mt-1">{{ form.errors.entity_sub }}</div>
                            </div>

                            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                                <!-- Adresse Email -->
                                <div>
                                    <label class="block text-sm font-bold text-slate-755 dark:text-slate-300 mb-1">Adresse Email :</label>
                                    <input v-model="form.email" type="email" class="w-full text-sm rounded-lg border-slate-300 shadow-sm focus:border-sky-500 focus:ring-sky-500 dark:bg-slate-950 dark:border-slate-800 dark:text-slate-200" required>
                                    <div v-if="form.errors.email" class="text-rose-500 text-xs mt-1">{{ form.errors.email }}</div>
                                </div>

                                <!-- Téléphone -->
                                <div>
                                    <label class="block text-sm font-bold text-slate-755 dark:text-slate-300 mb-1">Numéro de Téléphone :</label>
                                    <input v-model="form.telephone" type="text" class="w-full text-sm rounded-lg border-slate-300 shadow-sm focus:border-sky-500 focus:ring-sky-500 dark:bg-slate-950 dark:border-slate-800 dark:text-slate-200" required>
                                    <div v-if="form.errors.telephone" class="text-rose-500 text-xs mt-1">{{ form.errors.telephone }}</div>
                                </div>
                            </div>

                            <!-- Adresse postale -->
                            <div>
                                <label class="block text-sm font-bold text-slate-755 dark:text-slate-300 mb-1">Adresse postale (optionnel) :</label>
                                <input v-model="form.adresse" type="text" class="w-full text-sm rounded-lg border-slate-300 shadow-sm focus:border-sky-500 focus:ring-sky-500 dark:bg-slate-950 dark:border-slate-800 dark:text-slate-200">
                                <div v-if="form.errors.adresse" class="text-rose-500 text-xs mt-1">{{ form.errors.adresse }}</div>
                            </div>
                        </div>
                    </div>

                    <!-- Actions -->
                    <div class="flex items-center justify-end gap-3 pt-6 border-t border-slate-200 dark:border-slate-800">
                        <Link
                            :href="route('devis.index')"
                            class="inline-flex items-center px-4 py-2 border border-slate-300 text-sm font-semibold rounded-lg text-slate-700 bg-white hover:bg-slate-50 dark:bg-slate-900 dark:text-slate-300 dark:border-slate-850 dark:hover:bg-slate-800 transition-colors shadow-sm"
                        >
                            Annuler
                        </Link>
                        
                        <button
                            type="submit"
                            :disabled="form.processing"
                            class="inline-flex items-center px-4 py-2 text-sm font-semibold text-white bg-sky-600 hover:bg-sky-500 dark:bg-sky-500 dark:hover:bg-sky-400 rounded-lg shadow-sm focus:outline-none focus:ring-2 focus:ring-sky-500 transition-colors disabled:opacity-50"
                        >
                            Enregistrer les Paramètres
                        </button>
                    </div>
                </form>

            </div>
        </div>
    </AuthenticatedLayout>
</template>
