<?php

namespace App\Http\Livewire\Pages\Main;

use App\Models\Orders as ModelsOrders;
use Illuminate\Support\Facades\Log;
use Livewire\Component;

class Orders extends Component
{
    public $amount;
    public $orders = [];

    public $rules = [
        'amount' => ['required', 'numeric', 'min:1', 'max:1000000'],
    ];

    public function mount()
    {
        // $this->orders = ModelsOrders::with(['user', 'car'])->get();
    }

    public function updated($property)
    {
        $this->dispatchBrowserEvent('reinitializeModal');
        $this->validateOnly($property);
    }

    public function finance($car_id, $price, $duration, $plan)
    {
        $validateData = $this->validate();

        if($validateData) {
            $this->dispatchBrowserEvent('reinitializeModal');

            createInvoice($this->amount, $car_id, $price, $duration, $plan, function($response) {
                Log::info($response->status());
            });
        }
    }

    public function render()
    {
        return view('livewire.pages.main.orders');
    }
}
