<?php

namespace App\Livewire\CustomerPortal;

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

class Estimates extends Component
{
    use WithPagination;

    public $filterStatus = '';

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

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

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

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