<?php

namespace App\Livewire;

use Livewire\Component;
use App\Models\StickerPrinting;

class StickerPrintData extends Component
{
    public $plantId;

    public $refNumber;

    public $serialNumber;

    public bool $materialInvoice = false;

    public $records = [];

    protected $listeners = [
        'refreshEmptySticker' => 'loadStickerData',
        'addStickerToList' => 'loadSticker'
    ];

    public function loadStickerData($plantId, $refNumber)
    {
        $this->plantId = $plantId;
        $this->refNumber = $refNumber;
        $this->materialInvoice = true;

        $this->records = StickerPrinting::where('plant_id', $plantId)
        ->where('reference_number', $refNumber)
        ->orderBy('created_at', 'asc')
        ->get(['serial_number', 'created_by']);

    }

    public function loadSticker($plantId, $refNumber, $serialNumber)
    {

        $this->plantId = $plantId;
        $this->refNumber = $refNumber;
        $this->materialInvoice = true;

        $this->records = StickerPrinting::where('plant_id', $plantId)
        ->where('reference_number', $refNumber)
        ->orderBy('created_at', 'asc')
        ->get(['serial_number', 'created_by']);
    }

    public function render()
    {
        return view('livewire.sticker-print-data');
    }
}
