<?php

namespace App\Filament\Pages;

use App\Models\GuardPatrolEntry;
use App\Models\Plant;
use Carbon\Carbon;
use Filament\Facades\Filament;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Form;
use Filament\Pages\Dashboard\Concerns\HasFiltersForm;
use Filament\Pages\Page;
use Illuminate\Support\Facades\Auth;

class GuardPatrolEntryDashboard extends Page
{
    use HasFiltersForm;

    protected static ?string $navigationIcon = 'heroicon-o-document-text';

    protected static ?string $navigationGroup = 'Guard DashBoard';

    protected static string $view = 'filament.pages.guard-patrol-entry-dashboard';

    public function mount(): void
    {
        // session()->forget(['select_guard_plant', 'select_guard_date']);
        $this->filtersForm->fill([
            'plant' => null,
            'date' => null,
        ]);
    }

    public function filtersForm(Form $form): Form
    {
        return $form
            ->statePath('filters') // Explicitly set where to store form data
            ->schema([
                Select::make('plant')
                    ->label('Select Plant')
                    // ->options(Plant::pluck('name', 'id'))
                    ->options(function (callable $get) {
                        $userHas = Filament::auth()->user()->plant_id;
                        return ($userHas && strlen($userHas) > 0) ? Plant::where('id', $userHas)->pluck('name', 'id')->toArray() : Plant::pluck('name', 'id')->toArray();
                    })
                    ->reactive()
                    ->required()
                    ->afterStateUpdated(function ($state, callable $set, callable $get){
                        // $this->dispatch('invoiceChart');
                        if(!$state)
                        {
                            $set('date', now()->format('Y-m-d')); // H:i:s
                            // session(['select_guard_plant' => $state]);
                            // session(['select_guard_date' => $get('date')]);
                        } else {
                            if(!$get('date'))
                            {
                                $set('date', now()->format('Y-m-d'));
                                // session(['select_guard_plant' => $state]);
                                // session(['select_guard_date' => $get('date')]);
                            }
                            // else {
                            //     session(['select_guard_plant' => $state]);
                            //     session(['select_guard_date' => $get('date')]);
                            // }
                        }
                        $this->dispatch('loadGuardData', $state, $get('date')); //->format('Y-m-d')
                    }),
                DatePicker::make('date')
                    ->label('Select Date')
                    ->placeholder('Select Date')
                    ->reactive()
                    ->required()
                    ->beforeOrEqual(now())
                    ->default(now()->format('Y-m-d'))
                    ->afterStateUpdated(function ($state, callable $set, callable $get){
                        if(!$get('plant'))
                        {
                            $set('date', now()->format('Y-m-d'));
                            // session(['select_guard_plant' => $get('plant')]);
                            // session(['select_guard_date' => $state]);
                        } else {
                            if(!$get('date'))
                            {
                                $set('date', now()->format('Y-m-d'));
                                // session(['select_guard_plant' => $get('plant')]);
                                // session(['select_guard_date' => $state]);
                            }
                            // else {
                            //     $records = GuardPatrolEntry::whereDate('patrol_time', $state)->where('plant_id', $get('plant'))->orderBy('patrol_time', 'asc')->first(); //desc
                            //     //dd($get('plant'), $state, $records->patrol_time);//->toTimeString()
                            //     session(['select_guard_plant' => $get('plant')]);
                            //     session(['select_guard_date' => $state]);
                            // }
                        }
                        // $this->dispatch('invoiceChart');
                        $this->dispatch('loadGuardData', $get('plant'), $state);//->format('Y-m-d')
                    })
            ])
            ->columns(2);
    }


    public static function getNavigationLabel(): string
    {
        return 'Guard Patrol Status Table';
    }

    public function getHeading(): string
    {
        return 'Guard Patrol Entry Status';
    }

    public static function canAccess(): bool
    {
        return Auth::check() && Auth::user()->can('view guard patrol entry status dashboard');
    }
}
