<?php

namespace App\Livewire\CustomerPortal;

use App\Models\JobCard;
use App\Models\Customer;
use Livewire\Component;
use Livewire\WithPagination;
use Illuminate\Support\Facades\Auth;

class WorkOrders extends Component
{
    use WithPagination;

    public $filterStatus = '';

    public function updatingFilterStatus()
    {
        $this->resetPage();
    }

    public function render()
    {
        $user = Auth::user();
        $customer = Customer::where('email', $user->email)->first();

        $workOrders = collect();
        
        if ($customer) {
            $workOrders = JobCard::where('customer_id', $customer->id)
                ->when($this->filterStatus, function ($query) {
                    $query->where('status', $this->filterStatus);
                })
                ->with(['vehicle', 'serviceAdvisor', 'estimates'])
                ->orderBy('created_at', 'desc')
                ->paginate(10);
        }

        return view('livewire.customer-portal.work-orders', compact('workOrders'))
            ->layout('layouts.customer-portal-app');
    }
}
