<?php

namespace App\Livewire\Inventory\Parts;

use App\Models\Part;
use Livewire\Component;

class Show extends Component
{
    public Part $part;
    public $tab = 'details';

    public function mount(Part $part)
    {
        $this->part = $part->load(['supplier', 'stockMovements.createdBy']);
        $this->tab = request()->get('tab', 'details');
    }

    public function showHistory()
    {
        return $this->redirect(route('inventory.parts.show', $this->part) . '?tab=history', navigate: true);
    }

    public function addStockMovement()
    {
        return $this->redirect(route('inventory.stock-movements.create') . '?part_id=' . $this->part->id, navigate: true);
    }

    public function createPurchaseOrder()
    {
        return $this->redirect(route('inventory.purchase-orders.create') . '?part_id=' . $this->part->id, navigate: true);
    }

    public function goBack()
    {
        return $this->redirect(route('inventory.parts.index'), navigate: true);
    }

    public function edit()
    {
        return $this->redirect(route('inventory.parts.edit', $this->part), navigate: true);
    }

    public function render()
    {
        if ($this->tab === 'history') {
            return view('livewire.inventory.parts.show', [
                'showHistory' => true
            ]);
        }

        return view('livewire.inventory.parts.show', [
            'showHistory' => false
        ]);
    }
}
