<?php

$templateHtml = (string) ($templateHtml ?? '');
$variables = is_array($variables ?? null) ? $variables : [];
$hasLogo = (bool) ($hasLogo ?? false);
$logoVersion = (string) ($logoVersion ?? '');

$logoCurrentSrc = '';
if ($hasLogo) {
    $logoCurrentSrc = lurl('security/report-design/logo-file') . '?size=256'
        . ($logoVersion !== '' ? '&v=' . rawurlencode($logoVersion) : '');
}
?>
<div class="app-details-container">
  <section data-tab-panel data-tab-panel-wide>
    <?php
    $titlebar = [
        'title' => t('Customer report design'),
        'actions' => [
            ['label' => t('Save'), 'type' => 'submit', 'form' => 'security-report-design-form', 'class' => 'primary', 'tone' => 'success'],
        ],
    ];
    require templatePath('partials/app-details-titlebar.phtml');
    ?>

    <p class="app-muted"><?php e(t('Configure how the customer security-check report PDF looks: upload a logo and edit the HTML/CSS template that is rendered to the PDF.')); ?></p>

    <div class="app-details-card">
      <h3><?php e(t('Report logo')); ?></h3>
      <?php
      $fileUpload = [
          'name' => 'logo',
          'accept' => 'image/svg+xml,image/png,image/jpeg,image/webp',
          'hint' => t('Allowed file types: SVG, PNG, JPG, WEBP. Max 5 MB.'),
          'currentSrc' => $logoCurrentSrc,
          'currentLabel' => t('Current logo'),
          'deleteConfirm' => t('Delete this logo?'),
          'formId' => 'security-report-design-form',
          'deleteFormId' => 'security-report-logo-delete-form',
      ];
      require templatePath('partials/app-file-upload.phtml');
      ?>
    </div>

    <form method="post" id="security-report-design-form" enctype="multipart/form-data">
      <div class="app-details-card">
        <h3><?php e(t('Template (HTML & CSS)')); ?></h3>
        <p class="app-muted"><?php e(t('Leave empty to use the built-in default template. Scripts and remote resources are not executed.')); ?></p>
        <div class="security-template-editor">
          <textarea name="template_html" rows="22" spellcheck="false" autocomplete="off" wrap="off"><?php e($templateHtml); ?></textarea>
        </div>

        <?php if ($variables !== []): ?>
          <div class="security-variable-reference">
            <h4><?php e(t('Available variables')); ?></h4>
            <p class="app-muted"><?php e(t('These placeholders are replaced when the report is generated:')); ?></p>
            <dl class="security-variable-list">
              <?php foreach ($variables as $token => $description): ?>
                <dt><code><?php e('{{' . $token . '}}'); ?></code></dt>
                <dd><?php e((string) $description); ?></dd>
              <?php endforeach; ?>
            </dl>
          </div>
        <?php endif; ?>
      </div>

      <?php \MintyPHP\Session::getCsrfInput(); ?>
    </form>

    <form method="post" id="security-report-logo-delete-form" hidden>
      <?php \MintyPHP\Session::getCsrfInput(); ?>
      <input type="hidden" name="action" value="delete_logo">
    </form>
  </section>
</div>
