<?php

namespace App\Livewire;

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

class LocatorDataTable extends Component
{
    public $plantId;

    public $palletNumber, $serialNumber;

    public $locatorNumber;

    public $locators = [];

    public $locators1 = [];

    public $serials = [];

    protected $listeners = [
        'loadData' => 'loadPalletData',
        'loadLocator' => 'loadLocatorData',
        'loadserialNo' => 'loadserialNodata',
    ];

    public function loadserialNodata($serialNo, $plantId)
    {
        $this->plantId = $plantId;
        $this->serialNumber = $serialNo;
        $this->locators = [];

        $record = PalletValidation::where('plant_id', $plantId)
            ->where('serial_number', $serialNo)
            ->whereNotNull('locator_number')
            ->where('locator_number', '!=', '')
            ->first();

        if (!$record)
        {
            return;
        }

        $locatorNumber = $record->locator_number;

        $this->locators = PalletValidation::where('plant_id', $plantId)
            ->where('locator_number', $locatorNumber)
            ->whereNotNull('locator_number')
            ->where('locator_number', '!=', '')
            ->get()
            ->map(function ($record) {
                return [
                    'created_at' => $record->created_at ?? '',
                    'created_by' => $record->created_by ?? '',
                    'pallet_number' => $record->pallet_number ?? '',
                    'serial_number' => $record->serial_number ?? '',
                    'locator_number' => $record->locator_number ?? '',
                    'locator_quantity' => $record->locator_quantity ?? '',
                    'updated_at' => $record->updated_at ?? '',
                    'updated_by' => $record->updated_by ?? '',
                ];
            })
            ->toArray();
    }

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

        $this->locators = PalletValidation::query()
            ->where('plant_id', $plantId)
            ->where(function ($query) use ($palletNumber, $locatorNumber) {
                $query->where(function ($q) use ($palletNumber) {
                        $q->where('pallet_number', $palletNumber)
                        ->whereNotNull('locator_number')
                        ->where('locator_number', '!=', '');
                    })
                    ->orWhere(function ($q) use ($locatorNumber) {
                        $q->where('locator_number', $locatorNumber)
                        ->whereNotNull('locator_number')
                        ->where('locator_number', '!=', '');
                    });
            })
            ->get()
            ->map(function ($record) {
                return [
                    'created_at' => $record->created_at ?? '',
                    'created_by' => $record->created_by ?? '',
                    'pallet_number' => $record->pallet_number ?? '',
                    'serial_number' => $record->serial_number ?? '',
                    'locator_number' => $record->locator_number ?? '',
                    'locator_quantity' => $record->locator_quantity ?? '',
                    'updated_at' => $record->updated_at ?? '',
                    'updated_by' => $record->updated_by ?? '',
                ];
            })
            ->toArray();
    }

    public function loadLocatorData($scanLocator, $plantId)
    {
        $this->plantId = $plantId;
        $this->locatorNumber = $scanLocator;
        $this->locators = [];

        $this->locators = PalletValidation::query()
            ->where('plant_id', $plantId)
            ->where('locator_number', $scanLocator)
            ->get()
            ->map(function ($record) {
                return [
                    'created_at' => $record->created_at ?? '',
                    'created_by' => $record->created_by ?? '',
                    'pallet_number' => $record->pallet_number ?? '',
                    'serial_number' => $record->serial_number ?? '',
                    'locator_number' => $record->locator_number ?? '',
                    'locator_quantity' => $record->locator_quantity ?? '',
                    'updated_at' => $record->updated_at ?? '',
                    'updated_by' => $record->updated_by ?? '',
                ];
            })
            ->toArray();
    }

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