<template>
    <div class="h-full overflow-hidden flex flex-col">
        <div class="flex flex-wrap items-center justify-between gap-4">
            <h1 class="text-4xl font-bold text-primary-500">Heures</h1>
        </div>

        <HoursToolbar
            v-model:selected-date="selectedDate"
            v-model:view-mode="viewMode"
            v-model:selected-site-ids="selectedSiteIds"
            v-model:employee-filter="employeeFilter"
            :is-admin="isAdmin"
            :sites="sites"
            :absence-types="absenceTypes"
            :formatted-selected-date="formattedSelectedDate"
            :shortcut-button-class="shortcutButtonClass"
            :week-shortcut-button-class="weekShortcutButtonClass"
            :get-week-shortcut-label="getWeekShortcutLabel"
            @set-yesterday="setYesterday"
            @set-today="setToday"
            @set-tomorrow="setTomorrow"
            @set-previous-week="setPreviousWeek"
            @set-this-week="setThisWeek"
            @set-next-week="setNextWeek"
            @shift-date="shiftDate"
        />

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

        <div v-else-if="employees.length === 0" class="rounded-lg border border-neutral-200 bg-white p-6 text-md text-neutral-600">
            Aucun employé accessible.
        </div>

        <div v-else class="flex flex-1 min-h-0 flex-col gap-4">
            <div class="flex-1 min-h-0 flex flex-col">
                <HoursDayView
                    v-if="viewMode === 'day'"
                    v-model:rows="rows"
                    :employees="visibleEmployees"
                    :is-admin="isAdmin"
                    :day-grid-cols="dayGridCols"
                    :contract-label="contractLabel"
                    :is-time-tracking="isTimeTracking"
                :is-presence-tracking="isPresenceTracking"
                :is-row-locked="isRowLocked"
                :is-half-locked-by-absence="isHalfLockedByAbsence"
                :is-evening-locked-by-absence="isEveningLockedByAbsence"
                :is-validation-pending="isValidationPending"
                :can-toggle-validation="canToggleValidation"
                :is-bulk-validation-checked="isBulkValidationChecked"
                :is-bulk-validation-indeterminate="isBulkValidationIndeterminate"
                :on-toggle-validation="toggleValidation"
                :on-toggle-validation-bulk="toggleValidationBulk"
                :get-row-metrics="getRowMetrics"
                :get-row-absence-label="getRowAbsenceLabel"
                :get-presence-day-value="getPresenceDayValue"
                :on-absence-click="openAbsenceDrawer"
                :format-minutes="formatMinutes"
                class="max-h-full"
            />

                <HoursWeekView
                    v-else-if="isAdmin && viewMode === 'week'"
                    :is-week-loading="isWeekLoading"
                    :week-grid-cols="weekGridCols"
                    :weekly-summary="filteredWeeklySummary"
                    :week-day-headers="weekDayHeaders"
                    :format-minutes="formatMinutes"
                    class="max-h-full"
                />
            </div>

            <div v-if="viewMode === 'day'" class="shrink-0 px-4 pt-4 flex justify-center">
                <button
                    type="button"
                    class="rounded-lg bg-primary-500 px-6 py-2 text-md font-semibold text-white hover:bg-secondary-500"
                    :class="saveButtonClass"
                    :disabled="isSubmitting || visibleEmployees.length === 0"
                    @click="handleSave"
                >
                    Enregistrer
                </button>
            </div>
        </div>

        <AbsenceFormDrawer
            v-model="isAbsenceDrawerOpen"
            :employees="employees"
            :absence-types="absenceTypes"
            :form="absenceForm"
            :editing-absence="editingAbsence"
            :is-submitting="isAbsenceSubmitting"
            :lock-employee="true"
            :lock-dates="true"
            :show-comment="false"
            @submit="submitAbsence"
            @delete="deleteAbsenceFromDrawer"
            @cancel="closeAbsenceDrawer"
        />
    </div>
</template>

<script setup lang="ts">
const {
  isAdmin,
  viewMode,
  selectedDate,
  employeeFilter,
  sites,
  selectedSiteIds,
  employees,
  visibleEmployees,
  rows,
  absenceTypes,
  absenceForm,
  isAbsenceDrawerOpen,
  isAbsenceSubmitting,
  editingAbsence,
  filteredWeeklySummary,
  isLoading,
  isWeekLoading,
  isSubmitting,
  dayGridCols,
  weekGridCols,
  saveButtonClass,
  formattedSelectedDate,
  weekDayHeaders,
  shortcutButtonClass,
  weekShortcutButtonClass,
  getWeekShortcutLabel,
  setToday,
  setYesterday,
  setTomorrow,
  setThisWeek,
  setPreviousWeek,
  setNextWeek,
  shiftDate,
  contractLabel,
  isTimeTracking,
  isPresenceTracking,
  isRowLocked,
  isHalfLockedByAbsence,
  isEveningLockedByAbsence,
  isValidationPending,
  canToggleValidation,
  isBulkValidationChecked,
  isBulkValidationIndeterminate,
  toggleValidation,
  toggleValidationBulk,
  getRowMetrics,
  getRowAbsenceLabel,
  getPresenceDayValue,
  openAbsenceDrawer,
  submitAbsence,
  deleteAbsenceFromDrawer,
  closeAbsenceDrawer,
  formatMinutes,
  handleSave
} = useHoursPage()

useHead({
    title: 'Heures'
})
</script>
