import type { Employee } from '~/services/dto/employee' import { CONTRACT_TYPES } from '~/services/dto/contract' import { getEmployee } from '~/services/employees' export const useEmployeeDetailPage = () => { const route = useRoute() const employee = ref(null) const isLoading = ref(false) const activeTab = ref<'contract' | 'leave' | 'rtt' | 'mileage' | 'formation' | 'bonus' | 'observation'>('contract') const showLeaveTab = computed(() => employee.value?.currentContractNature !== 'INTERIM') const showRttTab = computed(() => employee.value?.contract?.type !== CONTRACT_TYPES.FORFAIT) const isForfait = computed(() => employee.value?.contract?.type === CONTRACT_TYPES.FORFAIT) const employeeContractWorkLabel = computed(() => { const contract = employee.value?.contract if (!contract) return '-' if (contract.type === CONTRACT_TYPES.FORFAIT) return 'Forfait - 218 jours' if (contract.weeklyHours !== null && contract.weeklyHours !== undefined) return `${contract.weeklyHours} heures` return contract.name || '-' }) const loadEmployee = async () => { const idParam = Array.isArray(route.params.id) ? route.params.id[0] : route.params.id const employeeId = Number(idParam) if (!Number.isInteger(employeeId) || employeeId <= 0) { return } isLoading.value = true try { employee.value = await getEmployee(employeeId) if (!showLeaveTab.value && activeTab.value === 'leave') { activeTab.value = 'contract' } if (!showRttTab.value && activeTab.value === 'rtt') { activeTab.value = 'contract' } leave.resetLoaded() rtt.resetLoaded() mileage.resetLoaded() formation.resetLoaded() bonus.resetLoaded() observation.resetLoaded() if (activeTab.value === 'leave' && showLeaveTab.value) { await leave.loadLeaveData() } else if (activeTab.value === 'rtt' && showRttTab.value) { await rtt.loadRttData() } else if (activeTab.value === 'mileage') { await mileage.loadMileageData() } else if (activeTab.value === 'formation') { await formation.loadFormationData() } else if (activeTab.value === 'bonus') { await bonus.loadBonusData() } else if (activeTab.value === 'observation') { await observation.loadObservationData() } else if (isForfait.value && showLeaveTab.value) { // Eager load: needed for the "X jours restants" header label on forfait employees. await leave.loadLeaveData() } } finally { isLoading.value = false } } const contract = useEmployeeContract(employee, loadEmployee) const leave = useEmployeeLeave(employee, loadEmployee) const forfaitRemainingDaysLabel = computed(() => { if (!isForfait.value) return '' const presence = leave.leaveSummary.value?.presenceDaysToToday if (presence === undefined || presence === null) return '' const remaining = 218 - presence return ` (${remaining} restants)` }) const rtt = useEmployeeRtt(employee, loadEmployee) const mileage = useEmployeeMileage(employee, loadEmployee) const formation = useEmployeeFormation(employee, loadEmployee) const bonus = useEmployeeBonus(employee, loadEmployee) const observation = useEmployeeObservation(employee, loadEmployee) watch(activeTab, (tab) => { if (tab === 'leave' && !leave.leaveDataLoaded.value && showLeaveTab.value) { leave.loadLeaveData() } else if (tab === 'rtt' && !rtt.rttDataLoaded.value && showRttTab.value) { rtt.loadRttData() } else if (tab === 'mileage' && !mileage.mileageDataLoaded.value) { mileage.loadMileageData() } else if (tab === 'formation' && !formation.formationDataLoaded.value) { formation.loadFormationData() } else if (tab === 'bonus' && !bonus.bonusDataLoaded.value) { bonus.loadBonusData() } else if (tab === 'observation' && !observation.observationDataLoaded.value) { observation.loadObservationData() } }) onMounted(async () => { await Promise.all([contract.loadContracts(), contract.loadInterimAgencies()]) await loadEmployee() }) return { employee, isLoading, activeTab, showLeaveTab, showRttTab, employeeContractWorkLabel, forfaitRemainingDaysLabel, ...contract, ...leave, ...rtt, ...mileage, ...formation, ...bonus, ...observation } }