<?php

namespace App\Livewire\ServiceOrders;

use Livewire\Component;
use App\Models\ServiceOrder;

class Show extends Component
{
    public ServiceOrder $serviceOrder;

    public function mount(ServiceOrder $serviceOrder)
    {
        $this->serviceOrder = $serviceOrder->load([
            'customer', 
            'vehicle', 
            'assignedTechnician',
            'serviceItems',
            'parts',
            'inspections',
            'appointments'
        ]);
    }

    public function updateStatus($status)
    {
        $this->serviceOrder->status = $status;
        
        if ($status === 'in_progress' && !$this->serviceOrder->started_at) {
            $this->serviceOrder->started_at = now();
        } elseif ($status === 'completed' && !$this->serviceOrder->completed_at) {
            $this->serviceOrder->completed_at = now();
        }
        
        $this->serviceOrder->save();
        session()->flash('success', 'Service order status updated successfully!');
        
        // Refresh the model
        $this->mount($this->serviceOrder);
    }

    public function render()
    {
        return view('livewire.service-orders.show');
    }
}
