forget(['selected_plant']); session()->forget(['selected_date']); session()->forget(['selected_name']); session()->forget(['selected_time']); session()->forget(['valid_sessions']); $this->filtersForm->fill([ 'plant' => null, 'date' => null, 'time_range' => null, 'guard_name' => null ]); } public function filtersForm(Form $form): Form { return $form ->statePath('filters') ->schema([ Select::make('plant') ->options(Plant::pluck('name', 'id')) ->label('Select Plant') ->reactive() ->required() ->afterStateUpdated(function ($state,callable $set) { if (is_null($state)) { session()->forget('selected_plant'); } else { session(['selected_plant' => $state]); } session(['selected_plant' => $state]); $set('date', null); session()->forget('selected_date'); $set('guard_name', null); session()->forget('selected_name'); $set('time_range', null); session()->forget('selected_time'); // $this->dispatch('patrolEntryChart'); }), DatePicker::make('date') ->label('Select Date') ->placeholder('Select Date') ->reactive() ->required() ->beforeOrEqual(now()) ->default(now()->format('Y-m-d')) ->afterStateUpdated(function ($state,callable $set) { session(['selected_date' => $state]); $set('guard_name', null); session()->forget('selected_name'); $set('time_range', null); session()->forget('selected_time'); // $this->dispatch('patrolEntryChart'); }), Select::make('guard_name') ->label('Guard Name') ->options(function (callable $get) { $plantId = $get('plant'); if (!$plantId) { return []; } return GuardPatrolEntry::where('guard_patrol_entries.plant_id', $plantId) ->join('guard_names', 'guard_patrol_entries.guard_name_id', '=', 'guard_names.id') ->select('guard_names.id', 'guard_names.name') ->groupBy('guard_names.id', 'guard_names.name') ->pluck('guard_names.name', 'guard_names.id') ->toArray(); }) ->reactive() ->required() ->afterStateUpdated(function ($state,callable $set) { if (is_null($state)) { session()->forget('selected_name'); } else { session(['selected_name' => $state]); } // Clear the time range field and its session $set('time_range', null); session()->forget('selected_time'); }), // Select::make('time_range') // ->label('Patrol Time Range') // ->options(function (callable $get) { // $plantId = $get('plant'); // $guardName = trim($get('guard_name')); // $date = $get('date'); // if (!$plantId || !$guardName || !$date) { // session(['valid_sessions' => []]); // return []; // } // // Get all patrols for the guard, plant, and date // $patrols = GuardPatrolEntry::where('guard_patrol_entries.plant_id', $plantId) // ->join('guard_names', 'guard_patrol_entries.guard_name_id', '=', 'guard_names.id') // ->where('guard_patrol_entries.guard_name_id', $guardName) // ->whereDate('guard_patrol_entries.patrol_time', $date) // ->orderBy('guard_patrol_entries.patrol_time') // ->get(['guard_patrol_entries.patrol_time', 'guard_patrol_entries.check_point_name_id']); // if ($patrols->isEmpty()) { // session(['valid_sessions' => []]); // return []; // } // $firstCheckpoint = CheckPointName::where('plant_id', $plantId) // ->orderBy('id') // ->first(['id', 'name']); // $lastCheckpoint = CheckPointName::where('plant_id', $plantId) // ->orderByDesc('id') // ->first(['id', 'name']); // $startCheckpointId = $firstCheckpoint->id; // $endCheckpointId = $lastCheckpoint->id; // $firstPatrolCheckpointId = $patrols[0]->check_point_name_id; // $firstPatrolCheckpointName = CheckPointName::where('id', $firstPatrolCheckpointId)->value('name'); // $firstCheckpointName = $firstCheckpoint->name; // $allSessionsInvalid = ($firstPatrolCheckpointName != $firstCheckpointName); // $sessions = []; // $currentSession = []; // foreach ($patrols as $patrol) { // $currentSession[] = $patrol; // if ($patrol->check_point_name_id == $endCheckpointId) { // $sessions[] = $currentSession; // $currentSession = []; // } // } // if (!empty($currentSession)) { // $sessions[] = $currentSession; // } // $options = []; // $validSessions = []; // foreach ($sessions as $session) { // $start = Carbon::parse($session[0]->patrol_time)->format('H:i:s'); // $end = Carbon::parse(end($session)->patrol_time)->format('H:i:s'); // $timeRange = "$start - $end"; // $options[$timeRange] = $timeRange; // // Check if session is valid // $isValid = !$allSessionsInvalid && // ($session[0]->check_point_name_id == $startCheckpointId) && // (end($session)->check_point_name_id == $endCheckpointId); // $validSessions[$timeRange] = $isValid; // } // // Store validSessions in session // session(['valid_sessions' => $validSessions]); // return $options; // }) // ->reactive() // ->required() // ->afterStateUpdated(function ($state) { // session(['selected_time' => $state]); // }), Select::make('time_range') ->label('Patrol Time Range') ->options(function (callable $get) { $plantId = $get('plant'); $guardName = trim($get('guard_name')); $date = $get('date'); if (!$plantId || !$guardName || !$date) { session(['valid_sessions' => []]); return []; } // Get all patrols for the guard, plant, and date $patrols = GuardPatrolEntry::where('guard_patrol_entries.plant_id', $plantId) ->join('guard_names', 'guard_patrol_entries.guard_name_id', '=', 'guard_names.id') ->where('guard_patrol_entries.guard_name_id', $guardName) ->whereDate('guard_patrol_entries.patrol_time', $date) ->orderBy('guard_patrol_entries.patrol_time') ->get(['guard_patrol_entries.patrol_time', 'guard_patrol_entries.check_point_name_id']); if ($patrols->isEmpty()) { session(['valid_sessions' => []]); return []; } // Get the first and last sequence for the plant $firstSequence = CheckPointTime::where('plant_id', $plantId) ->orderBy('sequence_number') ->first(['check_point1_id', 'check_point2_id']); $lastSequence = CheckPointTime::where('plant_id', $plantId) ->orderByDesc('sequence_number') ->first(['check_point1_id', 'check_point2_id']); $startCheckpointId = $firstSequence ? $firstSequence->check_point1_id : null; $endCheckpointId = $lastSequence ? $lastSequence->check_point2_id : null; $firstCheckpointName = $startCheckpointId ? CheckPointName::where('id', $startCheckpointId)->value('name') : null; $lastCheckpointName = $endCheckpointId ? CheckPointName::where('id', $endCheckpointId)->value('name') : null; $firstPatrolCheckpointId = $patrols[0]->check_point_name_id; $firstPatrolCheckpointName = CheckPointName::where('id', $firstPatrolCheckpointId)->value('name'); $allSessionsInvalid = ($firstPatrolCheckpointName != $firstCheckpointName); $sessions = []; $currentSession = []; foreach ($patrols as $patrol) { $currentSession[] = $patrol; if ($patrol->check_point_name_id == $endCheckpointId) { $sessions[] = $currentSession; $currentSession = []; } } if (!empty($currentSession)) { $sessions[] = $currentSession; } $options = []; $validSessions = []; foreach ($sessions as $session) { $start = Carbon::parse($session[0]->patrol_time)->format('H:i:s'); $end = Carbon::parse(end($session)->patrol_time)->format('H:i:s'); $timeRange = "$start - $end"; $options[$timeRange] = $timeRange; $isValid = !$allSessionsInvalid && ($session[0]->check_point_name_id == $startCheckpointId) && (end($session)->check_point_name_id == $endCheckpointId); $validSessions[$timeRange] = $isValid; } session(['valid_sessions' => $validSessions]); return $options; }) ->reactive() ->required() ->afterStateUpdated(function ($state) { session(['selected_time' => $state]); }), ]) ->columns(4); } public function getWidgets(): array { return [ GuardPatrolHourlyChart::class, ]; } public static function canAccess(): bool { return Auth::check() && Auth::user()->can('view guard patrol hourly count dashboard'); } }