<?php

namespace App\Filament\Resources\ReworkLocatorInvoiceValidationResource\Pages;

use App\Filament\Resources\ReworkLocatorInvoiceValidationResource;
use App\Models\LocatorInvoiceValidation;
use App\Models\PalletValidation;
use App\Models\Plant;
use App\Models\ReworkLocatorInvoiceValidation;
use Filament\Actions;
use Filament\Facades\Filament;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\CreateRecord;

class CreateReworkLocatorInvoiceValidation extends CreateRecord
{

    public $pId, $modalMessage, $modalTitle, $invoiceNo, $palletNo, $serialNo;

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

    protected static string $resource = ReworkLocatorInvoiceValidationResource::class;

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

    public function processInvoiceRework($invoiceNo)
    {
        $plantId = $this->form->getState()['plant'];

        $this->pId = $plantId;

        $invoiceNo = trim($this->form->getState()['invoice_number']);
        $this->invoiceNo = $invoiceNo;

        $palletNo = trim($this->form->getState()['scan_pallet_no']);
        $this->palletNo = $palletNo;

        $serialNo = trim($this->form->getState()['scan_serial_no']);
        $this->serialNo = $serialNo;

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

        $reworkType = $this->form->getState()['reworkType'] ?? null;

        $updateInvoiceStatus = $this->form->getState()['update_invoice'] ?? null;

        // $updatePalletStatus = $this->form->getState()['update_pallet'] ?? null;

        $InvoiceSerialNumbers = [];

        if ($reworkType == 'invoice')
        {
            if ($invoiceNo == '' || $invoiceNo == null)
            {
                Notification::make()
                    ->title("Invalid: Invoice Number")
                    ->body("Invoice number can't be empty!")
                    ->danger()
                    ->duration(5000)
                    ->send();

                $this->dispatch('loadData', '', '', $plantId, $reworkType);
                // $snoCount = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)->count();
                $this->form->fill([
                    'plant_id'       => $plantId,
                    'plant'          => $plantId,
                    'invoice_number' => null,
                    'scan_pallet_no' => null,
                    'scan_serial_no' => null,
                    'rework_type'    => $reworkType,
                    'reworkType'     => $reworkType,
                    'update_invoice' => 0,
                    'update_pallet'  => 0,
                    'created_by'     => $operatorName,
                    'scanned_by'     => $operatorName,
                ]);
                return;
            }

            $InvoiceSerialNumbers = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)->first();

            if (!$InvoiceSerialNumbers)
            {
                Notification::make()
                    ->title('Invoice Not Found')
                    ->body("Invoice number '$invoiceNo' doesn't exist in invoice table!<br><br>Scan the valid exist 'Invoice Number' to proceed..!")
                    ->danger()
                    ->duration(5000)
                    ->send();

                $this->dispatch('loadData', '', '', $plantId, $reworkType);
                $this->form->fill([
                    'plant_id'       => $plantId,
                    'plant'          => $plantId,
                    'invoice_number' => null,
                    'scan_pallet_no' => null,
                    'scan_serial_no' => null,
                    'rework_type'    => $reworkType,
                    'reworkType'     => $reworkType,
                    'update_invoice' => 0,
                    'update_pallet'  => 0,
                    'created_by'     => $operatorName,
                    'scanned_by'     => $operatorName,
                ]);
                return;
            }

            $notCompletedCount = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)
                ->where(function($query) {
                    $query->whereNull('scanned_status')
                        ->orWhere('scanned_status', '');
                })
                ->count();

            $records = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)->get();

            $allScanned = true;
            foreach ($records as $record)
            {
                if (($record->scanned_status == null) || trim($record->scanned_status) == '') {
                    $allScanned = false;
                    break;
                }
            }

            if (count($records) > 0 && $allScanned)
            {
                if (strlen($palletNo) > 0 && strlen($palletNo) < 10)
                {
                    Notification::make()
                        ->title("Invalid: Pallet Number")
                        ->body("Pallet number '$palletNo' must be at least 10 digits.")
                        ->danger()
                        ->duration(5000)
                        ->send();

                    $this->dispatch('loadData', $invoiceNo, '', $plantId, $reworkType);
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => $invoiceNo,
                        'scan_pallet_no' => null,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                    return;
                }
                else if (strlen($serialNo) > 0 && (strlen($serialNo) < 9 || strlen($serialNo) > 20))
                {
                    Notification::make()
                        ->title("Invalid: Serial Number")
                        ->body("Serial number should contain minimum 9 digits and maximum 20 digits.")
                        ->danger()
                        ->duration(5000)
                        ->send();

                    $this->dispatch('loadData', $invoiceNo, '', $plantId, $reworkType);
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => $invoiceNo,
                        'scan_pallet_no' => null,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                    return;
                }
                else if (strlen($serialNo) > 0 && !ctype_alnum($serialNo))
                {
                    Notification::make()
                        ->title("Invalid: Serial Number")
                        ->body("Serial number '$serialNo' must contain alpha-numeric values only.")
                        ->danger()
                        ->duration(5000)
                        ->send();

                    $this->dispatch('loadData', $invoiceNo, '', $plantId, $reworkType);
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => $invoiceNo,
                        'scan_pallet_no' => null,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                    return;
                }

                if ($updateInvoiceStatus == 1)
                {
                    foreach ($records as $row)
                    {
                        ReworkLocatorInvoiceValidation::create([
                            'plant_id' => $row->plant_id,
                            'invoice_number' => $row->invoice_number,
                            'serial_number' => $row->serial_number,
                            'pallet_number' => $row->pallet_number,
                            'locator_number' => $row->locator_number,
                            'scanned_status' => $row->scanned_status,
                            'upload_status' => $row->upload_status,
                            'created_by' => $row->created_by,
                            'scanned_by' => $row->scanned_by,
                            'updated_by' => $row->updated_by,
                            'reworked_by' => $operatorName,
                            'created_at' => $row->created_at,
                            'scanned_at' => $row->scanned_at,
                            'updated_at' => $row->updated_at,
                            'reworked_at' => now(),
                        ]);

                        $row->forceDelete(); // Delete the row from the original table
                    }

                    Notification::make()
                        ->title('Completed: Rework Invoice')
                        ->body("Scanned invoice number '$invoiceNo' successfully moved into rework invoice table.<br><br>Please, scan the next completed invoice number to proceed..!")
                        ->success()
                        ->duration(800)
                        ->send();

                    $this->dispatch('loadData', '', '', $plantId, $reworkType);
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => null,
                        'scan_pallet_no' => null,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                }
                else
                {
                    Notification::make()
                        ->title('Completed: Invoice Found')
                        ->body("Press 'Yes' and Press 'Enter' in Scan Invoice No to do rework for entire invoice.<br><br>Press 'No' to do rework against 'Pallet or Serial Number'..!")
                        ->info()
                        ->duration(5000)
                        ->send();

                    $this->dispatch('loadData', $invoiceNo, '', $plantId, $reworkType);
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => $invoiceNo,
                        'scan_pallet_no' => null,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => null,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                }
                return;
            }
            else
            {
                Notification::make()
                    ->title('Invoice Not Completed')
                    ->body("Scanned invoice number '$invoiceNo' doesn't completed the scanning process!<br><br>Has '$notCompletedCount' pending serial number to scan!<br><br>Please, scan the valid completed invoice number to proceed..!")
                    ->danger()
                    ->duration(5000)
                    ->send();

                $this->dispatch('loadData', '', '', $plantId, $reworkType);
                $this->form->fill([
                    'plant_id'       => $plantId,
                    'plant'          => $plantId,
                    'invoice_number' => null,
                    'scan_pallet_no' => null,
                    'scan_serial_no' => null,
                    'rework_type'    => $reworkType,
                    'reworkType'     => $reworkType,
                    'update_invoice' => 0,
                    'update_pallet'  => 0,
                    'created_by'     => $operatorName,
                    'scanned_by'     => $operatorName,
                ]);
                return;
            }
        }
        else if ($reworkType == 'pallet')
        {
            Notification::make()
                ->title("Choosed: Pallet Rework Type")
                ->body("Scan valid pallet number to proceed..!")
                ->danger()
                ->duration(5000)
                ->send();
            return;
        }
        else
        {
            Notification::make()
                ->title("Rework Type Not Found")
                ->danger()
                ->duration(5000)
                ->send();

            $this->dispatch('loadData', '', '', $plantId, $reworkType);
            $this->form->fill([
                'plant_id'       => $plantId,
                'plant'          => $plantId,
                'invoice_number' => null,
                'scan_pallet_no' => null,
                'scan_serial_no' => null,
                'rework_type'    => null,
                'reworkType'     => null,
                'update_invoice' => 0,
                'update_pallet'  => 0,
                'created_by'     => $operatorName,
                'scanned_by'     => $operatorName,
            ]);
            return;
        }
    }

    public function processPalletno($palletNo)
    {
        $plantId = $this->form->getState()['plant'];

        $this->pId = $plantId;

        $invoiceNo = trim($this->form->getState()['invoice_number']);
        $this->invoiceNo = $invoiceNo;

        $palletNo = trim($this->form->getState()['scan_pallet_no']);
        $this->palletNo = $palletNo;

        $serialNo = trim($this->form->getState()['scan_serial_no']);
        $this->serialNo = $serialNo;

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

        $reworkType = $this->form->getState()['reworkType'] ?? null;

        // $updateInvoiceStatus = $this->form->getState()['update_invoice'] ?? null;

        $updatePalletStatus = $this->form->getState()['update_pallet'] ?? null;

        $InvoiceSerialNumbers = [];

        $PalletSerialNumbers = [];

        if ($reworkType == 'invoice')
        {
            if ($invoiceNo == '' || $invoiceNo == null)
            {
                Notification::make()
                    ->title("Invalid: Invoice Number")
                    ->body("Invoice number can't be empty!")
                    ->danger()
                    ->duration(5000)
                    ->send();

                $this->dispatch('loadData', '', '', $plantId, $reworkType);
                // $snoCount = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)->count();
                $this->form->fill([
                    'plant_id'       => $plantId,
                    'plant'          => $plantId,
                    'invoice_number' => null,
                    'scan_pallet_no' => null,
                    'scan_serial_no' => null,
                    'rework_type'    => $reworkType,
                    'reworkType'     => $reworkType,
                    'update_invoice' => 0,
                    'update_pallet'  => 0,
                    'created_by'     => $operatorName,
                    'scanned_by'     => $operatorName,
                ]);
                return;
            }

            $InvoiceSerialNumbers = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)->first();

            if (!$InvoiceSerialNumbers)
            {
                Notification::make()
                    ->title('Invoice Not Found')
                    ->body("Invoice number '$invoiceNo' doesn't exist in invoice table!<br><br>Scan the valid exist 'Invoice Number' to proceed..!")
                    ->danger()
                    ->duration(5000)
                    ->send();

                $this->dispatch('loadData', '', '', $plantId, $reworkType);
                $this->form->fill([
                    'plant_id'       => $plantId,
                    'plant'          => $plantId,
                    'invoice_number' => null,
                    'scan_pallet_no' => null,
                    'scan_serial_no' => null,
                    'rework_type'    => $reworkType,
                    'reworkType'     => $reworkType,
                    'update_invoice' => 0,
                    'update_pallet'  => 0,
                    'created_by'     => $operatorName,
                    'scanned_by'     => $operatorName,
                ]);
                return;
            }

            $notCompletedCount = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)
                ->where(function($query) {
                    $query->whereNull('scanned_status')
                        ->orWhere('scanned_status', '');
                })
                ->count();

            $records = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)->get();

            $allScanned = true;
            foreach ($records as $record)
            {
                if (($record->scanned_status == null) || trim($record->scanned_status) == '') {
                    $allScanned = false;
                    break;
                }
            }

            if (count($records) > 0 && $allScanned)
            {
                if ($palletNo == '' || $palletNo == null)
                {
                    Notification::make()
                        ->title("Invalid: Pallet Number")
                        ->body("Pallet number can't be empty!")
                        ->danger()
                        ->duration(5000)
                        ->send();

                    $this->dispatch('loadData', $invoiceNo, '', $plantId, $reworkType);
                    // $snoCount = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)->count();
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => $invoiceNo,
                        'scan_pallet_no' => null,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                    return;
                }
                else if (strlen($palletNo) < 10)
                {
                    Notification::make()
                        ->title("Invalid: Pallet Number")
                        ->body("Pallet number '$palletNo' must be at least 10 digits.")
                        ->danger()
                        ->duration(5000)
                        ->send();

                    $this->dispatch('loadData', $invoiceNo, '', $plantId, $reworkType);
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => $invoiceNo,
                        'scan_pallet_no' => null,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                    return;
                }

                $InvoiceSerialNumbers = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)->where('pallet_number', $palletNo)->first();

                if (!$InvoiceSerialNumbers)
                {
                    Notification::make()
                        ->title('Pallet Not Found')
                        ->body("Pallet number '$palletNo' doesn't exist in scanned invoice number '$invoiceNo'!<br><br>Scan the valid exist 'Pallet Number' to proceed..!")
                        ->danger()
                        ->duration(5000)
                        ->send();

                    $this->dispatch('loadData', $invoiceNo, '', $plantId, $reworkType);
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => $invoiceNo,
                        'scan_pallet_no' => null,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                    return;
                }

                if (strlen($serialNo) > 0 && (strlen($serialNo) < 9 || strlen($serialNo) > 20))
                {
                    Notification::make()
                        ->title("Invalid: Serial Number")
                        ->body("Serial number should contain minimum 9 digits and maximum 20 digits.")
                        ->danger()
                        ->duration(5000)
                        ->send();

                    $this->dispatch('loadData', $invoiceNo, $palletNo, $plantId, $reworkType);
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => $invoiceNo,
                        'scan_pallet_no' => $palletNo,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                    return;
                }
                else if (strlen($serialNo) > 0 && !ctype_alnum($serialNo))
                {
                    Notification::make()
                        ->title("Invalid: Serial Number")
                        ->body("Serial number '$serialNo' must contain alpha-numeric values only.")
                        ->danger()
                        ->duration(5000)
                        ->send();

                    $this->dispatch('loadData', $invoiceNo, $palletNo, $plantId, $reworkType);
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => $invoiceNo,
                        'scan_pallet_no' => $palletNo,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                    return;
                }

                foreach ($records as $row)
                {
                    if ($row->pallet_number != null && $row->pallet_number != '' && $row->pallet_number == $palletNo)
                    {
                        ReworkLocatorInvoiceValidation::create([
                            'plant_id' => $row->plant_id,
                            'invoice_number' => $row->invoice_number,
                            'serial_number' => $row->serial_number,
                            'pallet_number' => $row->pallet_number,
                            'locator_number' => $row->locator_number,
                            'scanned_status' => $row->scanned_status,
                            'upload_status' => $row->upload_status,
                            'created_by' => $row->created_by,
                            'scanned_by' => $row->scanned_by,
                            'updated_by' => $row->updated_by,
                            'reworked_by' => $operatorName,
                            'created_at' => $row->created_at,
                            'scanned_at' => $row->scanned_at,
                            'updated_at' => $row->updated_at,
                            'reworked_at' => now(),
                        ]);

                        $row->forceDelete(); // Delete the row from the original table
                    }
                }

                Notification::make()
                    ->title('Completed: Rework Pallet from Invoice')
                    ->body("Scanned pallet number '$palletNo' successfully moved into rework invoice table from invoice number '$invoiceNo'.<br><br>Please, scan the next pallet number to proceed..!")
                    ->success()
                    ->duration(500)
                    ->send();

                $InvoiceSerialNumbers = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)->first();

                if (!$InvoiceSerialNumbers)
                {
                    Notification::make()
                        ->title("All available serial numbers are successfully moved into rework invoice table from invoice number '$invoiceNo'.<br><br>Please, scan the next invoice number to proceed..!")
                        ->success()
                        ->duration(800)
                        ->send();

                    $this->dispatch('loadData', '', '', $plantId, $reworkType);
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => null,
                        'scan_pallet_no' => null,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                }
                else
                {
                    $this->dispatch('loadData', $invoiceNo, '', $plantId, $reworkType);
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => $invoiceNo,
                        'scan_pallet_no' => null,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                }
            }
            else
            {
                Notification::make()
                    ->title('Invoice Not Completed')
                    ->body("Scanned invoice number '$invoiceNo' doesn't completed the scanning process!<br><br>Has '$notCompletedCount' pending serial number to scan!<br><br>Please, scan the valid completed invoice number to proceed..!")
                    ->danger()
                    ->duration(5000)
                    ->send();

                $this->dispatch('loadData', '', '', $plantId, $reworkType);
                $this->form->fill([
                    'plant_id'       => $plantId,
                    'plant'          => $plantId,
                    'invoice_number' => null,
                    'scan_pallet_no' => null,
                    'scan_serial_no' => null,
                    'rework_type'    => $reworkType,
                    'reworkType'     => $reworkType,
                    'update_invoice' => 0,
                    'update_pallet'  => 0,
                    'created_by'     => $operatorName,
                    'scanned_by'     => $operatorName,
                ]);
                return;
            }
        }
        else if ($reworkType == 'pallet')
        {
            if ($palletNo == '' || $palletNo == null)
            {
                Notification::make()
                    ->title("Invalid: Pallet Number")
                    ->body("Pallet number can't be empty!")
                    ->danger()
                    ->duration(5000)
                    ->send();

                $this->dispatch('loadData', '', '', $plantId, $reworkType);
                // $snoCount = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)->count();
                $this->form->fill([
                    'plant_id'       => $plantId,
                    'plant'          => $plantId,
                    'invoice_number' => null,
                    'scan_pallet_no' => null,
                    'scan_serial_no' => null,
                    'rework_type'    => $reworkType,
                    'reworkType'     => $reworkType,
                    'update_invoice' => 0,
                    'update_pallet'  => 0,
                    'created_by'     => $operatorName,
                    'scanned_by'     => $operatorName,
                ]);
                return;
            }
            else if (strlen($palletNo) < 10)
            {
                Notification::make()
                    ->title("Invalid: Pallet Number")
                    ->body("Pallet number '$palletNo' must be at least 10 digits.")
                    ->danger()
                    ->duration(5000)
                    ->send();

                $this->dispatch('loadData', '', '', $plantId, $reworkType);
                $this->form->fill([
                    'plant_id'       => $plantId,
                    'plant'          => $plantId,
                    'invoice_number' => null,
                    'scan_pallet_no' => null,
                    'scan_serial_no' => null,
                    'rework_type'    => $reworkType,
                    'reworkType'     => $reworkType,
                    'update_invoice' => 0,
                    'update_pallet'  => 0,
                    'created_by'     => $operatorName,
                    'scanned_by'     => $operatorName,
                ]);
                return;
            }

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

            if (!$PalletSerialNumbers)
            {
                Notification::make()
                    ->title('Pallet Not Found')
                    ->body("Pallet number '$palletNo' doesn't exist in pallet table!<br><br>Scan the valid exist 'Pallet Number' to proceed..!")
                    ->danger()
                    ->duration(5000)
                    ->send();

                $this->dispatch('loadData', '', '', $plantId, $reworkType);
                $this->form->fill([
                    'plant_id'       => $plantId,
                    'plant'          => $plantId,
                    'invoice_number' => null,
                    'scan_pallet_no' => null,
                    'scan_serial_no' => null,
                    'rework_type'    => $reworkType,
                    'reworkType'     => $reworkType,
                    'update_invoice' => 0,
                    'update_pallet'  => 0,
                    'created_by'     => $operatorName,
                    'scanned_by'     => $operatorName,
                ]);
                return;
            }

            $records = PalletValidation::where('plant_id', $plantId)->where('pallet_number', $palletNo)->get();

            $allScanned = true;
            foreach ($records as $record)
            {
                if (($record->pallet_status == null) || trim($record->pallet_status) == '') {
                    $allScanned = false;
                    break;
                }
            }

            if (count($records) > 0 && $allScanned)
            {
                $locatExist = ($PalletSerialNumbers->locator_number != null && $PalletSerialNumbers->locator_number != '') ? $PalletSerialNumbers->locator_number : '';
                if ($locatExist)
                {
                    Notification::make()
                        ->title('Locator Pallet Found')
                        ->body("Scanned pallet number '$palletNo' exist in locator number '$locatExist'.<br><br>Remove scanned 'Pallet' from 'Locator' to proceed re-master packing..!")
                        ->danger()
                        ->duration(5000)
                        ->send();

                    $this->dispatch('loadData', '', '', $plantId, $reworkType);
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => null,
                        'scan_pallet_no' => null,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                    return;
                }
                else
                {
                    if (strlen($serialNo) > 0 && (strlen($serialNo) < 9 || strlen($serialNo) > 20))
                    {
                        Notification::make()
                            ->title("Invalid: Serial Number")
                            ->body("Serial number should contain minimum 9 digits and maximum 20 digits.")
                            ->danger()
                            ->duration(5000)
                            ->send();

                        $this->dispatch('loadData', '', $palletNo, $plantId, $reworkType);
                        $this->form->fill([
                            'plant_id'       => $plantId,
                            'plant'          => $plantId,
                            'invoice_number' => null,
                            'scan_pallet_no' => $palletNo,
                            'scan_serial_no' => null,
                            'rework_type'    => $reworkType,
                            'reworkType'     => $reworkType,
                            'update_invoice' => 0,
                            'update_pallet'  => 0,
                            'created_by'     => $operatorName,
                            'scanned_by'     => $operatorName,
                        ]);
                        return;
                    }
                    else if (strlen($serialNo) > 0 && !ctype_alnum($serialNo))
                    {
                        Notification::make()
                            ->title("Invalid: Serial Number")
                            ->body("Serial number '$serialNo' must contain alpha-numeric values only.")
                            ->danger()
                            ->duration(5000)
                            ->send();

                        $this->dispatch('loadData', '', $palletNo, $plantId, $reworkType);
                        $this->form->fill([
                            'plant_id'       => $plantId,
                            'plant'          => $plantId,
                            'invoice_number' => null,
                            'scan_pallet_no' => $palletNo,
                            'scan_serial_no' => null,
                            'rework_type'    => $reworkType,
                            'reworkType'     => $reworkType,
                            'update_invoice' => 0,
                            'update_pallet'  => 0,
                            'created_by'     => $operatorName,
                            'scanned_by'     => $operatorName,
                        ]);
                        return;
                    }

                    if ($updatePalletStatus == 1)
                    {
                        foreach ($records as $row)
                        {
                            $row->forceDelete(); // Delete the row from the original table
                        }

                        Notification::make()
                            ->title('Completed: Rework Pallet')
                            ->body("Scanned pallet number '$palletNo' successfully removed from pallet table.<br><br>Please, scan the next pallet number to re-master packing..!")
                            ->success()
                            ->duration(800)
                            ->send();

                        $this->dispatch('loadData', '', '', $plantId, $reworkType);
                        $this->form->fill([
                            'plant_id'       => $plantId,
                            'plant'          => $plantId,
                            'invoice_number' => null,
                            'scan_pallet_no' => null,
                            'scan_serial_no' => null,
                            'rework_type'    => $reworkType,
                            'reworkType'     => $reworkType,
                            'update_invoice' => 0,
                            'update_pallet'  => 0,
                            'created_by'     => $operatorName,
                            'scanned_by'     => $operatorName,
                        ]);
                        return;
                    }
                    else
                    {
                        Notification::make()
                            ->title('Completed: Pallet Found')
                            ->body("Press 'Yes' and Press 'Enter' in Scan Pallet No to do re-master packing for entire pallet.<br><br>Press 'No' to do partial re-master packing against 'Serial Number'..!")
                            ->info()
                            ->duration(5000)
                            ->send();
                        $this->dispatch('loadData', '', $palletNo, $plantId, $reworkType);
                        $this->form->fill([
                            'plant_id'       => $plantId,
                            'plant'          => $plantId,
                            'invoice_number' => null,
                            'scan_pallet_no' => $palletNo,
                            'scan_serial_no' => null,
                            'rework_type'    => $reworkType,
                            'reworkType'     => $reworkType,
                            'update_invoice' => 0,
                            'update_pallet'  => null,
                            'created_by'     => $operatorName,
                            'scanned_by'     => $operatorName,
                        ]);
                        return;
                    }
                }
            }
            else
            {
                Notification::make()
                    ->title('Pallet Not Completed')
                    ->body("Scanned pallet number '$palletNo' doesn't completed the master packing!<br><br>Please, scan the valid completed pallet number to proceed..!")
                    ->danger()
                    ->duration(5000)
                    ->send();

                $this->dispatch('loadData', '', '', $plantId, $reworkType);
                $this->form->fill([
                    'plant_id'       => $plantId,
                    'plant'          => $plantId,
                    'invoice_number' => null,
                    'scan_pallet_no' => null,
                    'scan_serial_no' => null,
                    'rework_type'    => $reworkType,
                    'reworkType'     => $reworkType,
                    'update_invoice' => 0,
                    'update_pallet'  => 0,
                    'created_by'     => $operatorName,
                    'scanned_by'     => $operatorName,
                ]);
                return;
            }
        }
        else
        {
            Notification::make()
                ->title("Rework Type Not Found")
                ->danger()
                ->duration(5000)
                ->send();

            $this->dispatch('loadData', '', '', $plantId, $reworkType);
            $this->form->fill([
                'plant_id'       => $plantId,
                'plant'          => $plantId,
                'invoice_number' => null,
                'scan_pallet_no' => null,
                'scan_serial_no' => null,
                'rework_type'    => null,
                'reworkType'     => null,
                'update_invoice' => 0,
                'update_pallet'  => 0,
                'created_by'     => $operatorName,
                'scanned_by'     => $operatorName,
            ]);
            return;
        }
    }

    public function processSno($serialNo)
    {
        $plantId = $this->form->getState()['plant'];

        $this->pId = $plantId;

        $invoiceNo = trim($this->form->getState()['invoice_number']);
        $this->invoiceNo = $invoiceNo;

        $palletNo = trim($this->form->getState()['scan_pallet_no']);
        $this->palletNo = $palletNo;

        $serialNo = trim($this->form->getState()['scan_serial_no']);
        $this->serialNo = $serialNo;

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

        $reworkType = $this->form->getState()['reworkType'] ?? null;

        // $updateInvoiceStatus = $this->form->getState()['update_invoice'] ?? null;

        // $updatePalletStatus = $this->form->getState()['update_pallet'] ?? null;

        $PalletSerialNumbers = [];

        $InvoiceSerialNumbers = [];

        if ($reworkType == 'invoice')
        {
            if ($invoiceNo == '' || $invoiceNo == null)
            {
                Notification::make()
                    ->title("Invalid: Invoice Number")
                    ->body("Invoice number can't be empty!")
                    ->danger()
                    ->duration(5000)
                    ->send();

                $this->dispatch('loadData', '', '', $plantId, $reworkType);
                // $snoCount = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)->count();
                $this->form->fill([
                    'plant_id'       => $plantId,
                    'plant'          => $plantId,
                    'invoice_number' => null,
                    'scan_pallet_no' => null,
                    'scan_serial_no' => null,
                    'rework_type'    => $reworkType,
                    'reworkType'     => $reworkType,
                    'update_invoice' => 0,
                    'update_pallet'  => 0,
                    'created_by'     => $operatorName,
                    'scanned_by'     => $operatorName,
                ]);
                return;
            }

            $InvoiceSerialNumbers = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)->first();

            if (!$InvoiceSerialNumbers)
            {
                Notification::make()
                    ->title('Invoice Not Found')
                    ->body("Invoice number '$invoiceNo' doesn't exist in invoice table!<br><br>Scan the valid exist 'Invoice Number' to proceed..!")
                    ->danger()
                    ->duration(5000)
                    ->send();

                $this->dispatch('loadData', '', '', $plantId, $reworkType);
                $this->form->fill([
                    'plant_id'       => $plantId,
                    'plant'          => $plantId,
                    'invoice_number' => null,
                    'scan_pallet_no' => null,
                    'scan_serial_no' => null,
                    'rework_type'    => $reworkType,
                    'reworkType'     => $reworkType,
                    'update_invoice' => 0,
                    'update_pallet'  => 0,
                    'created_by'     => $operatorName,
                    'scanned_by'     => $operatorName,
                ]);
                return;
            }

            $notCompletedCount = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)
                ->where(function($query) {
                    $query->whereNull('scanned_status')
                        ->orWhere('scanned_status', '');
                })
                ->count();

            $records = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)->get();

            $allScanned = true;
            foreach ($records as $record)
            {
                if (($record->scanned_status == null) || trim($record->scanned_status) == '') {
                    $allScanned = false;
                    break;
                }
            }

            if (count($records) > 0 && $allScanned)
            {
                if (strlen($palletNo) > 0 && strlen($palletNo) < 10)
                {
                    Notification::make()
                        ->title("Invalid: Pallet Number")
                        ->body("Pallet number '$palletNo' must be at least 10 digits.")
                        ->danger()
                        ->duration(5000)
                        ->send();

                    $this->dispatch('loadData', $invoiceNo, '', $plantId, $reworkType);
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => $invoiceNo,
                        'scan_pallet_no' => null,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                    return;
                }
                else if ($serialNo == '' || $serialNo == null)
                {
                    Notification::make()
                        ->title("Invalid: Serial Number")
                        ->body("Serial number can't be empty!")
                        ->danger()
                        ->duration(5000)
                        ->send();

                    $this->dispatch('loadData', $invoiceNo, '', $plantId, $reworkType);
                    // $snoCount = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)->count();
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => $invoiceNo,
                        'scan_pallet_no' => null,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                    return;
                }
                else if (strlen($serialNo) < 9 || strlen($serialNo) > 20)
                {
                    Notification::make()
                        ->title("Invalid: Serial Number")
                        ->body("Serial number should contain minimum 9 digits and maximum 20 digits.")
                        ->danger()
                        ->duration(5000)
                        ->send();

                    $this->dispatch('loadData', $invoiceNo, '', $plantId, $reworkType);
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => $invoiceNo,
                        'scan_pallet_no' => null,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                    return;
                }
                else if (!ctype_alnum($serialNo))
                {
                    Notification::make()
                        ->title("Invalid: Serial Number")
                        ->body("Serial number '$serialNo' must contain alpha-numeric values only.")
                        ->danger()
                        ->duration(5000)
                        ->send();

                    $this->dispatch('loadData', $invoiceNo, '', $plantId, $reworkType);
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => $invoiceNo,
                        'scan_pallet_no' => null,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                    return;
                }

                $InvoiceSerialNumbers = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)->where('serial_number', $serialNo)->first();

                if (!$InvoiceSerialNumbers)
                {
                    Notification::make()
                        ->title('Serial Not Found')
                        ->body("Serial number '$serialNo' doesn't exist in scanned invoice number '$invoiceNo'!<br><br>Scan the valid exist 'Serial Number' to proceed..!")
                        ->danger()
                        ->duration(5000)
                        ->send();

                    $this->dispatch('loadData', $invoiceNo, '', $plantId, $reworkType);
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => $invoiceNo,
                        'scan_pallet_no' => null,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                    return;
                }

                foreach ($records as $row)
                {
                    if ($row->serial_number != null && $row->serial_number != '' && $row->serial_number == $serialNo)
                    {
                        ReworkLocatorInvoiceValidation::create([
                            'plant_id' => $row->plant_id,
                            'invoice_number' => $row->invoice_number,
                            'serial_number' => $row->serial_number,
                            'pallet_number' => $row->pallet_number,
                            'locator_number' => $row->locator_number,
                            'scanned_status' => $row->scanned_status,
                            'upload_status' => $row->upload_status,
                            'created_by' => $row->created_by,
                            'scanned_by' => $row->scanned_by,
                            'updated_by' => $row->updated_by,
                            'reworked_by' => $operatorName,
                            'created_at' => $row->created_at,
                            'scanned_at' => $row->scanned_at,
                            'updated_at' => $row->updated_at,
                            'reworked_at' => now(),
                        ]);

                        $row->forceDelete(); // Delete the row from the original table
                    }
                }

                Notification::make()
                    ->title('Completed: Rework Serial from Invoice')
                    ->body("Scanned serial number '$serialNo' successfully moved into rework invoice table from invoice number '$invoiceNo'.<br><br>Please, scan the next serial number to proceed..!")
                    ->success()
                    ->duration(500)
                    ->send();

                $InvoiceSerialNumbers = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)->first();

                if (!$InvoiceSerialNumbers)
                {
                    Notification::make()
                        ->title("All available serial numbers are successfully moved into rework invoice table from invoice number '$invoiceNo'.<br><br>Please, scan the next invoice number to proceed..!")
                        ->success()
                        ->duration(800)
                        ->send();

                    $this->dispatch('loadData', '', '', $plantId, $reworkType);
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => null,
                        'scan_pallet_no' => null,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                }
                else
                {
                    $this->dispatch('loadData', $invoiceNo, '', $plantId, $reworkType);
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => $invoiceNo,
                        'scan_pallet_no' => null,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                }
            }
            else
            {
                Notification::make()
                    ->title('Invoice Not Completed')
                    ->body("Scanned invoice number '$invoiceNo' doesn't completed the scanning process!<br><br>Has '$notCompletedCount' pending serial number to scan!<br><br>Please, scan the valid completed invoice number to proceed..!")
                    ->danger()
                    ->duration(5000)
                    ->send();

                $this->dispatch('loadData', '', '', $plantId, $reworkType);
                $this->form->fill([
                    'plant_id'       => $plantId,
                    'plant'          => $plantId,
                    'invoice_number' => null,
                    'scan_pallet_no' => null,
                    'scan_serial_no' => null,
                    'rework_type'    => $reworkType,
                    'reworkType'     => $reworkType,
                    'update_invoice' => 0,
                    'update_pallet'  => 0,
                    'created_by'     => $operatorName,
                    'scanned_by'     => $operatorName,
                ]);
                return;
            }
        }
        else if ($reworkType == 'pallet')
        {
            if ($palletNo == '' || $palletNo == null)
            {
                Notification::make()
                    ->title("Invalid: Pallet Number")
                    ->body("Pallet number can't be empty!")
                    ->danger()
                    ->duration(5000)
                    ->send();

                $this->dispatch('loadData', '', '', $plantId, $reworkType);
                // $snoCount = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)->count();
                $this->form->fill([
                    'plant_id'       => $plantId,
                    'plant'          => $plantId,
                    'invoice_number' => null,
                    'scan_pallet_no' => null,
                    'scan_serial_no' => null,
                    'rework_type'    => $reworkType,
                    'reworkType'     => $reworkType,
                    'update_invoice' => 0,
                    'update_pallet'  => 0,
                    'created_by'     => $operatorName,
                    'scanned_by'     => $operatorName,
                ]);
                return;
            }
            else if (strlen($palletNo) < 10)
            {
                Notification::make()
                    ->title("Invalid: Pallet Number")
                    ->body("Pallet number '$palletNo' must be at least 10 digits.")
                    ->danger()
                    ->duration(5000)
                    ->send();

                $this->dispatch('loadData', '', '', $plantId, $reworkType);
                $this->form->fill([
                    'plant_id'       => $plantId,
                    'plant'          => $plantId,
                    'invoice_number' => null,
                    'scan_pallet_no' => null,
                    'scan_serial_no' => null,
                    'rework_type'    => $reworkType,
                    'reworkType'     => $reworkType,
                    'update_invoice' => 0,
                    'update_pallet'  => 0,
                    'created_by'     => $operatorName,
                    'scanned_by'     => $operatorName,
                ]);
                return;
            }

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

            if (!$PalletSerialNumbers)
            {
                Notification::make()
                    ->title('Pallet Not Found')
                    ->body("Pallet number '$palletNo' doesn't exist in pallet table!<br><br>Scan the valid exist 'Pallet Number' to proceed..!")
                    ->danger()
                    ->duration(5000)
                    ->send();

                $this->dispatch('loadData', '', '', $plantId, $reworkType);
                $this->form->fill([
                    'plant_id'       => $plantId,
                    'plant'          => $plantId,
                    'invoice_number' => null,
                    'scan_pallet_no' => null,
                    'scan_serial_no' => null,
                    'rework_type'    => $reworkType,
                    'reworkType'     => $reworkType,
                    'update_invoice' => 0,
                    'update_pallet'  => 0,
                    'created_by'     => $operatorName,
                    'scanned_by'     => $operatorName,
                ]);
                return;
            }

            $records = PalletValidation::where('plant_id', $plantId)->where('pallet_number', $palletNo)->get();

            $allScanned = true;
            foreach ($records as $record)
            {
                if (($record->pallet_status == null) || trim($record->pallet_status) == '') {
                    $allScanned = false;
                    break;
                }
            }

            if (count($records) > 0 && $allScanned)
            {
                $locatExist = ($PalletSerialNumbers->locator_number != null && $PalletSerialNumbers->locator_number != '') ? $PalletSerialNumbers->locator_number : '';
                if ($locatExist)
                {
                    Notification::make()
                        ->title('Locator Pallet Found')
                        ->body("Scanned pallet number '$palletNo' exist in locator number '$locatExist'.<br><br>Remove scanned 'Pallet' from 'Locator' to proceed re-master packing..!")
                        ->danger()
                        ->duration(5000)
                        ->send();

                    $this->dispatch('loadData', '' , '', $plantId, $reworkType);
                    $this->form->fill([
                        'plant_id'       => $plantId,
                        'plant'          => $plantId,
                        'invoice_number' => null,
                        'scan_pallet_no' => null,
                        'scan_serial_no' => null,
                        'rework_type'    => $reworkType,
                        'reworkType'     => $reworkType,
                        'update_invoice' => 0,
                        'update_pallet'  => 0,
                        'created_by'     => $operatorName,
                        'scanned_by'     => $operatorName,
                    ]);
                    return;
                }
                else
                {
                    if ($serialNo == '' || $serialNo == null)
                    {
                        Notification::make()
                            ->title("Invalid: Serial Number")
                            ->body("Serial number can't be empty!")
                            ->danger()
                            ->duration(5000)
                            ->send();

                        $this->dispatch('loadData', '', $palletNo, $plantId, $reworkType);
                        // $snoCount = LocatorInvoiceValidation::where('plant_id', $plantId)->where('invoice_number', $invoiceNo)->count();
                        $this->form->fill([
                            'plant_id'       => $plantId,
                            'plant'          => $plantId,
                            'invoice_number' => null,
                            'scan_pallet_no' => $palletNo,
                            'scan_serial_no' => null,
                            'rework_type'    => $reworkType,
                            'reworkType'     => $reworkType,
                            'update_invoice' => 0,
                            'update_pallet'  => 0,
                            'created_by'     => $operatorName,
                            'scanned_by'     => $operatorName,
                        ]);
                        return;
                    }
                    else if (strlen($serialNo) < 9 || strlen($serialNo) > 20)
                    {
                        Notification::make()
                            ->title("Invalid: Serial Number")
                            ->body("Serial number should contain minimum 9 digits and maximum 20 digits.")
                            ->danger()
                            ->duration(5000)
                            ->send();

                        $this->dispatch('loadData', '', $palletNo, $plantId, $reworkType);
                        $this->form->fill([
                            'plant_id'       => $plantId,
                            'plant'          => $plantId,
                            'invoice_number' => null,
                            'scan_pallet_no' => $palletNo,
                            'scan_serial_no' => null,
                            'rework_type'    => $reworkType,
                            'reworkType'     => $reworkType,
                            'update_invoice' => 0,
                            'update_pallet'  => 0,
                            'created_by'     => $operatorName,
                            'scanned_by'     => $operatorName,
                        ]);
                        return;
                    }
                    else if (!ctype_alnum($serialNo))
                    {
                        Notification::make()
                            ->title("Invalid: Serial Number")
                            ->body("Serial number '$serialNo' must contain alpha-numeric values only.")
                            ->danger()
                            ->duration(5000)
                            ->send();

                        $this->dispatch('loadData', '', $palletNo, $plantId, $reworkType);
                        $this->form->fill([
                            'plant_id'       => $plantId,
                            'plant'          => $plantId,
                            'invoice_number' => null,
                            'scan_pallet_no' => $palletNo,
                            'scan_serial_no' => null,
                            'rework_type'    => $reworkType,
                            'reworkType'     => $reworkType,
                            'update_invoice' => 0,
                            'update_pallet'  => 0,
                            'created_by'     => $operatorName,
                            'scanned_by'     => $operatorName,
                        ]);
                        return;
                    }

                    $PalletSerialNumbers = PalletValidation::where('plant_id', $plantId)->where('pallet_number', $palletNo)->where('serial_number', $serialNo)->first();

                    if (!$PalletSerialNumbers)
                    {
                        Notification::make()
                            ->title('Serial Not Found')
                            ->body("Serial number '$serialNo' doesn't exist in pallet number '$palletNo'!<br><br>Scan the valid exist 'Serial Number' to proceed..!")
                            ->danger()
                            ->duration(5000)
                            ->send();

                        $this->dispatch('loadData', '', $palletNo, $plantId, $reworkType);
                        $this->form->fill([
                            'plant_id'       => $plantId,
                            'plant'          => $plantId,
                            'invoice_number' => null,
                            'scan_pallet_no' => $palletNo,
                            'scan_serial_no' => null,
                            'rework_type'    => $reworkType,
                            'reworkType'     => $reworkType,
                            'update_invoice' => 0,
                            'update_pallet'  => 0,
                            'created_by'     => $operatorName,
                            'scanned_by'     => $operatorName,
                        ]);
                        return;
                    }

                    foreach ($records as $row)
                    {
                        if ($row->serial_number != null && $row->serial_number != '' && $row->serial_number == $serialNo)
                        {
                            $row->forceDelete();
                            continue;
                        }

                        $row->pallet_status = null;
                        $row->updated_at = now();
                        $row->updated_by = $operatorName;
                        $row->save();
                    }

                    Notification::make()
                        ->title('Completed: Rework Serial from Pallet')
                        ->body("Scanned serial number '$serialNo' successfully removed from pallet number '$palletNo'.<br><br>Please, scan the next pallet number to re-master packing..!")
                        ->success()
                        ->duration(500)
                        ->send();

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

                    if (!$PalletSerialNumbers)
                    {
                        Notification::make()
                            ->title("Pallet number '$palletNo' successfully re-master packed.<br><br>Please, scan the next pallet number to re-master packing..!")
                            ->success()
                            ->duration(800)
                            ->send();

                        $this->dispatch('loadData', '', '', $plantId, $reworkType);
                        $this->form->fill([
                            'plant_id'       => $plantId,
                            'plant'          => $plantId,
                            'invoice_number' => null,
                            'scan_pallet_no' => null,
                            'scan_serial_no' => null,
                            'rework_type'    => $reworkType,
                            'reworkType'     => $reworkType,
                            'update_invoice' => 0,
                            'update_pallet'  => 0,
                            'created_by'     => $operatorName,
                            'scanned_by'     => $operatorName,
                        ]);
                    }
                    else
                    {
                        $this->dispatch('loadData', '', '', $plantId, $reworkType);
                        $this->form->fill([
                            'plant_id'       => $plantId,
                            'plant'          => $plantId,
                            'invoice_number' => null,
                            'scan_pallet_no' => null,
                            'scan_serial_no' => null,
                            'rework_type'    => $reworkType,
                            'reworkType'     => $reworkType,
                            'update_invoice' => 0,
                            'update_pallet'  => 0,
                            'created_by'     => $operatorName,
                            'scanned_by'     => $operatorName,
                        ]);
                    }
                }
            }
            else
            {
                Notification::make()
                    ->title('Pallet Not Completed')
                    ->body("Pallet number '$palletNo' doesn't completed the master packing for scanned serial number '$serialNo'!<br><br>Please, scan the valid completed pallet number to proceed..!")
                    ->danger()
                    ->duration(5000)
                    ->send();

                $this->dispatch('loadData', '' , '', $plantId, $reworkType);
                $this->form->fill([
                    'plant_id'       => $plantId,
                    'plant'          => $plantId,
                    'invoice_number' => null,
                    'scan_pallet_no' => null,
                    'scan_serial_no' => null,
                    'rework_type'    => $reworkType,
                    'reworkType'     => $reworkType,
                    'update_invoice' => 0,
                    'update_pallet'  => 0,
                    'created_by'     => $operatorName,
                    'scanned_by'     => $operatorName,
                ]);
                return;
            }
        }
        else
        {
            Notification::make()
                ->title("Rework Type Not Found")
                ->danger()
                ->duration(5000)
                ->send();

            $this->dispatch('loadData', '', '', $plantId, $reworkType);
            $this->form->fill([
                'plant_id'       => $plantId,
                'plant'          => $plantId,
                'invoice_number' => null,
                'scan_pallet_no' => null,
                'scan_serial_no' => null,
                'rework_type'    => null,
                'reworkType'     => null,
                'update_invoice' => 0,
                'update_pallet'  => 0,
                'created_by'     => $operatorName,
                'scanned_by'     => $operatorName,
            ]);
            return;
        }
    }

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