<?php

namespace App\Livewire;

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

class StickerValidation 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->records = ProductionQuantity::where('plant_id', $plantId)
        // ->where('production_order', $refNumber)
        // ->orderBy('created_at', 'asc')
        // ->get(['serial_number', 'operator_id']);

        $this->records = ProductionQuantity::query()
        ->where('production_quantities.plant_id', $plantId)
        ->where('production_quantities.production_order', $refNumber)
        ->leftJoin(
            'sticker_validations',
            'sticker_validations.serial_number',
            '=',
            'production_quantities.serial_number'
        )
        ->orderBy('production_quantities.created_at', 'asc')
        ->get([
            'production_quantities.serial_number',
            'production_quantities.operator_id',
            'sticker_validations.status',
            'sticker_validations.sticker_id',
        ])
        ->map(function ($row) {
            $row->status = $row->status ?? '';
            return $row;
        });

        //dd($this->records);

    }


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