<?php

namespace App\Filament\Resources;

use App\Filament\Resources\WinningBidsResource\Pages;
use App\Filament\Resources\WinningBidsResource\RelationManagers;
use App\Models\WinningBids;
use UnitEnum;
use BackedEnum;
use Filament\Schemas\Schema;
use Filament\Resources\Resource;
use Filament\Tables\Table;
use Filament\Tables;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use App\Models\Bidders;
use App\Models\Items;
use Filament\Forms\Components\Select;
use Filament\Actions\EditAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;

class WinningBidsResource extends Resource
{
    protected static ?string $model = WinningBids::class;

    protected static string | BackedEnum | null $navigationIcon = 'heroicon-o-rectangle-stack';

    protected static string | UnitEnum | null $navigationGroup = 'Silent Auction';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Select::make('winning_item_num')
                    ->label('Item')
                    ->options(Items::orderBy('item_assigned_num')->pluck('item_assigned_num', 'iditems'))
                    ->searchable(),
                Select::make('winning_bidder_num')
                    ->label('Bidder')
                    ->options(       
                        Bidders::orderBy('bidder_assigned_number')
                        ->pluck('bidder_assigned_number', 'idbidders')
                    )
                    ->searchable(),
                TextInput::make('winning_cost')
                    ->label('Winning Bid')
                    ->prefix('$'),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('items.item_assigned_num')
                    ->label('Item')
                    ->sortable(),
                TextColumn::make('items.item_desc')
                    ->label('Item Description')
                    ->limit(15),                    
                TextColumn::make('bidders.bidder_assigned_number')
                    ->label('Bidder Number')
                    ->sortable(),
                TextColumn::make('winning_cost')
                    ->label('Winning Bid Amt')
                    ->money('USD')
                    ->sortable(),
            ])
            ->filters([
                //
            ])
            ->recordActions([
                EditAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('items.item_assigned_num');            
    }
    
    public static function getRelations(): array
    {
        return [
            //
        ];
    }
    
    public static function getPages(): array
    {
        return [
            'index' => Pages\ListWinningBids::route('/'),
            'create' => Pages\CreateWinningBids::route('/create'),
            'edit' => Pages\EditWinningBids::route('/{record}/edit'),
        ];
    }    
}
