<?php

namespace App\Livewire;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
use Livewire\Attributes\Validate;
use Livewire\Component;
use Livewire\Attributes\Layout;

class ResourceRate extends Component
{

    public $resource;

    public $current_resource_rate = null;

    public $resource_rates = [];

    public $rate_groups = [];
    public $rate_types = [];
    public $company_rates = [];

    // #[Validate('required')] 
    public $rate_group_id = null;
    public $rate_type_id = null;
    public $company_rate_id = null;
    public $cost = 0;
    public $price = 0;
    public $enabled = 1;

    public $is_edit = false;

    public function resetFields(){
        $this->rate_group_id = null;
        $this->rate_type_id = null;
        $this->company_rate_id = null;
        $this->cost = 0;
        $this->price = 0;
        $this->enabled = 1;
    }

    public function mount()
    {
        $this->rate_groups = \App\Models\RateGroup::all();
        $this->rate_types = \App\Models\RateType::all();
        $this->company_rates = \App\Models\CompanyRate::all();
    }

    public function render()
    {
        $this->resource_rates = $this->resource->rates ?? [];
        return view('livewire.resource_rate');
    }

    public function add()
    {
        $this->resetFields();
        $this->is_edit = true;
        $this->current_resource_rate = null;
    }

    public function edit($id)
    {
        $this->resetFields();
        $this->is_edit = true;
        $this->current_resource_rate = \App\Models\ResourceRate::findOrFail($id);
        $this->rate_group_id = $this->current_resource_rate->rate_group_id;
        $this->rate_type_id = $this->current_resource_rate->rate_type_id;
        $this->company_rate_id = $this->current_resource_rate->company_rate_id;
        $this->cost = $this->current_resource_rate->cost;
        $this->price = $this->current_resource_rate->price;
        $this->enabled = $this->current_resource_equipment->enabled == 1;
    }

    public function save()
    {
        $this->validate();
        try 
        {
            if ($this->current_resource_rate == null)
            {
                \App\Models\ResourceRate::create([
                    'resource_id' => $this->resource->id,
                    'rate_group_id' => $this->rate_group_id,
                    'rate_type_id' => $this->rate_type_id,
                    'company_rate_id' => $this->company_rate_id,
                    'cost' => $this->cost,
                    'price' => $this->price,
                    'enabled' => $this->enabled
                ]);
            }
            else
            {
                $this->current_resource_rate->update([
                    'rate_group_id' => $this->rate_group_id,
                    'rate_type_id' => $this->rate_type_id,
                    'company_rate_id' => $this->company_rate_id,
                    'cost' => $this->cost,
                    'price' => $this->price,
                    'enabled' => $this->enabled
                ]);
            }
            session()->flash('success','Dati salvati con successo');
            $this->resetFields();
            $this->is_edit = false;
        } catch (\Exception $ex) {
            session()->flash('error','Errore (' . $ex->getMessage() . ')');
        }
    }

    public function cancel()
    {
        $this->resetFields();
        $this->is_edit = false;
        $this->current_resource_rate = null;
    }

}
