<?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 ResourceEquipment extends Component
{

    public $resource;

    public $current_resource_equipment = null;

    public $resource_equipments = [];

    public $equipment_categories = [];

    // #[Validate('required')] 
    public $equipment_category_id = null;
    public $name = '';
    public $start_date = null;
    public $end_date = null;
    public $supplier = '';
    public $sku = '';
    public $description = '';
    public $enabled = 1;

    public $is_edit = false;

    public function resetFields(){
        $this->equipment_category_id = null;
        $this->name = '';
        $this->start_date = null;
        $this->end_date = null;
        $this->supplier = '';
        $this->sku = '';        
        $this->description = '';    
        $this->enabled = 1;
    }

    public function mount()
    {
        $this->equipment_categories = \App\Models\EquipmentCategory::all();
    }

    public function render()
    {
        $this->resource_equipments = $this->resource->equipments ?? [];
        return view('livewire.resource_equipment');
    }

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

    public function edit($id)
    {
        $this->resetFields();
        $this->is_edit = true;
        $this->current_resource_equipment = \App\Models\ResourceEquipment::findOrFail($id);
        $this->equipment_category_id = $this->current_resource_equipment->equipment_category_id;
        $this->name = $this->current_resource_equipment->name;
        $this->start_date = $this->current_resource_equipment->start_date;
        $this->end_date = $this->current_resource_equipment->end_date;
        $this->supplier = $this->current_resource_equipment->supplier;
        $this->sku = $this->current_resource_equipment->sku;
        $this->description = $this->current_resource_equipment->description;
        $this->enabled = $this->current_resource_equipment->enabled == 1;
    }

    public function save()
    {
        $this->validate();
        try 
        {
            if ($this->current_resource_equipment == null)
            {
                \App\Models\ResourceEquipment::create([
                    'resource_id' => $this->resource->id,
                    'equipment_category_id' => $this->equipment_category_id,
                    'name' => $this->name,
                    'start_date' => $this->start_date,
                    'end_date' => $this->end_date,
                    'supplier' => $this->supplier,
                    'sku' => $this->sku,
                    'description' => $this->description,
                    'enabled' => $this->enabled
                ]);
            }
            else
            {
                $this->current_resource_equipment->update([
                    'equipment_category_id' => $this->equipment_category_id,
                    'name' => $this->name,
                    'start_date' => $this->start_date,
                    'end_date' => $this->end_date,
                    'supplier' => $this->supplier,
                    'sku' => $this->sku,
                    'description' => $this->description,
                    '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_equipment = null;
    }

}
