<?php

namespace App\Filament\Resources\Panel\SaleResource\RelationManagers;

use Filament\Forms;
use Filament\Tables;
use Filament\Forms\Form;
use Filament\Tables\Table;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Select;
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\SaleResource;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Forms\Get;
use Filament\Forms\Set;

class ProductsRelationManager extends RelationManager
{
    protected static string $relationship = 'products';

    protected static ?string $recordTitleAttribute = 'name';

    public function form(Form $form): Form
    {
        return $form->schema([
            Grid::make(['default' => 1])->schema([

                TextInput::make('quantity')
                    ->required()
                    ->numeric()
                    ->step(1)
                    ->autofocus()
                    ->live(onBlur: true)
                    ->afterStateUpdated(function (Set $set, Get $get) { // Corrected argument order
                        static::calculateTotals($get, $set);
                    }),

                TextInput::make('price')
                    ->required()
                    ->numeric()
                    ->step(1)
                    ->live(onBlur: true)
                    ->step(1)
                    ->afterStateUpdated(function (Set $set, Get $get) { // Corrected argument order
                        static::calculateTotals($get, $set);
                    }),

                TextInput::make('total')
                    ->required()
                    ->numeric()
                    ->readOnly()
                    ->step(1),
            ]),
        ]);
    }
    public static function calculateTotals(Get $get, Set $set): void
    {
        $quantity = $get('quantity');
        $price = $get('price');
        $total = $quantity * $price;
        $set('total', $total);
    }
    public function table(Table $table): Table
    {
        return $table
            ->allowDuplicates()
            ->columns([
                TextColumn::make('productCategory.name'),

                TextColumn::make('name'),

                TextColumn::make('package'),

                ImageColumn::make('photo')->visibility('public'),


                TextColumn::make('quantity'),

                TextColumn::make('price'),

                TextColumn::make('total'),
            ])
            ->filters([])
            ->headerActions([

                Tables\Actions\AttachAction::make()->form(
                    fn(Tables\Actions\AttachAction $action): array => [
                        $action->getRecordSelect(),


                        // Select::make('product_id')
                        // ->label('Product')
                        // ->required()
                        // ->searchable()
                        // ->options(fn () => \App\Models\Product::pluck('name', 'id')->toArray())
                        // ->reactive()
                        // ->afterStateUpdated(function (Set $set, $state) {
                        //     // Fetch the product's price when a product is selected
                        //     $product = \App\Models\Product::find($state);
                        //     $set('price', $product?->sell_price ?? 0); // Set the price or default to 0 if no product is found
                        // }),
                    
                    TextInput::make('price')
                        ->required()
                        ->numeric()
                        ->step(1)

                        ->afterStateUpdated(function (Set $set, Get $get) {
                            static::calculateTotals($get, $set);
                        })

                        // ->readOnly()
                        , // Make it read-only to prevent manual edits
                    
                            TextInput::make('quantity')
                            ->required()
                            ->numeric()
                            ->step(1)
                            ->autofocus()
                            ->live(onBlur: true)
                            ->afterStateUpdated(function (Set $set, Get $get) {
                                static::calculateTotals($get, $set);
                            }),


                            TextInput::make('total')
                            ->required()
                            ->numeric()
                            ->readOnly()
                            ->step(1),
                    ]
                ),

            ])
            ->actions([

                Tables\Actions\EditAction::make(),
                Tables\Actions\DetachAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([

                    Tables\Actions\DetachBulkAction::make(),
                ]),
            ]);
    }
}
