<?php

namespace App\Livewire;

use App\Models\WireMasterPacking;
use Livewire\Component;

class WireMasterDataTable extends Component
{

    public $plantId;

    public $wirePackNo;

    public $snoCount = 0;

    public $records = [];

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

    public function loadWireMasterData($wirePackNo, $plantId)
    {
        $this->plantId = $plantId;
        $this->wirePackNo = $wirePackNo;
        $this->records = [];

        $this->records = WireMasterPacking::query()
            ->where('plant_id', $this->plantId)
            ->where('wire_packing_number', $this->wirePackNo)
            ->orderBy('scanned_at')
            ->get()
            ->map(function ($record) {
                return [
                    'created_at' => $record->created_at,
                    'created_by' => $record->created_by ?? '',
                    'wire_packing_number' => $record->wire_packing_number,
                    'item_code'          => $record->item?->code ?? '',
                    'item_description'   => $record->item?->description ?? '',
                    'process_order' => $record->process_order,
                    'weight' => $record->weight,
                    'scanned_at' => $record->scanned_at,
                    'scanned_by' => $record->scanned_by ?? '',
                ];
            })
            ->toArray();

    }

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