<?php
$template = is_array($template ?? null) ? $template : [];
$schemaItems = is_array($schemaItems ?? null) ? $schemaItems : [];
$errors = is_array($errors ?? null) ? $errors : [];
$validationSummaryErrors = is_array($validationSummaryErrors ?? null) ? $validationSummaryErrors : [];
$closeTarget = $closeTarget ?? 'security/templates';
$isActive = (int) ($template['active'] ?? 1) === 1;
?>
<div class="app-details-container">
  <section data-tab-panel data-tab-panel-wide>
    <?php
    $titlebar = [
        'title' => trim((string) ($template['product_name'] ?? '')) !== '' ? (string) $template['product_name'] : t('Edit template'),
        'backHref' => lurl($closeTarget),
        'actions' => [
            ['label' => t('Save'), 'type' => 'submit', 'form' => 'security-template-form', 'name' => 'action', 'value' => 'save', 'class' => 'primary', 'tone' => 'success'],
            ['label' => t('Save & close'), 'type' => 'submit', 'form' => 'security-template-form', 'name' => 'action', 'value' => 'save_close', 'class' => 'secondary outline'],
        ],
    ];
    require templatePath('partials/app-details-titlebar.phtml');
    ?>

    <?php if ($validationSummaryErrors): ?>
      <?php require templatePath('partials/app-details-validation-summary.phtml'); ?>
    <?php endif; ?>

    <form method="post" id="security-template-form">
      <div class="app-details-card">
        <div class="security-field-row">
          <div class="security-field">
            <label><?php e(t('Product code')); ?></label>
            <input type="text" value="<?php e((string) ($template['product_code'] ?? '')); ?>" disabled>
            <small class="muted"><?php e(t('The product code cannot be changed after creation.')); ?></small>
          </div>
          <div class="security-field">
            <label for="security-template-name"><?php e(t('Product name')); ?> <span class="security-form-required">*</span></label>
            <input type="text" id="security-template-name" name="product_name" value="<?php e((string) ($template['product_name'] ?? '')); ?>" autocomplete="off">
            <?php if (!empty($errors['product_name'])): ?><p class="field-error"><?php e($errors['product_name']); ?></p><?php endif; ?>
          </div>
        </div>
        <label class="security-check-toggle security-active-toggle">
          <input type="checkbox" name="active" value="1"<?php if ($isActive): ?> checked<?php endif; ?>>
          <span><?php e(t('Active (selectable when creating a security check)')); ?></span>
        </label>
      </div>

      <div class="app-details-card">
        <h3><?php e(t('Technical checklist')); ?></h3>
        <p class="app-muted"><?php e(t('Define the product-specific technical checks. Add sections to group items. Items are frozen onto each security check at creation time.')); ?></p>
        <?php if (!empty($errors['schema'])): ?><p class="field-error"><?php e($errors['schema']); ?></p><?php endif; ?>

        <div id="security-schema-editor" data-app-component="security-template-schema-editor"
             data-input-name="tech_schema_json"
             data-label-section="<?php e(t('Section')); ?>"
             data-label-check="<?php e(t('Check item')); ?>"
             data-label-add-section="<?php e(t('Add section')); ?>"
             data-label-add-check="<?php e(t('Add check item')); ?>"
             data-label-remove="<?php e(t('Remove')); ?>"
             data-label-move-up="<?php e(t('Move up')); ?>"
             data-label-move-down="<?php e(t('Move down')); ?>"
             data-placeholder-section="<?php e(t('Section title')); ?>"
             data-placeholder-check="<?php e(t('Check item label')); ?>"
             data-placeholder-hint="<?php e(t('Hint (optional)')); ?>"
             data-placeholder-markdown="<?php e(t('Markdown guidance (optional) — shown in the checklist')); ?>"
             data-empty-text="<?php e(t('No checklist items yet. Add a check item to get started.')); ?>">
        </div>
        <input type="hidden" name="tech_schema_json" id="security-schema-json" value="">
        <script type="application/json" id="security-schema-initial"><?php gridJsonForJs(array_values($schemaItems)); ?></script>
      </div>

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