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

    public $resource;

    public $current_resource_contract = null;

    public $resource_contracts = [];

    public $contract_types = [];

    // #[Validate('required')] 
    public $contract_type_id = null;
    public $contract_date = null;
    public $contract_number = '';
    public $position = '';
    public $role = '';
    public $start_date = null;
    public $end_date = null;
    public $notes = '';
    public $enabled = 1;

    public $is_edit = false;

    public function resetFields(){
        $this->contract_type_id = null;
        $this->contract_date = null;
        $this->contract_number = '';
        $this->position = '';
        $this->role = '';
        $this->start_date = null;
        $this->end_date = null;
        $this->notes = '';    
        $this->enabled = 1;
    }

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

    public function render()
    {
        $this->resource_contracts = $this->resource->contracts ?? [];
        return view('livewire.resource_contract');
    }

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

    public function edit($id)
    {
        $this->resetFields();
        $this->is_edit = true;
        $this->current_resource_contract = \App\Models\ResourceContract::findOrFail($id);
        $this->contract_type_id = $this->current_resource_contract->contract_type_id;
        $this->contract_date = $this->current_resource_contract->contract_date;
        $this->contract_number = $this->current_resource_contract->contract_number;
        $this->position = $this->current_resource_contract->position;
        $this->role = $this->current_resource_contract->role;
        $this->start_date = $this->current_resource_contract->start_date;
        $this->end_date = $this->current_resource_contract->end_date;
        $this->notes = $this->current_resource_contract->notes;
        $this->enabled = $this->current_resource_contract->enabled == 1;
    }

    public function save()
    {
        $this->validate();
        try 
        {
            if ($this->current_resource_contract == null)
            {
                \App\Models\ResourceContract::create([
                    'resource_id' => $this->resource->id,
                    'contract_type_id' => $this->contract_type_id,
                    'contract_date' => $this->contract_date,
                    'contract_number' => $this->contract_number,
                    'position' => $this->position,
                    'role' => $this->role,
                    'start_date' => $this->start_date,
                    'end_date' => $this->end_date,
                    'notes' => $this->notes,
                    'enabled' => $this->enabled
                ]);
            }
            else
            {
                $this->current_resource_contract->update([
                    'contract_type_id' => $this->contract_type_id,
                    'contract_date' => $this->contract_date,
                    'contract_number' => $this->contract_number,
                    'position' => $this->position,
                    'role' => $this->role,
                    'start_date' => $this->start_date,
                    'end_date' => $this->end_date,
                    'notes' => $this->notes,
                    'enabled' => $this->enabled ? 1 : 0,
                ]);
            }
            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_contract = null;
    }

}
