<?php

namespace App\Livewire;

use App\Models\LocatorInvoiceValidation;
use App\Models\PalletValidation;
use Livewire\Component;
use Notification;

class InvoiceFinderDataTable extends Component
{

    public $plantId;

    public $invoiceNumber;

    public $records = [];

    protected $listeners = [
        'loadData' => 'loadlocatorInvoiceData',
    ];


    public function loadlocatorInvoiceData($invoiceNumber, $notFoundSerials, $incompleteSerials, $scannedSerials, $foundSerials, $plantId)
    {
        $this->records = [];

        if(count($notFoundSerials) > 0)
        {
            foreach($notFoundSerials as $serial)
            {
                $record = LocatorInvoiceValidation::where('serial_number', $serial)
                ->where('plant_id', $plantId)
                ->first();

                $this->records[] = [
                    'created_at' => $record->created_at,
                    'created_by' => $record->created_by ?? '',
                    'serial_number' => $record->serial_number,
                    'pallet_number' => '',
                    'locator_number' => '',
                    'scanned_status' => 'Not Exist',
                    'scanned_at' => '',
                    'scanned_by' => ''
                ];
            }
        }

        if(count($incompleteSerials) > 0)
        {
            foreach($incompleteSerials as $serial)
            {
                $locatorRecord = LocatorInvoiceValidation::where('serial_number', $serial)
                ->where('plant_id', $plantId)
                ->first();

                $palletRecord = palletValidation::where('serial_number', $serial)
                ->where('plant_id', $plantId)
                ->first();

                $this->records[] = [
                    'created_at' => $locatorRecord->created_at,
                    'created_by' => $locatorRecord->created_by,
                    'serial_number' => $locatorRecord->serial_number,
                    'pallet_number' => $palletRecord->pallet_number,
                    'locator_number' => $palletRecord->locator_number,
                    'scanned_status' => 'Incompleted',
                    'scanned_at' => '',
                    'scanned_by' => ''
                ];
            }
        }

        if(count($foundSerials) > 1)
        {
            $foundPno = [];
            $foundLno = [];
            $foundSno = [];
            $palletNo = '';
            $locatorNo = '';
            $palletSno = [];
            $locatorSno = [];
            foreach($foundSerials as $serial)
            {
                foreach($foundSerials as $sNo)
                {
                    if (in_array($sNo, $foundSno))
                    {
                        continue;
                    }

                    $palletRecord = palletValidation::where('serial_number', $sNo)
                        ->where('plant_id', $plantId)
                        ->where('pallet_number', '!=','')
                        ->where('pallet_number', '!=',null)
                        // ->whereNotNull('pallet_number')
                        ->first();

                    if(!$palletRecord || ($palletNo && count($foundPno) > 0 && !in_array($palletRecord->pallet_number, $foundPno)))
                    {
                        continue;
                    }
                    else
                    {
                        $palletNo = $palletRecord->pallet_number;
                        $locatorNo = $palletRecord->locator_number;
                        $foundSno[] = $sNo;
                        $foundPno[] = $palletRecord->pallet_number;
                        $palletSno[] = $sNo;
                    }
                }

                if(count($palletSno) > 1)
                {
                    $locatorRecord = null;

                    foreach($palletSno as $mulSNo)
                    {
                        $locatorRecord = LocatorInvoiceValidation::where('serial_number', $mulSNo)
                            ->where('plant_id', $plantId)
                            ->first();

                    }

                    $this->records[] = [
                        'created_at' => $locatorRecord->created_at,
                        'created_by' => $locatorRecord->created_by,
                        'serial_number' => count($palletSno),
                        'pallet_number' => $palletNo,
                        'locator_number' => $locatorNo,
                        'scanned_status' => '',
                        'scanned_at' => '',
                        'scanned_by' => ''
                    ];

                    $palletNo = '';
                    $locatorNo = '';
                    $palletSno = [];
                }
                else if(count($palletSno) == 1)
                {
                    foreach($palletSno as $singleSNo)
                    {
                        $palletRecord = palletValidation::where('serial_number', $singleSNo)
                            ->where('plant_id', $plantId)
                            ->where('pallet_number', '!=','')
                            ->whereNotNull('pallet_number')
                            ->first();

                        $locatorRecord = LocatorInvoiceValidation::where('serial_number', $singleSNo)
                            ->where('plant_id', $plantId)
                            ->first();
                        $this->records[] = [
                            'created_at' => $locatorRecord->created_at,
                            'created_by' => $locatorRecord->created_by,
                            'serial_number' => $locatorRecord->serial_number,
                            'pallet_number' => $palletRecord->pallet_number,
                            'locator_number' => $palletRecord->locator_number,
                            'scanned_status' => '',
                            'scanned_at' => '',
                            'scanned_by' => ''
                        ];
                    }
                    $palletNo = '';
                    $locatorNo = '';
                    $palletSno = [];
                }

                if (in_array($serial, $foundSno))
                {
                    continue;
                }

                $int = 0;
                foreach($foundSerials as $locSNo)
                {
                    if (in_array($locSNo, $foundSno))
                    {
                        continue;
                    }

                    $int++;
                    $locatorRecord = null;
                    $locatorRecord = palletValidation::where('serial_number', $locSNo)
                        ->where('plant_id', $plantId)
                        ->where('locator_number', '!=','')
                        ->where('locator_number', '!=',null)
                        ->first();

                    if(!$locatorRecord || ($locatorNo && count($foundLno) > 0 && !in_array($locatorRecord->locator_number, $foundLno)))
                    {
                        continue;
                    }
                    else
                    {
                        $palletNo = $locatorRecord->pallet_number;
                        $locatorNo = $locatorRecord->locator_number;
                        $foundSno[] = $locSNo;
                        $foundLno[] = $locatorRecord->locator_number;
                        $locatorSno[] = $locSNo;
                    }
                }

                if(count($locatorSno) > 1)
                {
                    $locatorRecord = null;

                    foreach($locatorSno as $mulSNo)
                    {
                        $locatorRecord = LocatorInvoiceValidation::where('serial_number', $mulSNo)
                            ->where('plant_id', $plantId)
                            ->first();
                    }

                    $this->records[] = [
                        'created_at' => $locatorRecord->created_at,
                        'created_by' => $locatorRecord->created_by,
                        'serial_number' => count($locatorSno),
                        'pallet_number' => $palletNo,
                        'locator_number' => $locatorNo,
                        'scanned_status' => '',
                        'scanned_at' => '',
                        'scanned_by' => ''
                    ];

                    $palletNo = '';
                    $locatorNo = '';
                    $locatorSno = [];
                }
                else if(count($locatorSno) == 1)
                {
                    foreach($locatorSno as $singleSNo)
                    {
                        $palletRecord = palletValidation::where('serial_number', $singleSNo)
                            ->where('plant_id', $plantId)
                            ->where('locator_number', '!=','')
                            ->where('locator_number', '!=',null)
                            ->first();

                        $locatorRecord = LocatorInvoiceValidation::where('serial_number', $singleSNo)
                            ->where('plant_id', $plantId)
                            ->first();
                        $this->records[] = [
                            'created_at' => $locatorRecord->created_at,
                            'created_by' => $locatorRecord->created_by,
                            'serial_number' => $locatorRecord->serial_number,
                            'pallet_number' => $palletRecord->pallet_number,
                            'locator_number' => $palletRecord->locator_number,
                            'scanned_status' => '',
                            'scanned_at' => '',
                            'scanned_by' => ''
                        ];
                    }
                    $palletNo = '';
                    $locatorNo = '';
                    $locatorSno = [];
                }
            }
        }
        else if(count($foundSerials) == 1)
        {

            foreach($foundSerials as $serial)
            {

                $locatorRecord = LocatorInvoiceValidation::where('serial_number', $serial)
                ->where('plant_id', $plantId)
                ->first();

                $palletRecord = palletValidation::where('serial_number', $serial)
                ->where('plant_id', $plantId)
                ->first();

                $this->records[] = [
                    'created_at' => $locatorRecord->created_at,
                    'created_by' => $locatorRecord->created_by,
                    'serial_number' => $locatorRecord->serial_number,
                    'pallet_number' => $palletRecord->pallet_number,
                    'locator_number' => $palletRecord->locator_number,
                    'scanned_status' => '',
                    'scanned_at' => '',
                    'scanned_by' => ''
                ];
            }
        }

        if(count($scannedSerials) > 0)
        {
            foreach($scannedSerials as $serial)
            {
                $locatorRecord = LocatorInvoiceValidation::where('serial_number', $serial)
                ->where('plant_id', $plantId)
                ->first();

                $this->records[] = [
                    'created_at' => $locatorRecord->created_at,
                    'created_by' => $locatorRecord->created_by,
                    'serial_number' => $locatorRecord->serial_number,
                    'pallet_number' => $locatorRecord->pallet_number,
                    'locator_number' => $locatorRecord->locator_number,
                    'scanned_status' => $locatorRecord->scanned_status,
                    'scanned_at' => $locatorRecord->scanned_at,
                    'scanned_by' => $locatorRecord->scanned_by
                ];
            }
        }
    }

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