<?php

namespace App\Filament\Admin\Resources;

use App\Enums\IconEnum;
use App\Models\Customer;
use App\Models\Order;
use App\Models\PackingSlip;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Split;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class PackingSlipResource extends Resource
{
    protected static ?string $model = PackingSlip::class;

    protected static ?string $navigationIcon = IconEnum::PACKING_SLIP->value;

    protected static ?string $navigationGroup = 'Production';

    protected static ?int $navigationSort = 2;

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Section::make([
                    Split::make([
                        Grid::make(1)
                            ->schema([
                                DatePicker::make('date_received')
                                    ->default(today())
                                    ->required(),
                                Select::make('customer_id')
                                    ->label('Customer')
                                    ->options(Customer::all()->pluck('company_name', 'id'))
                                    ->reactive()
                                    ->searchable(),
                                Select::make('order_id')
                                    ->label('Order')
                                    ->options(fn ($get): array => Order::where('customer_id', $get('customer_id') ?? null)
                                        ->get()
                                        ->pluck('customer_po', 'id')
                                        ->toArray())
                                    ->searchable(),
                            ])
                            ->columnSpan(1),

                        Grid::make(1)
                            ->schema([
                                TextArea::make('contents')
                                    ->rows(9),
                            ])
                            ->columnSpan(1),
                    ]),
                ]),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('date_received')
                    ->sortable()
                    ->searchable(),
                TextColumn::make('order.customer_po')
//                    ->url(fn ($record) => $record->to)
                    ->url(fn ($record) => OrderResource::getUrl('edit', ['record' => $record->id]))
                    ->weight('bold')
                    ->color('code')
                    ->sortable()
                    ->searchable(),
                TextColumn::make('contents')
                    ->extraHeaderAttributes(['class' => 'w-full']),
                //                TextColumn::make('amount')
                //                    ->label('Quantity'),
                TextColumn::make('order.customer.company_name')
                    ->sortable()
                    ->searchable(),
            ])
            ->defaultSort('date_received', 'desc')
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => \App\Filament\Admin\Resources\PackingSlipResource\Pages\ListPackingSlips::route('/'),
            //            'create' => \App\Filament\Admin\Resources\PackingSlipResource\Pages\CreatePackingSlip::route('/create'),
            //            'edit' => \App\Filament\Admin\Resources\PackingSlipResource\Pages\EditPackingSlip::route('/{record}/edit'),
        ];
    }
}