<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 Conducteurs</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"
            :show-validation-calendar="true"
            :marked-dates="markedDates"
            :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"
            @month-change="onCalendarMonthChange"
        />

        <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="visibleEmployees.length === 0" class="rounded-lg border border-neutral-200 bg-white p-6 text-md text-neutral-600">
            Aucun conducteur accessible.
        </div>

        <div v-else class="flex min-h-0 flex-col gap-4">
            <div class="min-h-0 flex flex-col max-h-[calc(100vh-300px)]">
                <DriverHoursDayView
                    v-if="viewMode === 'day'"
                    v-model:rows="rows"
                    :employees="displayedEmployees"
                    :is-admin="isAdmin"
                    :is-site-manager="isSiteManager"
                    :day-grid-cols="dayGridCols"
                    :is-holiday="isSelectedDateHoliday"
                    :holiday-label="selectedHolidayLabel"
                    :contract-label="contractLabel"
                    :is-row-locked="isRowLocked"
                    :has-contract-at-selected-date="hasContractAtSelectedDate"
                    :is-validation-pending="isValidationPending"
                    :is-site-validation-pending="isSiteValidationPending"
                    :can-toggle-validation="canToggleValidation"
                    :can-toggle-site-validation="canToggleSiteValidation"
                    :can-create-site-validation-row-from-absence="canCreateSiteValidationRowFromAbsence"
                    :is-bulk-validation-checked="isBulkValidationChecked"
                    :is-bulk-validation-indeterminate="isBulkValidationIndeterminate"
                    :is-bulk-site-validation-checked="isBulkSiteValidationChecked"
                    :is-bulk-site-validation-indeterminate="isBulkSiteValidationIndeterminate"
                    :can-bulk-toggle-site-validation="canBulkToggleSiteValidation"
                    :on-toggle-validation="toggleValidation"
                    :on-toggle-site-validation="toggleSiteValidation"
                    :on-toggle-validation-bulk="toggleValidationBulk"
                    :on-toggle-site-validation-bulk="toggleSiteValidationBulk"
                    :get-row-metrics="getRowMetrics"
                    :get-row-absence-label="getRowAbsenceLabel"
                    :get-row-absence-style="getRowAbsenceStyle"
                    :get-row-contract-nature="getRowContractNature"
                    :get-row-updated-at="getRowUpdatedAt"
                    :on-absence-click="openAbsenceDrawer"
                    :format-minutes="formatMinutes"
                    class="max-h-[calc(100vh-300px)]"
                />

                <DriverHoursWeekView
                    v-else-if="isAdmin && viewMode === 'week'"
                    :is-week-loading="isWeekLoading"
                    :is-admin="isAdmin"
                    :week-grid-cols="weekGridCols"
                    :weekly-summary="filteredWeeklySummary"
                    :week-day-headers="weekDayHeaders"
                    :format-minutes="formatMinutes"
                    class="max-h-[calc(100vh-300px)]"
                    @open-comment="openWeekCommentDrawer"
                />
            </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"
        />

        <HoursWeekCommentDrawer
            v-if="weekCommentContext"
            v-model="isWeekCommentDrawerOpen"
            :employee-id="weekCommentContext.employeeId"
            :employee-label="weekCommentContext.employeeLabel"
            :week-start="weekCommentContext.weekStart"
            :week-end="weekCommentContext.weekEnd"
            :initial-content="weekCommentContext.content"
            :comment-id="weekCommentContext.commentId"
            @saved="reloadWeeklySummary"
        />
    </div>
</template>

<script setup lang="ts">
const {
  isAdmin,
  isSiteManager,
  viewMode,
  selectedDate,
  employeeFilter,
  sites,
  selectedSiteIds,
  employees,
  visibleEmployees,
  displayedEmployees,
  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,
  isRowLocked,
  hasContractAtSelectedDate,
  isValidationPending,
  isSiteValidationPending,
  canToggleValidation,
  canToggleSiteValidation,
  canCreateSiteValidationRowFromAbsence,
  isBulkValidationChecked,
  isBulkValidationIndeterminate,
  isBulkSiteValidationChecked,
  isBulkSiteValidationIndeterminate,
  canBulkToggleSiteValidation,
  toggleValidation,
  toggleSiteValidation,
  toggleValidationBulk,
  toggleSiteValidationBulk,
  getRowMetrics,
  getRowAbsenceLabel,
  getRowAbsenceStyle,
  getRowContractNature,
  getRowUpdatedAt,
  openAbsenceDrawer,
  submitAbsence,
  deleteAbsenceFromDrawer,
  closeAbsenceDrawer,
  formatMinutes,
  isSelectedDateHoliday,
  selectedHolidayLabel,
  handleSave,
  markedDates,
  onCalendarMonthChange,
  isWeekCommentDrawerOpen,
  weekCommentContext,
  openWeekCommentDrawer,
  reloadWeeklySummary
} = useDriverHoursPage()

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