<?php

namespace App\Filament\Pages;

use App\Models\RequestQuotation;
use Filament\Pages\Page;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Form;
use Filament\Pages\Dashboard\Concerns\HasFiltersForm;
use Illuminate\Support\Facades\Auth;

class RfqOverview extends Page
{
    protected static ?string $navigationIcon = 'heroicon-o-document-text';

    protected static string $view = 'filament.pages.rfq-overview';

    protected static ?string $navigationGroup = 'RFQ Dashboard';

    use HasFiltersForm;

    public function mount(): void
    {
        session()->forget(['rfq_id']);
        $this->filtersForm->fill([
            'rfq_id' => null
        ]);
    }

    public function filtersForm(Form $form): Form
    {
       return $form
           ->statePath('filters')
           ->schema([
            Section::make('')
                ->schema([
                Select::make('rfq_number')
                    ->label('Select RFQ Number')
                    ->reactive()
                    ->options(function (callable $get) {
                        return RequestQuotation::orderBy('rfq_number')
                        ->pluck('rfq_number', 'id')
                        ->toArray();
                    })
                    ->afterStateUpdated(function ($state, callable $set) {
                        session(['rfq_id' => $state]);
                    }),
                ])
                ->columns(1),
            ]);
    }

    public static function canAccess(): bool
    {
        return Auth::check() && Auth::user()->can('view rfq overview dashboard');
    }
}
