<?php

namespace App\Livewire;

use App\Models\ProductionQuantity;
use App\Models\QualityValidation;
use Livewire\Component;

class ProductionSapData extends Component
{
    public $plantId;
    public $productionOrder;


    public $items = [];

    public bool $attempted = false;

    public bool $refresh = false;

    public $scanned_quantity;

    protected $listeners = [
        'loadItems',
        'refreshed',

    ];

    public function refreshed($plantId, $productionOrder)
    {
        $this->plantId = $plantId;
        $this->productionOrder = $productionOrder;
        $this->refresh = true;
        $this->attempted = false;

        if (!$plantId || !$productionOrder) {
            $this->items = [];
            return;
        }

        $this->items = ProductionQuantity::where('plant_id', $plantId)
        ->where('production_order', $productionOrder)
        ->get()
        ->map(function ($item) {
            return [
                'item_code' => optional($item->item)->code ?? 'N/A',
                'serial_number' => $item->serial_number,
                'created_at' => $item->created_at->format('Y-m-d H:i:s'),
                'sap_status' => $item->sap_msg_status,
                'sap_description' => $item->sap_msg_description,
            ];
        })
        ->toArray();

        $this->updateRowCount();
    }

    public function loadItems($plantId, $productionOrder)
    {
        $this->plantId = $plantId;
        $this->productionOrder = $productionOrder;
        $this->attempted = true;
        $this->refresh = false;


        if (!$plantId || !$productionOrder) {
            $this->items = [];
            return;
        }

        $this->items = ProductionQuantity::where('plant_id', $plantId)
            ->where('production_order', $productionOrder)
            ->get()
            ->map(function ($item) {
                return [
                    'item_code' => optional($item->item)->code ?? 'N/A',
                    'serial_number' => $item->serial_number,
                    'created_at' => $item->created_at->format('Y-m-d H:i:s'),
                    'sap_status' => $item->sap_msg_status,
                    'sap_description' => $item->sap_msg_description,
                ];
            })
            ->toArray();

        $this->updateRowCount();
    }

    // Method to update row count based on scanned_quantity
    public function updateRowCount()
    {
        $this->attempted = true;

        // Ensure scanned_quantity is a valid number and doesn't exceed the available rows
        if ($this->scanned_quantity > 0 && $this->scanned_quantity <= count($this->items)) {
            $this->items = array_slice($this->items, 0, $this->scanned_quantity);
        } else {
            $this->scanned_quantity = count($this->items);  // Reset to show all items if invalid quantity
        }

        $this->dispatch('updateScannedQuantity', $this->scanned_quantity);
    }
    public function render()
    {
        return view('livewire.production-sap-data');
    }
}
