<?php

namespace App\Livewire;

use App\Models\PalletValidation;
use Filament\Facades\Filament;
use Filament\Notifications\Notification;
use Livewire\Component;


class PalletDataTable extends Component
{
    public $plantId;

    public $palletNumber;

    public $snoCount = 0;

    public $records = [];

    protected $listeners = [
        //'removeSno' => 'requestRemoveSerialNumber',
        'loadData' => 'loadPalletData',
    ];



    public function loadPalletData($palletNumber, $plantId)
    {
        $this->plantId = $plantId;
        $this->palletNumber = $palletNumber;
        $this->records = [];

        $this->records = PalletValidation::query()
            ->where('plant_id', $this->plantId)
            ->where('pallet_number', $this->palletNumber)
            ->orderBy('scanned_at')
            ->get()
            ->map(function ($record) {
                return [
                    'created_at' => $record->created_at,
                    'created_by' => $record->created_by ?? '',
                    'pallet_number' => $record->pallet_number,
                    'serial_number' => $record->serial_number,
                    'scanned_at' => $record->scanned_at,
                    'scanned_by' => $record->scanned_by ?? '',
                ];
            })
            ->toArray();

    }


    // public function requestRemoveSerialNumber($serialNumber, $palletNumber,$plantId)
    // {

    //     $user = Filament::auth()->user();

    //     $operatorName = $user->name;

    //     $found = collect($this->records)->contains(function ($record) use ($serialNumber) {
    //         return $record['serial_number'] == $serialNumber;
    //     });

    //     if (!$found) {
    //         Notification::make()
    //             ->title('Serial number not found in the pallet data table. Cannot delete.')
    //             ->danger()
    //             ->send();
    //         return;
    //     }

    //     // Proceed to delete from the database
    //     $deleted = PalletValidation::where('plant_id', $plantId)
    //         ->where('pallet_number', $palletNumber)
    //         ->where('serial_number', $serialNumber)
    //         ->forceDelete();

    //     if ($deleted)
    //     {
    //         Notification::make()
    //             ->title('Serial number deleted successfully.')
    //             ->success()
    //             ->send();

    //             $this->form->fill([
    //                 'plant_id'      => $this->plantId,
    //                 'pallet_number' => $palletNumber,
    //                 'removeSno_number' => null,
    //                 'pending_pallet_list' => $this->pendingPallet,
    //                 'Sno_quantity' => $this->snoCount,
    //                 'created_by'    => $operatorName,
    //                 'scanned_by'    => $operatorName,
    //             ]);

    //             $this->snoCount = PalletValidation::where('plant_id', $plantId)
    //             ->where('pallet_number', $palletNumber)
    //             ->count();

    //             $this->dispatch('updateSnoQuantity', $this->snoCount);

    //         //reload the data table
    //         $this->loadPalletData($this->palletNumber, $this->plantId);
    //     }
    //     else
    //     {
    //         Notification::make()
    //             ->title('Failed to delete serial number.')
    //             ->danger()
    //             ->send();
    //     }

    // }


    public function render()
    {
        return view('livewire.pallet-data-table');
    }
}
