<?php


namespace App\Components\Forms;


use App\Model\Enums\FlashMessage;
use Nette\Application\AbortException;
use Throwable;
use Tracy\Debugger;
use xhulav\BootstrapForms\Rendering\BootstrapHorizontalFormRenderer;

class EventCreateForm extends EventForm
{
    /**
     * @return BootstrapForm
     */
    protected function createComponentForm() : BootstrapForm
    {
        $form = $this->setupForm();
        $form->addSubmit('submit', 'Create');

        $form->setRenderer(new BootstrapHorizontalFormRenderer());
        return $form;
    }

    /**
     * @param BootstrapForm $form
     * @param $values
     * @throws AbortException
     */
    public function formSucceeded(BootstrapForm $form, $values)
    {
        try {
            $event = $this->siteFacade->getEventWriter()->createEvent($this->site, $values->template, $values->name, $values->description, $values->start, $values->end, $values->limit);
            $this->onSave($this, $event);
        } catch (AbortException $e) {
            throw $e;   // redirect
        } catch (Throwable $e) {
            Debugger::log($e);
            $this->presenter->flashMessage('Record saving has failed.', FlashMessage::ERROR);
            $this->onError($this);
        }
    }
}