<template>
    <div class="h-full overflow-hidden flex flex-col">

        <div v-if="isLoading" class="mt-6 rounded-lg border border-neutral-200 bg-white p-6 text-md text-neutral-600">
            Chargement...
        </div>

        <div v-else-if="!employee"
             class="mt-6 rounded-lg border border-neutral-200 bg-white p-6 text-md text-neutral-600">
            Employé introuvable.
        </div>

        <div v-else class="flex min-h-0 flex-1 flex-col">
            <div class="flex items-center justify-between">
                <h1 class="text-4xl font-bold text-primary-500">{{ employee.firstName }} {{ employee.lastName }}</h1>
                <div class="text-right">
                    <p class="font-bold text-[20px]">{{ contractNatureLabel(employee.currentContractNature) }} {{ employeeContractWorkLabel }}</p>
                    <p class="text-[18px]">{{ employee.site?.name ?? '-' }}</p>
                </div>
            </div>
            <div class="mt-12 border-b border-primary-500">
                <div class="flex justify-center gap-16 text-2xl font-bold">
                    <button
                        class="pb-2 border-b-2 flex items-center gap-3"
                        :class="activeTab === 'contract'
                            ? 'border-primary-500 text-primary-500'
                            : 'border-transparent text-primary-500/50 hover:text-primary-500'"
                        @click="activeTab = 'contract'"
                    >
                        <Icon name="mdi:file-check-outline" size="24" class="align-self"/>
                        Suivi contrat
                    </button>
                    <button
                        v-if="showLeaveTab"
                        class="pb-2 border-b-2 flex items-center gap-3"
                        :class="activeTab === 'leave'
                            ? 'border-primary-500 text-primary-500'
                            : 'border-transparent text-primary-500/50 hover:text-primary-500'"
                        @click="activeTab = 'leave'"
                    >
                        <Icon name="mdi:event-blank-outline" size="24" class="align-self"/>
                        Congé
                    </button>
                    <button
                        class="pb-2 border-b-2 flex items-center gap-3"
                        :class="activeTab === 'rtt'
                            ? 'border-primary-500 text-primary-500'
                            : 'border-transparent text-primary-500/50 hover:text-primary-500'"
                        @click="activeTab = 'rtt'"
                    >
                        <Icon name="mdi:schedule" size="24" class="align-self"/>
                        RTT
                    </button>
                </div>
            </div>
            <div class="min-h-0 flex-1">
                <EmployeesContractTab
                    v-if="activeTab === 'contract'"
                    class="h-full overflow-y-auto pr-1"
                    :contract-history="contractHistory"
                    :contract-nature-label="contractNatureLabel"
                    :contract-history-label="contractHistoryLabel"
                    :format-date="formatDate"
                    :is-contract-submitting="isContractSubmitting"
                    :can-close-current-contract="canCloseCurrentContract"
                    :is-create-contract-submitting="isCreateContractSubmitting"
                    :contracts="contracts"
                    :can-create-contract="canCreateContract"
                    :is-contract-drawer-open="isContractDrawerOpen"
                    :contract-form="contractForm"
                    :readonly-field-class="readonlyFieldClass"
                    :close-contract-worked-hours-label="closeContractWorkedHoursLabel"
                    :contract-end-date-field-class="contractEndDateFieldClass"
                    :show-contract-end-date-error="showContractEndDateError"
                    :is-contract-end-date-valid="isContractEndDateValid"
                    :is-create-contract-drawer-open="isCreateContractDrawerOpen"
                    :create-contract-form="createContractForm"
                    :create-contract-nature-field-class="createContractNatureFieldClass"
                    :create-contract-field-class="createContractFieldClass"
                    :create-contract-start-date-field-class="createContractStartDateFieldClass"
                    :requires-create-contract-end-date="requiresCreateContractEndDate"
                    :create-contract-end-date-field-class="createContractEndDateFieldClass"
                    :is-create-contract-form-valid="isCreateContractFormValid"
                    :on-open-close-contract-drawer="openCloseContractDrawer"
                    :on-open-create-contract-drawer="openCreateContractDrawer"
                    :on-update-contract-drawer-open="setContractDrawerOpen"
                    :on-update-create-contract-drawer-open="setCreateContractDrawerOpen"
                    :on-submit-close-contract="submitContractUpdate"
                    :on-submit-create-contract="submitCreateContract"
                />
                <EmployeesLeaveTab
                    v-else-if="showLeaveTab && activeTab === 'leave'"
                    class="h-full"
                    :absences="employeeAbsences"
                    :summary="leaveSummary"
                    :public-holidays="publicHolidays"
                    @update-fractioned-days="submitFractionedDays"
                />
                <EmployeesRttTab v-else class="h-full" :summary="rttSummary" />
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
const {
    employee,
    isLoading,
    activeTab,
    contracts,
    employeeAbsences,
    leaveSummary,
    rttSummary,
    publicHolidays,
    showLeaveTab,
    contractHistory,
    employeeContractWorkLabel,
    contractForm,
    createContractForm,
    isContractDrawerOpen,
    isContractSubmitting,
    isCreateContractDrawerOpen,
    isCreateContractSubmitting,
    canCloseCurrentContract,
    canCreateContract,
    readonlyFieldClass,
    closeContractWorkedHoursLabel,
    contractEndDateFieldClass,
    showContractEndDateError,
    isContractEndDateValid,
    createContractNatureFieldClass,
    createContractFieldClass,
    createContractStartDateFieldClass,
    requiresCreateContractEndDate,
    createContractEndDateFieldClass,
    isCreateContractFormValid,
    contractNatureLabel,
    contractHistoryLabel,
    formatDate,
    openCloseContractDrawer,
    openCreateContractDrawer,
    setContractDrawerOpen,
    setCreateContractDrawerOpen,
    submitContractUpdate,
    submitCreateContract,
    submitFractionedDays
} = useEmployeeDetailPage()

useHead(() => ({
    title: employee.value
        ? `${employee.value.firstName} ${employee.value.lastName}`
        : 'Détail employé'
}))
</script>
