<?php

namespace App\Filament\Pages;

use Filament\Pages\Page;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Pages\Dashboard\Concerns\HasFiltersForm;
use Filament\Tables\Filters\SelectFilter;
use Illuminate\Support\Facades\DB;
use App\Models\Plant;
use App\Models\RequestQuotation;
use App\Models\RfqTransporterBid;
use Filament\Facades\Filament;
use Filament\Widgets\Widget;
use Illuminate\Support\Facades\Auth;
use Filament\Forms\Components\Section;
use Filament\Forms;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;


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

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

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

    use HasFiltersForm;

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

    public function filtersForm(Form $form): Form
    {
       return $form
           ->statePath('filters')
           ->schema([
            Section::make('')
                ->schema([
            //    Select::make('plant')
            //         ->label('Select Plant')
            //         ->reactive()
            //         ->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();
            //         })
            //         ->afterStateUpdated(function ($state,callable $set) {
            //             session(['selected_plant' => $state]);
            //                 // $set('rfq_number', null);
            //                 session()->forget('rfq_number');
            //         }),

                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]);
                        $set('transport_name', null);
                        session()->forget('transport_name');
                    }),

                Select::make('transport_name')
                    ->label('User name')
                    ->reactive()
                    ->options(function (callable $get) {
                        $rfqId = $get('rfq_number');

                        if (!$rfqId) {
                            return [];
                        }

                        $user = Filament::auth()->user();

                        if ($user->hasRole(['Super Admin', 'Rfq Supervisor'])) {
                            return RfqTransporterBid::query()
                                ->where('request_quotation_id', $rfqId)
                                ->whereNotNull('transporter_name')
                                ->distinct()
                                ->pluck('transporter_name', 'transporter_name')
                                ->toArray();
                        }

                        return RfqTransporterBid::query()
                            ->where('request_quotation_id', $rfqId)
                            ->where('transporter_name', $user->name)
                            ->distinct()
                            ->pluck('transporter_name', 'transporter_name')
                            ->toArray();
                    })
                    ->afterStateUpdated(function ($state, callable $set) {
                        session(['transport_name' => $state]);
                    }),

                // Select::make('transport_name')
                //     ->label('User name')
                //     ->reactive()
                //     ->options(function () {
                //         $user = Filament::auth()->user();

                //         if ($user->hasRole(['Super Admin', 'Rfq Supervisor'])) {
                //             return RfqTransporterBid::query()
                //                 ->whereNotNull('transporter_name')
                //                 ->distinct()
                //                 ->pluck('transporter_name', 'transporter_name')
                //                 ->toArray();
                //         }

                //         return RfqTransporterBid::query()
                //         ->where('transporter_name', $user->name)
                //         ->distinct()
                //         ->pluck('transporter_name', 'transporter_name')
                //         ->toArray();
                //     })
                //     ->afterStateUpdated(function ($state, callable $set) {
                //         session(['transport_name' => $state]);
                //         $set('rfq_number', null);
                //         session()->forget('rfq_number');
                //     }),

                // Select::make('rfq_number')
                //     ->label('Select RFQ Number')
                //     ->reactive()
                //     ->options(function (callable $get) {
                //         $transportName = $get('transport_name');

                //         if (!$transportName) {
                //             return [];
                //         }

                //         return RequestQuotation::where('transporter_name', $transportName)
                //             ->pluck('rfq_number', 'rfq_number')
                //             ->toArray();
                //     })
                //     ->afterStateUpdated(function ($state) {
                //         session(['rfq_number' => $state]);
                //     }),
                ])
                ->columns(2),
            ]);
    }

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