<?php

namespace App\Filament\Resources\Panel;

use Filament\Forms;
use Filament\Tables;
use Livewire\Component;
use Filament\Forms\Form;
use App\Models\Supplier;
use Filament\Tables\Table;
use Filament\Resources\Resource;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Section;
use Filament\Tables\Columns\TextColumn;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Columns\ImageColumn;
use Illuminate\Database\Eloquent\Builder;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\FileUpload;
use App\Filament\Resources\Panel\SupplierResource\Pages;
use App\Filament\Resources\Panel\SupplierResource\RelationManagers;

class SupplierResource extends Resource
{
    protected static ?string $model = Supplier::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    protected static ?int $navigationSort = 8;

    protected static ?string $navigationGroup = 'البيانات الأساسيه';

    public static function getModelLabel(): string
    {
        return __('crud.suppliers.itemTitle');
    }

    public static function getPluralModelLabel(): string
    {
        return __('crud.suppliers.collectionTitle');
    }

    public static function getNavigationLabel(): string
    {
        return __('crud.suppliers.collectionTitle');
    }

    public static function form(Form $form): Form
    {
        return $form->schema([
            Section::make()->schema([
                Grid::make(['default' => 1])->schema([
                    TextInput::make('name')
                        ->required()
                        ->string()
                        ->unique('suppliers', 'name', ignoreRecord: true)
                        ->autofocus(),

                    TextInput::make('phone')
                        ->nullable()
                        ->numeric()
                        ->step(1),

                    TextInput::make('whatsapp')
                        ->nullable()
                        ->numeric()
                        ->step(1),

                    TextInput::make('email')
                        ->nullable()
                        ->string()
                        ->email(),

                    RichEditor::make('address')
                        ->nullable()
                        ->string()
                        ->fileAttachmentsVisibility('public'),

                        TextInput::make('first_mony')
                        ->nullable()
                        ->numeric()
                        ->step(1),


                    FileUpload::make('photo')
                        ->rules(['image'])
                        ->nullable()
                        ->maxSize(1024)
                        ->image()
                        ->imageEditor()
                        ->imageEditorAspectRatios([null, '16:9', '4:3', '1:1']),
                ])->columns(2),
            ]),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->poll('60s')
            ->columns([
                TextColumn::make('name')->badge()
                ->searchable()
                ->sortable()
                ->toggleable(),

                TextColumn::make('phone')->badge()
                ->searchable()
                ->sortable()
                ->toggleable(),

                TextColumn::make('whatsapp')->badge()
                ->searchable()
                ->sortable()
                ->toggleable(),

                TextColumn::make('email')->badge()
                ->searchable()
                ->sortable()
                ->toggleable(),

                TextColumn::make('address')->limit(255)->badge()
                ->searchable()
                ->sortable()
                ->toggleable(),

                
                TextColumn::make('first_mony')->badge()
                ->searchable()
                ->sortable()
                ->toggleable(),


                ImageColumn::make('photo')->visibility('public'),
            ])
            ->filters([])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\ViewAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('id', 'desc');
    }

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

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListSuppliers::route('/'),
            'create' => Pages\CreateSupplier::route('/create'),
            'view' => Pages\ViewSupplier::route('/{record}'),
            'edit' => Pages\EditSupplier::route('/{record}/edit'),
        ];
    }
}
