<?php

use MintyPHP\Session;

/**
 * @var string $domainNo
 * @var array $domain
 * @var bool $hasError
 * @var string $errorMessage
 * @var string $domainUrl
 * @var string $customerNo
 * @var string $customerName
 * @var string $domainState
 * @var string $domainAdministration
 * @var string $pageTitle
 * @var string $securityLevel
 * @var string $securityLevelVariant
 * @var string $securityLevelNote
 */

$domainNo = $domainNo ?? '';
$domain = is_array($domain ?? null) ? $domain : [];
$hasError = $hasError ?? false;
$errorMessage = $errorMessage ?? '';
$domainUrl = $domainUrl ?? '';
$customerNo = $customerNo ?? '';
$customerName = $customerName ?? '';
$domainState = $domainState ?? '';
$domainAdministration = $domainAdministration ?? '';
$pageTitle = $pageTitle ?? $domainNo;
$securityLevel = $securityLevel ?? 'normal';
$securityLevelVariant = $securityLevelVariant ?? 'neutral';
$securityLevelNote = $securityLevelNote ?? '';

$stateVariantMap = [
    'Aktiv' => 'success',
    'In Vorbereitung' => 'warning',
];
$stateVariant = $stateVariantMap[$domainState] ?? 'neutral';

?>
<div class="app-details-container"
     data-app-component="helpdesk-domain-detail"
     data-domain-no="<?php e($domainNo); ?>"
     data-customer-no="<?php e($customerNo); ?>"
     data-customer-name="<?php e($customerName); ?>"
     data-detail-url="<?php e(lurl('helpdesk/domain-detail-data')); ?>"
     data-debitor-base-url="<?php e(lurl('helpdesk/debitor/')); ?>"
     data-handover-edit-base-url="<?php e(lurl('helpdesk/handovers/edit/')); ?>"
     data-domain-base-url="<?php e(lurl('helpdesk/domain/')); ?>"
     data-ticket-base-url="<?php e(lurl('helpdesk/ticket/')); ?>"
>
  <section>
    <?php
    $titlebar = [
        'title' => $pageTitle,
        'backHref' => lurl('helpdesk/domains'),
        'backTitle' => t('Back to domains'),
        'actions' => [],
    ];
    require templatePath('partials/app-details-titlebar.phtml');
    ?>

    <?php if ($hasError): ?>
      <div class="notice" data-variant="error" role="alert">
        <p><?php e(t('Could not connect to Business Central.')); ?></p>
        <?php if ($errorMessage !== ''): ?>
          <small><?php e($errorMessage); ?></small>
        <?php endif; ?>
      </div>
    <?php elseif ($domain !== []): ?>

      <div class="app-tabs" data-tabs data-app-component="tabs" data-tabs-param="tab" data-tabs-storage-key="helpdesk-domain-detail">
        <div class="app-tabs-nav">
          <button type="button" data-tab="overview" data-tab-default>
            <?php e(t('Overview')); ?>
          </button>
        </div>

        <!-- Overview panel -->
        <div data-tab-panel="overview" data-tab-panel-wide>

          <!-- Loading skeleton -->
          <div id="domain-loading" aria-busy="true">
            <div class="helpdesk-domain-overview-grid">
              <div class="helpdesk-skeleton-block" style="height: 180px;"></div>
              <div class="helpdesk-skeleton-block" style="height: 180px;"></div>
            </div>
            <div class="helpdesk-skeleton-block" style="height: 120px; margin-top: calc(var(--app-spacing) * 1.25);"></div>
          </div>

          <!-- Content (hidden until async load) -->
          <div id="domain-content" hidden>

            <!-- Customer + Contract side by side -->
            <div class="helpdesk-domain-overview-grid">

              <!-- Customer Info -->
              <details class="app-details-card" open>
                <summary>
                  <span class="app-details-card-summary-title"><?php e(t('Customer information')); ?></span>
                </summary>
                <div class="app-details-card-container">
                  <div id="domain-customer-info">
                    <p class="text-muted"><?php e(t('Loading...')); ?></p>
                  </div>
                </div>
              </details>

            <!-- Contract Info -->
            <details class="app-details-card" id="domain-contract-section" open hidden>
              <summary>
                <span class="app-details-card-summary-title"><?php e(t('Contract information')); ?></span>
              </summary>
              <div class="app-details-card-container">
                <div id="domain-contract-info">
                  <p class="text-muted"><?php e(t('Loading...')); ?></p>
                </div>
              </div>
            </details>

            <!-- Security Level -->
            <details class="app-details-card" open>
              <summary>
                <span class="app-details-card-summary-title"><?php e(t('Security level')); ?></span>
              </summary>
              <div class="app-details-card-container">
                <div id="domain-security-level-form">
                  <form method="POST" action="<?php e(lurl('helpdesk/domains/security-level-data')); ?>" data-security-level-form>
                    <input type="hidden" name="domain_no" value="<?php e($domainNo); ?>">
                    <?php Session::getCsrfInput(); ?>
                    <div class="app-form-grid">
                      <div class="app-form-group">
                        <label for="domain-security-level-select"><?php e(t('Security level')); ?></label>
                        <select name="security_level" id="domain-security-level-select" data-security-level-select>
                          <option value="niedrig" <?php e($securityLevel === 'niedrig' ? 'selected' : ''); ?>><?php e(t('Low')); ?></option>
                          <option value="normal" <?php e($securityLevel === 'normal' ? 'selected' : ''); ?>><?php e(t('Normal')); ?></option>
                          <option value="hoch" <?php e($securityLevel === 'hoch' ? 'selected' : ''); ?>><?php e(t('High')); ?></option>
                          <option value="kritisch" <?php e($securityLevel === 'kritisch' ? 'selected' : ''); ?>><?php e(t('Critical')); ?></option>
                        </select>
                      </div>
                      <div class="app-form-group">
                        <label for="domain-security-level-note"><?php e(t('Note')); ?></label>
                        <textarea name="note" id="domain-security-level-note" rows="3" placeholder="<?php e(t('Optional note...')); ?>"><?php e($securityLevelNote); ?></textarea>
                      </div>
                    </div>
                    <div class="app-form-actions">
                      <button type="submit" class="secondary outline small"><?php e(t('Save')); ?></button>
                    </div>
                  </form>
                </div>
              </div>
            </details>

            </div>

            <!-- Handovers (full width) -->
            <details class="app-details-card" open >
              <summary>
                <span class="app-details-card-summary-title"><?php e(t('Handovers')); ?></span>
                <span class="app-details-card-summary-meta">
                  <span class="badge" data-variant="neutral" id="domain-handover-count">0</span>
                </span>
              </summary>
              <div class="app-details-card-container">
                <div id="domain-handovers-list"></div>
              </div>
            </details>

            <!-- Updates (full width) -->
            <details class="app-details-card" id="domain-updates-section" open hidden>
              <summary>
                <span class="app-details-card-summary-title"><?php e(t('Updates')); ?></span>
                <span class="app-details-card-summary-meta">
                  <span class="badge" data-variant="neutral" id="domain-update-count">0</span>
                </span>
              </summary>
              <div class="app-details-card-container">
                <div id="domain-updates-list"></div>
              </div>
            </details>

          </div>
        </div>
      </div>

    <?php endif; ?>
  </section>

  <?php if (!$hasError && $domain !== []): ?>
  <aside id="app-details-aside-section">
    <div class="app-details-aside-section">

      <hgroup>
        <h2><?php e($domainNo); ?></h2>
        <?php if ($domainUrl !== ''): ?>
          <p><?php e($domainUrl); ?></p>
        <?php endif; ?>
      </hgroup>

      <div class="badge-list">
        <?php if ($domainState !== ''): ?>
          <span class="badge" data-variant="<?php e($stateVariant); ?>"><?php e($domainState); ?></span>
        <?php endif; ?>
        <?php if ($domainAdministration !== ''): ?>
          <span class="badge" data-variant="neutral"><?php e($domainAdministration); ?></span>
        <?php endif; ?>
        <span class="badge" data-variant="<?php e($securityLevelVariant); ?>" id="domain-security-badge"><?php e(t(ucfirst($securityLevel))); ?></span>
      </div>

      <hr>

      <?php if ($domainUrl !== ''): ?>
        <a href="<?php e('https://' . ltrim($domainUrl, 'htps:/')); ?>" target="_blank" rel="noopener" class="secondary outline small" role="button">
          <i class="bi bi-box-arrow-up-right" aria-hidden="true"></i>
          <?php e(t('Open domain')); ?>
        </a>
      <?php endif; ?>

      <div id="domain-related-section" hidden>
        <hr>
        <details name="domain-aside" open>
          <summary><?php e(t('Related domains')); ?></summary>
          <div id="domain-related-domains"></div>
        </details>
      </div>

    </div>
  </aside>
  <?php endif; ?>
</div>

<?php if (!$hasError && $domain !== []): ?>
<script type="application/json" id="helpdesk-domain-i18n"><?php gridJsonForJs([
    'Loading...' => t('Loading...'),
    'Network error — please try again' => t('Network error — please try again'),
    'No handovers for this domain yet' => t('No handovers for this domain yet'),
    'No contract information available' => t('No contract information available'),
    'Create handover' => t('Create handover'),
    'Product' => t('Product'),
    'Status' => t('Status'),
    'Created by' => t('Created by'),
    'Created' => t('Created'),
    'Name' => t('Name'),
    'Customer No.' => t('Customer No.'),
    'Address' => t('Address'),
    'City' => t('City'),
    'Postal code' => t('Postal code'),
    'Phone' => t('Phone'),
    'E-Mail' => t('E-Mail'),
    'Salesperson' => t('Salesperson'),
    'Support type' => t('Support type'),
    'Contract type' => t('Contract type'),
    'Contract No.' => t('Contract No.'),
    'Description' => t('Description'),
    'Contract state' => t('Contract state'),
    'Line state' => t('Line state'),
    'URL' => t('URL'),
    'No.' => t('No.'),
    'State' => t('State'),
    'Total' => t('Total'),
    'Draft' => t('Draft'),
    'In progress' => t('In progress'),
    'Completed' => t('Completed'),
    'Archived' => t('Archived'),
    'No updates for this domain yet' => t('No updates for this domain yet'),
    'Ticket' => t('Ticket'),
    'Type' => t('Type'),
    'Gitea link' => t('Gitea link'),
    'Update' => t('Update'),
    'Hotfix' => t('Hotfix'),
]); ?></script>

<?php endif; ?>
