<?php

namespace App\Filament\Resources\PalletValidationResource\Pages;

use App\Filament\Resources\PalletValidationResource;
use App\Models\LocatorInvoiceValidation;
use App\Models\PalletValidation;
use Filament\Actions;
use Filament\Facades\Filament;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\View\View;

class CreatePalletValidation extends CreateRecord
{
    public $plantId;

    public $pallet_number;

    public $palletNo;

    public $pendingPallet;

    public $snoCount = 0;

    public $pallet_number_locked = false;

    public $serial_number;

    protected static string $view = 'filament.resources.pallet-validation-resource.pages.create-pallet-validation';

    protected static string $resource = PalletValidationResource::class;

    protected $listeners = [
        'updateSnoQuantity' => 'handleUpdateSnoQuantity',
    ];

    public ?array $data = null;

    protected function getRedirectUrl(): string
    {
        return $this->getResource()::getUrl('create');
    }

    public function processPalletSNo()
    {
        $plantId = $this->form->getState()['plant_id'];

        $plantId = trim($plantId) ?? null;

        $pendingPallet = $this->form->getState()['pending_pallet_list'];

        $palletNumber = trim($this->form->getState()['pallet_number'])?? null;

        $palletNumber = trim($palletNumber) ?? null;

        $serialNumber = trim($this->form->getState()['serial_number'])?? null;

        $serialNumber = trim($serialNumber) ?? null;

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

        $operatorName = $user->name;

        $clickedAt = session('pallet_clicked_time');

        $clickedBy = session('pallet_created_by');

        if (!$palletNumber)
        {
            Notification::make()
                ->title("Pallet number can't be empty")
                ->danger()
                ->duration(5000)
                ->send();

            $this->form->fill([
                'serial_number' => null,
                'plant_id' => $plantId,
                'pallet_number' => $palletNumber,
                'pending_pallet_list' => $pendingPallet,
                'Sno_quantity' => 0,
                'created_by'    => $operatorName,
                'scanned_by'    => $operatorName,
            ]);
            return;
        }

        $count = PalletValidation::where('plant_id', $plantId)
        ->where('pallet_number', $palletNumber)
        ->count('pallet_number');
        if (!$serialNumber)
        {
            Notification::make()
                ->title('Serial number is required to add.')
                ->danger()
                ->duration(5000)
                ->send();

            $this->dispatch('loadData', $palletNumber, $plantId);
            $this->form->fill([
                'serial_number' => null,
                'plant_id' => $plantId,
                'pallet_number' => $palletNumber,
                'pending_pallet_list' => $pendingPallet,
                'Sno_quantity' => $count,
                'created_by'    => $operatorName,
                'scanned_by'    => $operatorName,
            ]);
            return;
        }
        else if (strlen($serialNumber) < 9 || strlen($serialNumber) > 20)
        {
            Notification::make()
                ->title('Serial number should contain minimum 9 digits and maximum 20 digits.')
                ->danger()
                ->duration(5000)
                ->send();

            $this->dispatch('loadData', $palletNumber, $plantId);
            $this->form->fill([
                'serial_number' => null,
                'plant_id' => $plantId,
                'pallet_number' => $palletNumber,
                'pending_pallet_list' => $pendingPallet,
                'Sno_quantity' => $count,
                'created_by'    => $operatorName,
                'scanned_by'    => $operatorName,
            ]);
            return;
        }
        else if (!ctype_alnum($serialNumber))
        {
            Notification::make()
                ->title('Serial number must contain alpha-numeric values only.')
                ->danger()
                ->duration(5000)
                ->send();

            $this->dispatch('loadData', $palletNumber, $plantId);
            $this->form->fill([
                'serial_number' => null,
                'plant_id' => $plantId,
                'pallet_number' => $palletNumber,
                'pending_pallet_list' => $pendingPallet,
                'Sno_quantity' => $count,
                'created_by'    => $operatorName,
                'scanned_by'    => $operatorName,
            ]);
            return;
        }

        $existInvoiceSno = LocatorInvoiceValidation::where('serial_number', $serialNumber)
        ->where('plant_id', $plantId)
        ->where('scanned_status', '=', 'Scanned')
        ->first();

        $invoiceNumber = $existInvoiceSno?->invoice_number;
        if ($existInvoiceSno)
        {
            Notification::make()
                ->title("Scanned serial number '{$serialNumber}' already completed the scanning process and exist in invoice number : {$invoiceNumber}.<br>Scan the new serial number to add!")
                ->danger()
                ->duration(5000)
                ->send();

            $this->dispatch('loadData', $palletNumber, $plantId);
            $this->form->fill([
                'serial_number' => null,
                'plant_id' => $plantId,
                'pallet_number' => $palletNumber,
                'pending_pallet_list' => $pendingPallet,
                'Sno_quantity' => $count,
                'created_by'    => $operatorName,
                'scanned_by'    => $operatorName,
            ]);
            return;
        }

        $existingRecord = PalletValidation::where('serial_number', $serialNumber)
        ->where('plant_id', $plantId)
        ->first();

        if ($existingRecord)
        {

            // if ($existingRecord && ($existingRecord->pallet_number == null || $existingRecord->pallet_number == '')) {
            //     Notification::make()
            //         ->title("Scanned serial number '{$serialNumber}' exists in pallet table .<br>scan the valid serial number to proceed...")
            //         ->danger()
            //         ->duration(5000)
            //         ->send();
            //         $this->form->fill([
            //             'serial_number' => null,
            //             'plant_id' => $this->plantId,
            //             'pallet_number' => $this->pallet_number,
            //             'pending_pallet_list' => $this->pendingPallet,
            //             'created_by'    => $operatorName,
            //             'scanned_by'    => $operatorName,
            //         ]);
            //         return;

            // }

            if ($existingRecord && $existingRecord->pallet_number == $palletNumber)
            {
                Notification::make()
                    ->title("Scanned serial number '{$serialNumber}' is already exists in pallet number '{$palletNumber}'.<br>Scan the new serial number to proceed...")
                    ->danger()
                    ->duration(5000)
                    ->send();
            }
            else if ($existingRecord && $existingRecord->pallet_number && $existingRecord->pallet_number != $palletNumber)
            {
                Notification::make()
                    ->title("Scanned serial number '{$serialNumber}' already exists in pallet number '{$existingRecord->pallet_number}'.<br>Scan the new serial number to proceed...")
                    ->danger()
                    ->duration(5000)
                    ->send();
            }
            else if ($existingRecord && $existingRecord->locator_number)
            {
                Notification::make()
                    ->title("Scanned serial number '{$serialNumber}' is already exists in locator number '{$existingRecord->locator_number}'.<br>Scan the new serial number to proceed...")
                    ->danger()
                    ->duration(5000)
                    ->send();
            }
            $this->dispatch('loadData', $palletNumber, $plantId);
            $this->form->fill([
                'serial_number' => null,
                'plant_id' => $plantId,
                'pallet_number' => $palletNumber,
                'pending_pallet_list' => $pendingPallet,
                'Sno_quantity' => $count,
                'created_by'    => $operatorName,
                'scanned_by'    => $operatorName,
            ]);
            return;
        }

        try
        {

            $existingPallet = PalletValidation::where('plant_id', $plantId)
            ->where('pallet_number', $palletNumber)
            ->first();

            $createdAt = $existingPallet ? $existingPallet->created_at : $clickedAt ?? now();

            $createdBy = $existingPallet ? $existingPallet->created_by : $clickedBy ?? $operatorName;

            $record = PalletValidation::create([
                'plant_id'       => $plantId,
                'pallet_number'  => $palletNumber,
                'serial_number'  => $serialNumber,
                'created_by'     => $createdBy,
                'scanned_by'     => $operatorName,
                'created_at'     => $createdAt,
                'scanned_at'     => now(),
                'updated_by'     => $operatorName,
            ]);

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

            if ($record)
            {
                // Notification::make()
                //     ->title("Scanned serial number '{$serialNumber}' successfully inserted into pallet table!<br>Scan the next new serial number to proceed...")
                //     ->success()
                //     ->duration(600)
                //     ->send();

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

                $this->dispatch('loadData', $palletNumber, $plantId);
                $this->form->fill([
                    'plant_id'      => $plantId,
                    'pallet_number' => $palletNumber,
                    'serial_number' => null,
                    'pending_pallet_list' => $pendingPallet,
                    'Sno_quantity' => $count,
                    'created_by'    => $operatorName,
                    'scanned_by'    => $operatorName,
                ]);
            }
            else
            {
                Notification::make()
                    ->title("Failed to insert scanned serial number '$serialNumber' into pallet table!")
                    ->danger()
                    ->duration(5000)
                    ->send();

                $this->dispatch('loadData', $palletNumber, $plantId);
                $this->form->fill([
                    'plant_id'      => $plantId,
                    'pallet_number' => $palletNumber,
                    'serial_number' => null,
                    'pending_pallet_list' => $pendingPallet,
                    'Sno_quantity' => $count,
                    'scanned_by'    => $operatorName,
                ]);
                return;
            }
        }
        catch (\Exception $e)
        {
            Notification::make()
                ->title('Error: Serial not inserted.')
                ->body("Something went wrong while inserting serial number '{$serialNumber}' into pallet table!\nScan the new serial number to proceed...")
                ->danger()
                ->duration(5000)
                ->send();

                $this->dispatch('loadData', $palletNumber, $plantId);
                $this->form->fill([
                    'plant_id'      => $plantId,
                    'pallet_number' => $palletNumber,
                    'serial_number' => null,
                    'pending_pallet_list' => $pendingPallet,
                    'Sno_quantity' => $count,
                    'created_by'    => $operatorName,
                    'scanned_by'    => $operatorName,
                ]);
                return;
        }
    }

    public function handleUpdateSnoQuantity($newValue)
    {
        // Update the form field
        $this->form->fill([
            'Sno_quantity' => $newValue,
        ]);
    }

    public function redirectToQrPdf($palletNo)
    {
       $this->palletNo = $this->form->getState()['pallet_number'];

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

    }

    public function markAsComplete()
    {

        $plantId = $this->form->getState()['plant_id'];

        $plantId = trim($plantId) ?? null;

        $pendingPallet = $this->form->getState()['pending_pallet_list'];

        $palletNumber = trim($this->form->getState()['pallet_number'])?? null;

        $palletNumber = trim($palletNumber) ?? null;

        $serialNumber = trim($this->form->getState()['serial_number'])?? null;

        $serialNumber = trim($serialNumber) ?? null;

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

        $operatorName = $user->name;

        $isCompleted = $this->data['is_completed'] ?? false;

        $this->pendingPallet = $this->form->getState()['pending_pallet_list'];


        $palletExist = PalletValidation::where('pallet_number', $palletNumber)
        ->where('plant_id', $plantId)
        ->first();

        if (!$palletExist)
        {
            Notification::make()
                ->title("Pallet number '$palletNumber' does not have serial numbers to save!<br>Add the valid serial number into pallet number to proceed...")
                ->danger()
                ->duration(5000)
                ->send();

            $this->dispatch('loadData', $palletNumber, $plantId);
            $this->form->fill([
                'serial_number' => null,
                'plant_id' => $plantId,
                'pallet_number' => $palletNumber,
                'pending_pallet_list' => $pendingPallet,
                'Sno_quantity' => 0,
                'created_by'    => $operatorName,
                'scanned_by'    => $operatorName,
            ]);
            return;
        }

        $allCompleted = PalletValidation::where('plant_id', $plantId)
        ->where('pallet_number', $palletNumber)
        ->where('pallet_status', '=','Completed')
        ->first();

        if ($allCompleted)
        {
            Notification::make()
                ->title("Pallet number '$palletNumber' already completed the master packing!<br>Generate the new pallet number or choose from pending pallet list!")
                ->danger()
                ->duration(5000)
                ->send();

            $this->dispatch('loadData', '', $plantId);
            $this->form->fill([
                'serial_number' => null,
                'plant_id' => $plantId,
                'pallet_number' => null,
                'pending_pallet_list' => null,//$pendingPallet
                'Sno_quantity' => 0,
                'created_by'    => $operatorName,
                'scanned_by'    => $operatorName,
            ]);
            return;
        }

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

        if (!$isCompleted)
        {
            $updated = PalletValidation::where('pallet_number', $palletNumber)
            ->where('plant_id', $plantId)
            ->update([
                'updated_at' => now(),
                'updated_by' => $operatorName,
            ]);

            if ($updated > 0)
            {
                Notification::make()
                    ->title("Pallet number '$palletNumber' records saved successfully!")
                    ->success()
                    ->duration(800)
                    ->send();

                $this->dispatch('loadData', '', $plantId);
                $this->form->fill([
                    'serial_number' => null,
                    'plant_id' => $plantId,
                    'pallet_number' => null,//$palletNumber
                    'pending_pallet_list' => null,//$pendingPallet
                    'Sno_quantity' => 0,//$count,
                    'created_by'    => $operatorName,
                    'scanned_by'    => $operatorName,
                ]);
                return;
            }
        }
        else
        {
            $updated = PalletValidation::where('pallet_number', $palletNumber)
            ->where('plant_id', $plantId)
            ->update([
                'pallet_status' => 'Completed',
                'updated_at' => now(),
                'updated_by' => $operatorName,
            ]);

            if ($updated > 0)
            {
                Notification::make()
                    ->title("Pallet number '$palletNumber' completed the master packing successfully!")
                    ->success()
                    ->duration(800)
                    ->send();

                $this->dispatch('loadData', '', $plantId);
                $this->form->fill([
                    'serial_number' => null,
                    'plant_id' => $plantId,
                    'pallet_number' => null,//$palletNumber
                    'pending_pallet_list' => null,//$pendingPallet
                    'Sno_quantity' => 0,//$count
                    'created_by'    => $operatorName,
                    'scanned_by'    => $operatorName,
                ]);
                return;
            }
        }
    }

    public function processRemoveSNo()
    {
        $plantId = $this->form->getState()['plant_id'];

        $plantId = trim($plantId) ?? null;

        $pendingPallet = $this->form->getState()['pending_pallet_list'];

        $palletNumber = trim($this->form->getState()['pallet_number']) ?? null;

        $palletNumber = trim($palletNumber) ?? null;

        $serialNumber = trim($this->form->getState()['removeSno_number']) ?? null;

        $serialNumber = trim($serialNumber) ?? null;

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

        $operatorName = $user->name;

        if (!$palletNumber)
        {
            Notification::make()
                ->title('Pallet number is required to remove.')
                ->danger()
                ->duration(5000)
                ->send();
            return;
        }


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

        if (!$serialNumber)
        {
            Notification::make()
                ->title('Serial number is required to remove.')
                ->danger()
                ->duration(5000)
                ->send();

            $this->dispatch('loadData', $palletNumber, $plantId);
            $this->form->fill([
                'serial_number' => null,
                'plant_id' => $plantId,
                'pallet_number' => $palletNumber,
                'pending_pallet_list' => $pendingPallet,
                'Sno_quantity' => $count,
                'created_by'    => $operatorName,
                'scanned_by'    => $operatorName,
            ]);
            return;
        }
        else if (strlen($serialNumber) < 9 || strlen($serialNumber) > 20)
        {
            Notification::make()
                ->title('Serial number should contain minimum 9 digits and maximum 20 digits.')
                ->danger()
                ->duration(5000)
                ->send();

            $this->dispatch('loadData', $palletNumber, $plantId);
            $this->form->fill([
                'serial_number' => null,
                'plant_id' => $plantId,
                'pallet_number' => $palletNumber,
                'pending_pallet_list' => $pendingPallet,
                'Sno_quantity' => $count,
                'created_by'    => $operatorName,
                'scanned_by'    => $operatorName,
            ]);
            return;
        }
        else if (!ctype_alnum($serialNumber))
        {
            Notification::make()
                ->title('Serial number must contain alpha-numeric values only.')
                ->danger()
                ->duration(5000)
                ->send();

            $this->dispatch('loadData', $palletNumber, $plantId);
            $this->form->fill([
                'serial_number' => null,
                'plant_id' => $plantId,
                'pallet_number' => $palletNumber,
                'pending_pallet_list' => $pendingPallet,
                'Sno_quantity' => $count,
                'created_by'    => $operatorName,
                'scanned_by'    => $operatorName,
            ]);
            return;
        }

        $invoiceExist = LocatorInvoiceValidation::where('plant_id', $plantId)
        ->where('serial_number', $serialNumber)
        ->where('scanned_status', '=', 'Scanned')
        ->first();

        if ($invoiceExist)
        {
            Notification::make()
                ->title("Scanned serial number '{$serialNumber}' already completed the scanning process and exist in invoice number '{$invoiceExist->invoice_number}'.<br>Scan the valid exist serial number to remove!")
                ->danger()
                ->duration(5000)
                ->send();

            $this->dispatch('loadData', $palletNumber, $plantId);
            $this->form->fill([
                'serial_number' => null,
                'plant_id' => $plantId,
                'pallet_number' => $palletNumber,
                'pending_pallet_list' => $pendingPallet,
                'Sno_quantity' => $count,
                'created_by'    => $operatorName,
                'scanned_by'    => $operatorName,
            ]);
            return;
        }

        $serialexist = PalletValidation::where('plant_id', $plantId)
        ->where('serial_number', $serialNumber)
        ->first();
        if (!$serialexist)
        {
            Notification::make()
                ->title('Serial number not exists in pallet table.')
                ->danger()
                ->duration(5000)
                ->send();

            $this->dispatch('loadData', $palletNumber, $plantId);
            $this->form->fill([
                'serial_number' => null,
                'plant_id' => $plantId,
                'pallet_number' => $palletNumber,
                'pending_pallet_list' => $pendingPallet,
                'Sno_quantity' => $count,
                'created_by'    => $operatorName,
                'scanned_by'    => $operatorName,
            ]);
            return;
        }

        $palletExist = PalletValidation::where('plant_id', $plantId)
        ->where('serial_number', $serialNumber)
        ->where('pallet_number', '!=', '')
        ->where('pallet_number', '!=', null)
        ->first();

        if ($palletExist && $palletExist->pallet_number != $palletNumber)
        {
            Notification::make()
                ->title("Scanned serial number exist in pallet number '$palletExist->pallet_number'.<br>Scan the valid exist serial number to remove!")
                ->danger()
                ->duration(5000)
                ->send();

            $this->dispatch('loadData', $palletNumber, $plantId);
            $this->form->fill([
                'serial_number' => null,
                'plant_id' => $plantId,
                'pallet_number' => $palletNumber,
                'pending_pallet_list' => $pendingPallet,
                'Sno_quantity' => $count,
                'created_by'    => $operatorName,
                'scanned_by'    => $operatorName,
            ]);
            return;
        }

        $locatorExist = PalletValidation::where('plant_id', $plantId)
        ->where('serial_number', $serialNumber)
        ->where('locator_number', '!=', '')
        ->where('locator_number', '!=', null)
        ->first();

        if ($locatorExist && $locatorExist->locator_number)
        {
            Notification::make()
                ->title("Scanned serial number exist in locator number '$locatorExist->locator_number'.<br>Scan the valid exist serial number to remove!")
                ->danger()
                ->duration(5000)
                ->send();

            $this->dispatch('loadData', $palletNumber, $plantId);
            $this->form->fill([
                'serial_number' => null,
                'plant_id' => $plantId,
                'pallet_number' => $palletNumber,
                'pending_pallet_list' => $pendingPallet,
                'Sno_quantity' => $count,
                'created_by'    => $operatorName,
                'scanned_by'    => $operatorName,
            ]);
            return;
        }


        $deleted = PalletValidation::where('plant_id', $plantId)
            ->where('pallet_number', $palletNumber)
            ->where('serial_number', $serialNumber)
            ->forceDelete();

            if ($deleted)
            {
                // Notification::make()
                //     ->title("Scanned serial number '$serialNumber' successfully removed from pallet table!<br>Scan the next exist serial number to remove...")
                //     ->success()
                //     ->duration(600)
                //     ->send();

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

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

                $this->dispatch('loadData', $palletNumber, $plantId);
            }
            else
            {
                Notification::make()
                    ->title("Failed to remove scanned serial number '$serialNumber' from pallet table!")
                    ->danger()
                    ->duration(5000)
                    ->send();

                $this->dispatch('loadData', $palletNumber, $plantId);
                $this->form->fill([
                    'serial_number' => null,
                    'plant_id' => $plantId,
                    'pallet_number' => $palletNumber,
                    'pending_pallet_list' => $pendingPallet,
                    'Sno_quantity' => $count,
                    'created_by'    => $operatorName,
                    'scanned_by'    => $operatorName,
                ]);
            }

        //$this->dispatch('removeSno', $serialNumber, $palletNumber, $plantId);
    }

    public function processPalletNo()
    {
        $plantId = $this->form->getState()['plant_id'];

        $plantId = trim($plantId) ?? null;

        $pendingPallet = $this->form->getState()['pending_pallet_list'];

        $palletNumber = trim($this->form->getState()['pallet_number']) ?? null;

        $palletNumber = trim($palletNumber) ?? null;

        $serialNumber = trim($this->form->getState()['serial_number']) ?? null;

        $serialNumber = trim($serialNumber) ?? null;

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

        $operatorName = $user->name;

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

        if (!$palletNumber)
        {
            Notification::make()
                ->title('Pallet number is required.')
                ->danger()
                ->duration(5000)
                ->send();

            $this->dispatch('loadData', '', $plantId);
            $this->form->fill([
                'serial_number' => null,
                'plant_id' => $plantId,
                'pallet_number' => $palletNumber,
                'pending_pallet_list' => $pendingPallet,
                'Sno_quantity' => 0,
                'created_by'    => $operatorName,
                'scanned_by'    => $operatorName,
            ]);
            return;
        }

        if (strlen($palletNumber) < 10)
        {
            Notification::make()
                ->title("Pallet number '$palletNumber' must be at least 10 digits.")
                ->danger()
                ->duration(5000)
                ->send();

            $this->dispatch('loadLocator' ,'',$plantId);
            $this->form->fill([
                'serial_number' => null,
                'plant_id' => $plantId,
                'pallet_number' => $palletNumber,
                'pending_pallet_list' => $pendingPallet,
                'Sno_quantity' => 0,
                'created_by'    => $operatorName,
                'scanned_by'    => $operatorName,
            ]);
            return;
        }

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


        $palletNotCompleted = PalletValidation::where('plant_id', $plantId)
        ->where('pallet_number', $palletNumber)
        ->where('pallet_status', '=','')
        ->orWhere('pallet_status', '=',null)
        ->first();

        if (!$palletNotCompleted)
        {
            Notification::make()
                ->title("Already completed for pallet number $palletNumber!")
                ->danger()
                ->duration(5000)
                ->send();

            $this->dispatch('loadData', $palletNumber, $plantId);
            $this->form->fill([
                'serial_number' => null,
                'plant_id' => $plantId,
                'pallet_number' => $palletNumber,
                'pending_pallet_list' => $pendingPallet,
                'Sno_quantity' => $count,
                'created_by'    => $operatorName,
                'scanned_by'    => $operatorName,
            ]);
            return;
        }

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

        $this->dispatch('loadData', $palletNumber, $plantId);

    }

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

    public function getHeader(): ?View
    {
        return null;
    }

    public function getHeading(): string
    {
        return '';
    }

    public function getSubheading(): ?string
    {
        return null;
    }
}
