<?php
namespace EIS\Component\EIS\Administrator\Controller;

\defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Router\Route;

class ConfigController extends BaseController
{
    public function save(): void
    {
        $app = Factory::getApplication();
        $input = $app->getInput();
        $db = Factory::getDbo();

        // Eingabe
        $pdfPath = $input->getString('pdf_path', '');

        // Existiert ein Eintrag?
        $query = $db->getQuery(true)
            ->select('COUNT(*)')
            ->from($db->quoteName('#__eis_settings'));
        $db->setQuery($query);
        $exists = (int) $db->loadResult() > 0;

        if ($exists) {
            // Update
            $query = $db->getQuery(true)
                ->update($db->quoteName('#__eis_settings'))
                ->set($db->quoteName('pdf_path') . ' = ' . $db->quote($pdfPath));
        } else {
            // Insert
            $query = $db->getQuery(true)
                ->insert($db->quoteName('#__eis_settings'))
                ->columns([$db->quoteName('pdf_path')])
                ->values($db->quote($pdfPath));
        }

        $db->setQuery($query)->execute();

        $app->enqueueMessage('Pfad gespeichert: ' . $pdfPath, 'message');
        $this->setRedirect(Route::_('index.php?option=com_eis&view=config', false));
    }
}
