<?php

namespace App\Livewire\Calendar;

use Livewire\Attributes\On;
use Livewire\Component;

class Sidebar extends Component
{
    public $open = false;
    public $mode = 'create'; // create | edit

    public $eventId = null;
    public $date = null;
    public $time = null;

    protected $listeners = [
        'sidebar:openEvent' => 'openEvent',
        'sidebar:createEvent' => 'createEvent',
        'sidebar:close' => 'close',

        'event:save' => 'saveEvent',
        'event:delete' => 'deleteEvent',
    ];

    public function saveEvent()
    {
        $this->dispatch('eventForm:save');
    }

    public function deleteEvent()
    {
        $this->dispatch('eventForm:delete');
    }

    public function openEvent($eventId, $date = null)
    {
        $this->eventId = $eventId;
        $this->date = $date; // 🔥 WICHTIG
        $this->mode = 'edit';
        $this->open = true;
    }

    public function createEvent($date = null, $time = null)
    {
        $this->eventId = null;
        $this->date = $date;
        $this->time = $time;
        $this->mode = 'create';
        $this->open = true;
    }

    public function close()
    {
        $this->reset(['eventId', 'date', 'time']);
        $this->open = false;
    }

    public function render()
    {
        return view('livewire.calendar.sidebar');
    }

}
