<?php

namespace App\Filament\Pages;


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

class InvoiceDashboard extends Page
{

    use HasFiltersForm;

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

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

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


    public function mount(): void
    {
        session()->forget(['selec_plant', 'select_invoice']);
        session()->forget(['from_date']);
        session()->forget(['to_date']);
        $this->filtersForm->fill([
            'plant' => null,
            'invoice' => null,
            'from_date' => null,
            'to_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()
                    ->afterStateUpdated(function ($state) {
                        session(['selec_plant' => $state]);
                        $this->dispatch('invoiceChart');
                    }),
                Select::make('invoice')
                    ->options([
                        'serial_invoice' => 'Serial Invoice',
                        'individual_material' => 'Individual Material Invoice',
                        'bundle_material' => 'Bundle Material Invoice',
                    ])
                    ->label('Select Invoice')
                    ->reactive()
                    ->default(0)
                    ->afterStateUpdated(function ($state) {
                        session(['select_invoice' => $state]);
                        $this->dispatch('invoiceChart');
                    }),
                DatePicker::make('created_from')
                    ->label('Created From')
                    ->reactive()
                    ->afterStateUpdated(function ($state,callable $set) {
                        session(['from_date' => $state]);
                        $this->dispatch('invoiceChart');
                    }),
                DatePicker::make('created_to')
                    ->label('Created To')
                    ->reactive()
                    ->afterStateUpdated(function ($state,callable $set) {
                        session(['to_date' => $state]);
                        $this->dispatch('invoiceChart');
                    }),
            ])
            ->columns(4);
    }

    public static function getNavigationLabel(): string
    {
        return 'Invoice';
    }

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

}
