<template>
    <section class="mt-8">
        <div class="overflow-hidden bg-white">
            <div
                class="grid grid-cols-5 border border-black bg-tertiary-500 px-6 py-3 text-[20px] font-semibold text-black rounded-t-md">
                <p>Mois</p>
                <p>Nombre de Km</p>
                <p>Montant €</p>
                <p>Commentaire</p>
                <p>Justificatif</p>
            </div>
            <div v-if="allowances.length === 0" class="px-6 py-4 text-[20px] font-bold text-primary-500 border-x border-b border-primary-500 rounded-b-md">
                Aucun frais kilométrique.
            </div>
            <div v-else class="border-x border-b border-primary-500 rounded-b-md">
                <div
                    v-for="item in allowances"
                    :key="item.id"
                    class="grid grid-cols-5 border-b border-primary-500 px-6 py-3 text-md font-bold text-primary-500 last:border-b-0 cursor-pointer hover:bg-tertiary-500"
                    @click="onOpenEditDrawer(item)"
                >
                    <p>{{ formatMonth(item.month) }}</p>
                    <p>{{ item.kilometers }}</p>
                    <p>{{ item.amount ? item.amount + ' €' : '-' }}</p>
                    <p>{{ item.comment ?? '-' }}</p>
                    <p>
                        <a
                            v-if="item.receiptPath"
                            :href="getReceiptUrl(props.apiBase, item.id)"
                            target="_blank"
                            class="text-primary-500 hover:text-secondary-500 flex gap-2 items-center"
                            @click.stop
                        >
                            <Icon name="mdi:file-download-outline" size="20"/>
                            <span>{{ item.receiptName ?? 'Télécharger' }}</span>
                        </a>
                        <span v-else>-</span>
                    </p>
                </div>
            </div>
        </div>
        <div class="flex justify-center mb-4 mt-8">
            <button
                type="button"
                class="flex w-[200px] items-center justify-center gap-2 rounded-md bg-primary-500 px-4 py-2 text-md text-white disabled:cursor-not-allowed disabled:opacity-50"
                @click="onOpenCreateDrawer"
            >
                + Ajouter
            </button>
        </div>


        <AppDrawer v-model="isDrawerOpen" title="Frais">
            <form class="space-y-4" @submit.prevent="onSubmit">
                <div>
                    <label class="text-md font-semibold text-neutral-700" for="mileage-month">
                        Mois <span class="text-red-600">*</span>
                    </label>
                    <input
                        id="mileage-month"
                        v-model="form.month"
                        type="month"
                        class="capitalize mt-2 w-full rounded-md border border-neutral-300 px-3 py-2 text-base text-neutral-900 focus:border-primary-500 focus:outline-none focus:ring-2 focus:ring-secondary-500/20"
                    />
                </div>

                <div>
                    <label class="text-md font-semibold text-neutral-700" for="mileage-kilometers">
                        Nombre de Km
                    </label>
                    <input
                        id="mileage-kilometers"
                        v-model.number="form.kilometers"
                        type="number"
                        step="0.1"
                        min="0"
                        class="mt-2 w-full rounded-md border border-neutral-300 px-3 py-2 text-base text-neutral-900 focus:border-primary-500 focus:outline-none focus:ring-2 focus:ring-secondary-500/20"
                    />
                </div>

                <div>
                    <label class="text-md font-semibold text-neutral-700" for="mileage-amount">
                        Montant (€)
                    </label>
                    <input
                        id="mileage-amount"
                        v-model.number="form.amount"
                        type="number"
                        step="0.01"
                        min="0"
                        class="mt-2 w-full rounded-md border border-neutral-300 px-3 py-2 text-base text-neutral-900 focus:border-primary-500 focus:outline-none focus:ring-2 focus:ring-secondary-500/20"
                    />
                    <p class="mt-1 text-sm text-neutral-500">Au moins un des deux champs doit être rempli</p>
                </div>

                <div>
                    <label class="text-md font-semibold text-neutral-700" for="mileage-receipt">
                        Justificatif
                    </label>
                    <div v-if="isEditing && editingItem?.receiptName" class="mt-1 text-sm text-neutral-500">
                        Fichier actuel : {{ editingItem.receiptName }}
                    </div>
                    <input
                        id="mileage-receipt"
                        ref="fileInput"
                        type="file"
                        class="mt-2 w-full rounded-md border border-neutral-300 px-3 py-2 text-base text-neutral-900 file:mr-3 file:rounded file:border-0 file:bg-primary-500 file:px-3 file:py-1 file:text-sm file:text-white"
                        @change="onFileChange"
                    />
                    <p v-if="fileError" class="mt-1 text-sm text-red-600">{{ fileError }}</p>
                    <p v-else class="mt-1 text-sm text-neutral-500">Fichier au format pdf</p>
                </div>

                <div>
                    <label class="text-md font-semibold text-neutral-700" for="mileage-comment">
                        Commentaire
                    </label>
                    <textarea
                        id="mileage-comment"
                        v-model="form.comment"
                        rows="3"
                        class="mt-2 w-full rounded-md border border-neutral-300 px-3 py-2 text-base text-neutral-900 focus:border-primary-500 focus:outline-none focus:ring-2 focus:ring-secondary-500/20"
                        placeholder="Commentaire..."
                    />
                </div>

                <div v-if="isEditing" class="grid grid-cols-2 gap-3 pt-2">
                    <button
                        type="button"
                        class="flex items-center justify-center rounded-md bg-red-500 px-4 py-2 text-md font-semibold text-white hover:bg-red-600"
                        @click="onDelete"
                    >
                        Supprimer
                    </button>
                    <button
                        type="submit"
                        class="flex items-center justify-center rounded-md bg-primary-500 px-4 py-2 text-md font-semibold text-white hover:bg-secondary-500 disabled:cursor-not-allowed disabled:opacity-50"
                        :disabled="!isFormValid"
                    >
                        Modifier
                    </button>
                </div>
                <div v-else class="flex justify-center pt-2">
                    <button
                        type="submit"
                        class="flex w-[200px] items-center justify-center gap-2 rounded-md bg-primary-500 px-4 py-2 text-md font-semibold text-white hover:bg-secondary-500 disabled:cursor-not-allowed disabled:opacity-50"
                        :disabled="!isFormValid"
                    >
                        + Ajouter
                    </button>
                </div>
            </form>
        </AppDrawer>
    </section>
</template>

<script setup lang="ts">
import type {MileageAllowance} from '~/services/dto/mileage-allowance'
import {getReceiptUrl} from '~/services/mileage-allowances'
import AppDrawer from '~/components/AppDrawer.vue'

const props = defineProps<{
    allowances: MileageAllowance[]
    apiBase: string
}>()

const emit = defineEmits<{
    (event: 'create', data: { month: string; kilometers: number; amount: number; comment?: string }, file?: File): void
    (event: 'update', id: number, data: { month: string; kilometers: number; amount: number; comment?: string }, file?: File): void
    (event: 'delete', id: number): void
}>()

const isDrawerOpen = ref(false)
const isEditing = ref(false)
const editingItem = ref<MileageAllowance | null>(null)
const selectedFile = ref<File | undefined>(undefined)
const fileInput = ref<HTMLInputElement | null>(null)
const fileError = ref('')

const currentYearMonth = () => {
    const now = new Date()
    return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`
}

const form = reactive({
    month: currentYearMonth(),
    kilometers: 0,
    amount: 0,
    comment: ''
})

const isFormValid = computed(() => {
    return form.month && (form.kilometers > 0 || form.amount > 0) && !fileError.value
})

const monthLabels: Record<number, string> = {
    1: 'Janvier',
    2: 'Février',
    3: 'Mars',
    4: 'Avril',
    5: 'Mai',
    6: 'Juin',
    7: 'Juillet',
    8: 'Août',
    9: 'Septembre',
    10: 'Octobre',
    11: 'Novembre',
    12: 'Décembre'
}

const formatMonth = (dateStr: string): string => {
    const date = new Date(dateStr)
    if (Number.isNaN(date.getTime())) return dateStr
    const month = date.getMonth() + 1
    const year = date.getFullYear()
    return `${monthLabels[month]} ${year}`
}

const resetForm = () => {
    form.month = currentYearMonth()
    form.kilometers = 0
    form.amount = 0
    form.comment = ''
    selectedFile.value = undefined
    fileError.value = ''
    if (fileInput.value) {
        fileInput.value.value = ''
    }
}

const onOpenCreateDrawer = () => {
    isEditing.value = false
    editingItem.value = null
    resetForm()
    isDrawerOpen.value = true
}

const onOpenEditDrawer = (item: MileageAllowance) => {
    isEditing.value = true
    editingItem.value = item
    // Extract YYYY-MM from YYYY-MM-DD
    form.month = item.month.substring(0, 7)
    form.kilometers = item.kilometers
    form.amount = item.amount
    form.comment = item.comment ?? ''
    selectedFile.value = undefined
    if (fileInput.value) {
        fileInput.value.value = ''
    }
    isDrawerOpen.value = true
}

const onFileChange = (event: Event) => {
    const target = event.target as HTMLInputElement
    const file = target.files?.[0]
    if (file && file.type !== 'application/pdf') {
        fileError.value = 'Seuls les fichiers PDF sont acceptés.'
        selectedFile.value = undefined
        target.value = ''
        return
    }
    fileError.value = ''
    selectedFile.value = file ?? undefined
}

const onSubmit = () => {
    const data = {
        month: `${form.month}-01`,
        kilometers: form.kilometers,
        amount: form.amount,
        comment: form.comment || undefined
    }

    if (isEditing.value && editingItem.value) {
        emit('update', editingItem.value.id, data, selectedFile.value)
    } else {
        emit('create', data, selectedFile.value)
    }
    isDrawerOpen.value = false
}

const onDelete = () => {
    if (!editingItem.value) return
    const ok = window.confirm('Supprimer ce frais kilométrique ?')
    if (!ok) return
    emit('delete', editingItem.value.id)
    isDrawerOpen.value = false
}
</script>
