<?php

namespace App\Livewire;

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


class PalletFromLocatorDataTable extends Component
{
    public $plantId;

    public $locatorNo, $palletNumber, $newpallet;

    public $showSessionRecords = false;

    public $showRemovedRecords = false;

    public $records = [];

    public $scannedRecords = [];
    public $removeRecords = [];

    public $latestPalletDateTime = null;
    public $createdBy;

    protected $listeners = [
        'loadData' => 'loadlocatorData',
        'open-confirm-modal' => 'openConfirmModal',
    ];

    // public function generatePallet($plantId)
    // {
    //     $year = now()->format('y');
    //     $month = now()->format('m');
    //     $prefix = "EP-{$year}{$month}";

    //     $lastPallet = PalletValidation::where('pallet_number', 'like', "{$prefix}%")
    //         ->where('plant_id', $plantId)
    //         ->orderByDesc('pallet_number')
    //         ->first();

    //     $newNumber = $lastPallet
    //         ? str_pad(intval(substr($lastPallet->pallet_number, -3)) + 1, 3, '0', STR_PAD_LEFT)
    //         : '001';

    //     $newPalletNumber = "{$prefix}{$newNumber}";


    //     session(['latestPalletNumber' => $newPalletNumber]);

    //       $url = route('download-qr-pdf', ['palletNo' => $newPalletNumber]);
    //       $this->js(<<<JS
    //           window.dispatchEvent(new CustomEvent('open-pdf', {
    //               detail: {
    //                   url: "{$url}"
    //               }
    //           }));
    //       JS);
    // }

    public function openConfirmModal($locatorNo, $plantId)
    {
        $this->locatorNo = $locatorNo;
        $this->plantId = $plantId;
        $this->dispatch('open-modal', id: 'confirm-process-modal');
    }

    public function loadlocatorData($records, $plantId)
    {
        $this->plantId = $plantId;
        $this->records = $records;
    }

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

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