<?php

namespace App\Filament\Resources;

use App\Filament\Resources\MovimientoCajaResource\Pages;
use App\Filament\Resources\MovimientoCajaResource\RelationManagers;
use App\Models\MovimientoCaja;
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;
use Filament\Forms\Components\Select;

class MovimientoCajaResource extends Resource
{
    protected static ?string $model = MovimientoCaja::class;

    protected static ?string $navigationIcon = 'heroicon-o-archive-box';

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

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Select::make('caja_id')
                    ->label('Caja (Fecha de apertura)')
                    ->options(function () {
                        return \App\Models\Caja::where('estado', 'Abierta')
                            ->pluck('created_at', 'id')
                            ->mapWithKeys(function ($createdAt, $id) {
                                return [$id => \Carbon\Carbon::parse($createdAt)->format('d/m/Y H:i')];
                            });
                    })
                    ->required(),

                Forms\Components\TextInput::make('tipo')
                    ->required(),

                Forms\Components\TextInput::make('monto')
                    ->required()
                    ->numeric(),

                Forms\Components\Textarea::make('descripcion')
                    ->columnSpanFull(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('caja.created_at')
                    ->numeric()
                    ->sortable(),
                Tables\Columns\TextColumn::make('tipo'),
                Tables\Columns\TextColumn::make('monto')
                    ->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\ListMovimientoCajas::route('/'),
            'create' => Pages\CreateMovimientoCaja::route('/create'),
            'edit' => Pages\EditMovimientoCaja::route('/{record}/edit'),
        ];
    }
}
