<?php
$form = is_array($form ?? null) ? $form : [];
$errors = is_array($errors ?? null) ? $errors : [];
$validationSummaryErrors = is_array($validationSummaryErrors ?? null) ? $validationSummaryErrors : [];
$templates = is_array($templates ?? null) ? $templates : [];
$assignableUsers = is_array($assignableUsers ?? null) ? $assignableUsers : [];
$closeTarget = $closeTarget ?? 'security/checks';
?>
<div class="app-wizard-content">
  <div class="app-wizard-header">
    <a href="<?php e(lurl($closeTarget)); ?>" class="app-wizard-back" data-tooltip="<?php e(t('Back to list')); ?>">
      <i class="bi bi-arrow-left"></i>
    </a>
    <h1 class="app-wizard-title"><?php e(t('New security check')); ?></h1>
  </div>

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

  <div class="app-wizard-card">
    <form method="post" id="security-check-create-form">
      <h2 class="app-wizard-card-heading"><?php e(t('Select customer, domain and product')); ?></h2>
      <p class="app-wizard-card-description"><?php e(t('Choose the customer, domain and software product this security check applies to.')); ?></p>

      <div class="app-wizard-field-group">
        <label><?php e(t('Customer')); ?> <span class="security-form-required">*</span></label>
        <div
          data-app-lookup
          data-lookup-url="<?php e(lurl('security/customer-search-data')); ?>"
          data-lookup-min-chars="2"
          data-lookup-debounce="300"
          data-lookup-value-key="value"
          data-lookup-display-key="label"
          data-lookup-placeholder="<?php e(t('Search by name or number...')); ?>"
          data-lookup-empty-text="<?php e(t('No customers found')); ?>"
          data-lookup-error-text="<?php e(t('Search failed')); ?>"
          data-lookup-initial-value="<?php e($form['debitor_no'] ?? ''); ?>"
          data-lookup-initial-display="<?php e($form['debitor_name'] ?? ''); ?>"
        >
          <input type="hidden" name="debitor_no" data-lookup-value value="<?php e($form['debitor_no'] ?? ''); ?>">
          <input type="hidden" name="debitor_name" data-lookup-display-value value="<?php e($form['debitor_name'] ?? ''); ?>">
        </div>
        <?php if (!empty($errors['debitor_no'])): ?>
          <p class="field-error"><?php e($errors['debitor_no']); ?></p>
        <?php endif; ?>
      </div>

      <div class="app-wizard-field-group" id="security-domain-group" data-app-component="security-customer-domain-select"
           data-domains-url="<?php e(lurl('security/customer-domains-data')); ?>"
           data-text-initial="<?php e(t('Select a customer first...')); ?>"
           data-text-loading="<?php e(t('Loading domains...')); ?>"
           data-text-select="<?php e(t('Please select...')); ?>"
           data-text-empty="<?php e(t('No domains found for this customer')); ?>"
           data-text-error="<?php e(t('Failed to load domains')); ?>"
           data-initial-debitor="<?php e($form['debitor_no'] ?? ''); ?>"
           data-initial-domain="<?php e($form['domain_no'] ?? ''); ?>"
      >
        <label for="security-domain"><?php e(t('Domain')); ?> <span class="security-form-required">*</span></label>
        <div class="app-wizard-domain-select-wrap">
          <select id="security-domain" disabled>
            <option value=""><?php e(t('Select a customer first...')); ?></option>
          </select>
        </div>
        <input type="hidden" name="domain_no" id="security-domain-no" value="<?php e($form['domain_no'] ?? ''); ?>">
        <input type="hidden" name="domain_url" id="security-domain-url" value="<?php e($form['domain_url'] ?? ''); ?>">
        <div id="security-domain-feedback" class="app-wizard-field-feedback" role="alert" hidden></div>

        <div id="security-domain-manual" hidden>
          <p class="app-wizard-field-hint"><?php e(t('No domains found for this customer. Enter domain name manually:')); ?></p>
          <input type="text" id="security-domain-manual-no" placeholder="<?php e(t('Domain name (e.g. example.de)')); ?>"
                 value="<?php e($form['domain_no'] ?? ''); ?>" autocomplete="off">
          <input type="text" id="security-domain-manual-url" placeholder="<?php e(t('URL (optional, e.g. https://example.de)')); ?>"
                 value="<?php e($form['domain_url'] ?? ''); ?>" autocomplete="off" style="margin-top:0.4rem;">
        </div>

        <?php if (!empty($errors['domain_no'])): ?>
          <p class="field-error"><?php e($errors['domain_no']); ?></p>
        <?php endif; ?>
      </div>

      <div class="app-wizard-field-group">
        <label for="security-product-code"><?php e(t('Software product')); ?> <span class="security-form-required">*</span></label>
        <?php if ($templates === []): ?>
          <div class="notice" data-variant="info" role="alert">
            <p><?php e(t('No check templates found. Create a check template first.')); ?></p>
          </div>
        <?php else: ?>
          <select id="security-product-code" name="product_code">
            <option value=""><?php e(t('Please select...')); ?></option>
            <?php foreach ($templates as $tpl):
                $code = (string) ($tpl['product_code'] ?? '');
                $name = trim((string) ($tpl['product_name'] ?? ''));
                $displayLabel = $name !== '' ? $name : $code;
            ?>
              <option value="<?php e($code); ?>"<?php if (($form['product_code'] ?? '') === $code): ?> selected<?php endif; ?>>
                <?php e($displayLabel); ?>
              </option>
            <?php endforeach; ?>
          </select>
          <?php if (!empty($errors['product_code'])): ?>
            <p class="field-error"><?php e($errors['product_code']); ?></p>
          <?php endif; ?>
        <?php endif; ?>
      </div>

      <div class="app-wizard-field-group">
        <label for="security-owner"><?php e(t('Owner')); ?></label>
        <select id="security-owner" name="owner_user_id">
          <?php
          $selectedOwner = (int) ($form['owner_user_id'] ?? 0);
          foreach ($assignableUsers as $assignableUser):
              $assignableUserId = (int) ($assignableUser['id'] ?? 0);
              if ($assignableUserId <= 0) {
                  continue;
              }
              $assignableUserName = trim((string) ($assignableUser['display_name'] ?? ''));
          ?>
            <option value="<?php e((string) $assignableUserId); ?>"<?php if ($assignableUserId === $selectedOwner): ?> selected<?php endif; ?>><?php e($assignableUserName !== '' ? $assignableUserName : ('#' . $assignableUserId)); ?></option>
          <?php endforeach; ?>
        </select>
      </div>

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

      <div class="app-wizard-actions">
        <a href="<?php e(lurl($closeTarget)); ?>" role="button" class="outline secondary"><?php e(t('Cancel')); ?></a>
        <button type="submit" class="primary"<?php if ($templates === []): ?> disabled<?php endif; ?>><?php e(t('Create security check')); ?></button>
      </div>
    </form>
  </div>
</div>
