<?php

namespace App\Filament\Resources;

use App\Filament\Resources\DetalleCompraResource\Pages;
use App\Filament\Resources\DetalleCompraResource\RelationManagers;
use App\Models\DetalleCompra;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class DetalleCompraResource extends Resource
{
    protected static ?string $model = DetalleCompra::class;

    protected static ?string $navigationIcon = 'heroicon-o-shopping-cart';

    public static function canViewAny(): bool
    {
        return auth()->user()->can('ver detalle de compras');
    }


    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Select::make('compra_id')
                    ->relationship('compra', 'id')
                    ->required(),
                Forms\Components\Select::make('producto_id')
                    ->relationship('producto', 'id')
                    ->required(),
                Forms\Components\TextInput::make('cantidad')
                    ->required()
                    ->numeric(),
                Forms\Components\TextInput::make('precio_unitario')
                    ->required()
                    ->numeric(),
                Forms\Components\TextInput::make('subtotal')
                    ->required()
                    ->numeric(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('compra.id')
                    ->numeric()
                    ->sortable(),
                Tables\Columns\TextColumn::make('producto.id')
                    ->numeric()
                    ->sortable(),
                Tables\Columns\TextColumn::make('cantidad')
                    ->numeric()
                    ->sortable(),
                Tables\Columns\TextColumn::make('precio_unitario')
                    ->numeric()
                    ->sortable(),
                Tables\Columns\TextColumn::make('subtotal')
                    ->numeric()
                    ->sortable(),
                Tables\Columns\TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
             ->paginated(true)
            //->paginationPageOptions([50])
            ->defaultSort('created_at', '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' => Pages\ListDetalleCompras::route('/'),
            'create' => Pages\CreateDetalleCompra::route('/create'),
            'edit' => Pages\EditDetalleCompra::route('/{record}/edit'),
        ];
    }
}
