Forms\Components\Select::make('common_area_id')
->label('Tipo de área')
->required()
->relationship('commonareas', 'nombre')
->reactive()
->afterStateUpdated(function ($state, callable $get, callable $set) {
$commonArea = CommonArea::find($state);
if($commonArea) {
$set('valor', $commonArea->valor);
$set('tramoreserva', "{$commonArea->rangefrom} - {$commonArea->rangeuntil}");
$set('availabledays', $commonArea->availabledays);
// Show available days
//dd($commonArea->availabledays);
} else {
$set('valor', '');
$set('tramoreserva', '');
$set('availabledays', []);
}
}),
Forms\Components\Hidden::make('availabledays')
->reactive(),
Forms\Components\DatePicker::make('fecha')
->label('Fecha')
->minDate(now()->toDateString()) // Fecha mínima de solicitud la actual o mayor
->required()
->reactive()
->afterStateUpdated(function ($state, callable $get, callable $set) {
$commonArea = CommonArea::find($get('common_area_id'));
if ($commonArea && !empty($commonArea->availabledays)) {
$set('availabledays', $commonArea->availabledays);
} else {
$set('availabledays', []);
}
})
->disabledDates(function (callable $get) {
$availableDays = $get('availabledays');
if (!$availableDays) {
return [];
}
// Mapeo de los días de la semana a sus índices correspondientes
$dayMap = [
'Domingo' => 0,
'Lunes' => 1,
'Martes' => 2,
'Miercoles' => 3,
'Jueves' => 4,
'Viernes' => 5,
'Sabado' => 6,
];
$allowedDays = array_map(fn($day) => $dayMap[$day] ?? null, $availableDays);
$allowedDays = array_filter($allowedDays, fn($day) => $day !== null);
$unavailableDays = array_diff([0,1,2,3,4,5,6], $allowedDays);
return function (\Carbon\Carbon $date) use ($unavailableDays) {
return in_array($date->dayOfWeek, $unavailableDays);
};
}),