<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ReserveditemResourceUser\Pages;
use App\Filament\Resources\ReserveditemResourceUser\RelationManagers;
use App\Models\Reserveditem;
use App\Models\Item;
use Filament\Forms;
use Filament\Schemas\Schema;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Filament\Schemas\Components\Section;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Support\Enums\IconPosition;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\IconColumn;
use Filament\Actions\Action;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;

class ReserveditemResourceUser extends Resource
{
    protected static ?string $model = Reserveditem::class;

    protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-archive-box-x-mark';

    protected static ?string $modelLabel = 'Reserve items';

    protected static ?string $slug = 'user';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->schema([
                Select::make('item_id')
                ->label('Choose an item to reserve')
                ->translateLabel()
                   ->relationship(
                       name: 'item', 
                       titleAttribute: 'desc',
                       modifyQueryUsing: fn ($query) =>  $query->where('can_be_loaned', true)->where('reserved', false)
                   )
                   ->required(),
                //Forms\Components\TextInput::make('user_id')
                //    ->label('Användare')
                //    ->default(auth()->id())
                //    ->disabledOn('create') 
                //    //->hiddenOn('create')
                //    ->readOnly(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('items.desc')
                    ->label('Name')
                    ->translateLabel()
                    ->sortable(),
                TextColumn::make('username')
                    ->label('User')
                    ->translateLabel()
                    ->sortable(),
                TextColumn::make('reserved_date')
                    ->label('Reserved date')
                    ->translateLabel()
                    ->sortable(),
                TextColumn::make('delivered_date')
                    ->label('Delivery date')
                    ->translateLabel()
                    ->sortable(),
                TextColumn::make('return_date')
                    ->label('Return date')
                    ->translateLabel()
                    ->sortable(),
                TextColumn::make('returned_date')
                    ->label('Returned')
                    ->translateLabel()
                    ->sortable(),
            ])
            ->filters([
                //
            ])
            ->actions([
            ])
            ->bulkActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }

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

    public static function getPages(): array
    {
        return [
            'index' => Pages\CreateReserveditemUser::route('/'),
        ];
    }



    public static function canViewAny(): bool
    {
        return false;
        //return auth()->user()->is_admin==false;
    }
}
