<?php

namespace App\Http\Livewire\Traits;

use App\Models\ReportProtocolloNote;

trait HasNote{
    public $noteText;
    public $notes;
    public function addNote()
    {
        ReportProtocolloNote::create([
            'report_id' => $this->dataId,
            'text' => $this->noteText,
            'created' => now(),
            'created_by' => auth()->id() ?? 0,
        ]);

        $this->noteText = '';
        $this->refreshNotes();
    }

    public function removeNote($id)
    {
        ReportProtocolloNote::findOrFail($id)->delete();
        $this->refreshNotes();
    }

    private function refreshNotes()
    {
        $this->notes = ReportProtocolloNote::where('report_id', $this->dataId)->get();
    }
}
