<?php
/** @var action_plugin_bez $this */

use \dokuwiki\plugin\bez;

if ($this->model->get_level() < BEZ_AUTH_ADMIN) {
    throw new bez\meta\PermissionDeniedException();
}

$labels = $this->model->labelFactory->get_all();

$id = null;
if (isset($_POST['id'])) {
    $id = (int)$_POST['id'];
} else {
    $id = $this->get_param('id');
}

if ($id) {
    $label = $this->model->labelFactory->get_one($id);
} else {
    $label = $this->model->labelFactory->create_object();
}

$this->tpl->set('labels', $labels);
$this->tpl->set('label', $label);
$this->tpl->set('error', '');


if ($this->get_param('action') === 'edit') {

    $this->tpl->set_values($label->get_assoc());

} else if ($this->get_param('action') === 'remove') {

    $this->model->labelFactory->delete($label);

    header('Location: '.$this->url('types'));

} elseif (count($_POST) > 0) {
    try {
        $label->set_data($_POST);
        $this->model->labelFactory->save($label);

        header('Location: '.$this->url('types'));
    } catch (\Exception $e) {
        if ($e->getMessage() === 'duplicate_name') {
            $this->tpl->set('error', 'Ya existe un cliente con el nombre "' . htmlspecialchars($_POST['name']) . '". Por favor, use un nombre diferente.');
            $this->tpl->set_values($_POST);
        } else {
            throw $e;
        }
    }
}
