<?php

namespace App\Livewire;

use App\Models\Customer;
use Illuminate\Support\Collection;
use Livewire\Component;

class CustomerAndContactSelect extends Component
{
    public Collection $customers;

    public Collection $contacts;

    public string $selectedCustomer;

    public function mount(Collection $customers)
    {
        $this->customers = $customers;

        if (isset($this->selectedCustomer)) {
            $this->contacts = Customer::find($this->selectedCustomer)->contacts;
        } else {
            $this->contacts = $customers->first()->contacts;
        }

    }

    public function updateContactList()
    {
        $this->contacts = Customer::find($this->selectedCustomer)->contacts;
    }

    public function render()
    {
        return view('livewire.customer-and-contact-select', [
            'customers' => $this->customers,
            'contacts'  => $this->contacts,
        ]);
    }
}
