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

    public $resource;

    public $current_resource_document = null;

    public $resource_documents = [];

    // #[Validate('required')] 
    public $document_type = '';
    public $name = '';
    public $description = '';
    public $file = '';
    public $enabled = 1;

    public $is_edit = false;

    public function resetFields(){
        $this->document_type = null;
        $this->name = null;
        $this->description = '';
        $this->file = '';
        $this->enabled = 1;
    }

    public function render()
    {
        $this->resource_documents = $this->resource->documents ?? [];
        return view('livewire.resource_document');
    }

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

    public function edit($id)
    {

        $file = '';

        $this->resetFields();
        $this->is_edit = true;
        $this->current_resource_document = \App\Models\ResourceDocument::findOrFail($id);
        $this->document_type = $this->current_resource_document->contract_type_id;
        $this->name = $this->current_resource_document->contract_date;
        $this->description = $this->current_resource_document->contract_number;
        $this->file = $file; // TODO - gestire file old
        $this->enabled = $this->current_resource_document->enabled == 1;
    }

    public function save()
    {
        $this->validate();
        try 
        {

            // TODO - gestire file upload
            $file = '';

            if ($this->current_resource_document == null)
            {
                \App\Models\ResourceContract::create([
                    'resource_id' => $this->resource->id,
                    'document_type' => $this->document_type,
                    'name' => $this->name,
                    'description' => $this->description,
                    'file' => $file,
                    'enabled' => $this->enabled
                ]);
            }
            else
            {
                $this->current_resource_document->update([
                    'document_type' => $this->document_type,
                    'name' => $this->name,
                    'description' => $this->description,
                    'file' => $file,
                    '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_document = null;
    }

}
