<?php
\defined('_JEXEC') or die;

use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\HTML\HTMLHelper;

// Wert aus der View (HtmlView::getPdfPath()), Fallback leer
$pdfPath = method_exists($this, 'getPdfPath') ? (string) $this->getPdfPath() : '';
?>
<h2 class="mb-3">
  <?php echo Text::_('COM_EIS_SETTINGS_TITLE') ?: 'Einstellungen'; ?>
</h2>

<form action="<?php echo Route::_('index.php?option=com_eis&task=config.save'); ?>"
      method="post"
      name="adminForm"
      id="adminForm"
      novalidate>
  <div class="form-horizontal">
    <fieldset class="adminform">
      <legend><?php echo Text::_('COM_EIS_FIELDSET_GENERAL') ?: 'Allgemein'; ?></legend>

      <div class="control-group">
        <label class="control-label" for="document_root">
          <?php echo Text::_('COM_EIS_FIELD_DOCUMENT_ROOT_LABEL') ?: 'Basis-Verzeichnis für PDFs'; ?>
        </label>
        <div class="controls">
          <input
            type="text"
            class="form-control"
            name="document_root"
            id="document_root"
            value="<?php echo htmlspecialchars($pdfPath, ENT_QUOTES, 'UTF-8'); ?>"
            placeholder="/var/www/pdf"
            size="60"
            required
          />
          <p class="help-block" style="margin-top:.35rem;">
            <?php echo Text::_('COM_EIS_FIELD_DOCUMENT_ROOT_DESC')
              ?: 'Absoluter Serverpfad, in dem die PDF-Dateien liegen (z. B. /var/www/pdf).'; ?>
          </p>
        </div>
      </div>

    </fieldset>
  </div>

  <div class="mt-2">
    <button type="submit" class="btn btn-primary">
      <?php echo Text::_('JSAVE') ?: 'Speichern'; ?>
    </button>
  </div>

  <?php echo HTMLHelper::_('form.token'); ?>
</form>

<script>
document.addEventListener('DOMContentLoaded', function () {
  const form = document.getElementById('adminForm');
  if (!form) return;

  form.addEventListener('submit', function (e) {
    const input = document.getElementById('document_root');
    if (!input || !input.value.trim()) {
      e.preventDefault();
      alert('<?php echo Text::_('COM_EIS_MSG_INVALID_PATH') ?: 'Bitte einen gültigen Pfad angeben.'; ?>');
      input?.focus();
    }
  });
});
</script>
