<?php

use MintyPHP\Module\Helpdesk\Service\HandoverService;

$handover = is_array($handover ?? null) ? $handover : [];
$tenantUsers = is_array($tenantUsers ?? null) ? $tenantUsers : [];
$validationSummaryErrors = is_array($validationSummaryErrors ?? null) ? $validationSummaryErrors : [];
$errors = is_array($errors ?? null) ? $errors : [];
$editTarget = $editTarget ?? 'helpdesk/handovers';

$formId = 'handover-assign-form';
$currentAssignedTo = (int) ($handover['assigned_to'] ?? 0);
$currentDueDate = (string) ($handover['due_date'] ?? '');
$hasAssignee = $currentAssignedTo > 0;
$currentStatus = (string) ($handover['status'] ?? '');
?>
<div class="app-details-container">
  <section>
    <?php
    $titlebar = [
        'title' => $titleText ?? t('Assign handover'),
        'backHref' => $editTarget,
        'backTitle' => t('Back'),
        'actions' => [
            [
                'form' => $formId,
                'name' => 'action',
                'value' => 'assign',
                'class' => 'primary',
                'label' => t('Assign'),
            ],
        ],
    ];
    require templatePath('partials/app-details-titlebar.phtml');
    ?>

    <?php require templatePath('partials/app-details-validation-summary.phtml'); ?>

    <form id="<?php e($formId); ?>" method="post">
      <div class="app-field-group">
        <label for="assigned_to">
          <?php e(t('Assign to')); ?>
          <span aria-hidden="true" class="required-indicator">*</span>
        </label>
        <select
          id="assigned_to"
          name="assigned_to"
          <?php if (!empty($errors['assigned_to'])): ?>aria-describedby="assigned_to_error"<?php endif; ?>
        >
          <option value=""><?php e(t('— Select employee —')); ?></option>
          <?php foreach ($tenantUsers as $u):
              $uId = (int) ($u['u']['id'] ?? $u['id'] ?? 0);
              $uName = (string) ($u['u']['display_name'] ?? $u['display_name'] ?? '');
              $uEmail = (string) ($u['u']['email'] ?? $u['email'] ?? '');
          ?>
            <option value="<?php e($uId); ?>"<?php if ($uId === $currentAssignedTo): ?> selected<?php endif; ?>>
              <?php e($uName ?: $uEmail); ?>
            </option>
          <?php endforeach; ?>
        </select>
        <?php if (!empty($errors['assigned_to'])): ?>
          <p id="assigned_to_error" class="field-error"><?php e($errors['assigned_to']); ?></p>
        <?php endif; ?>
      </div>

      <div class="app-field-group">
        <label for="due_date"><?php e(t('Due date (optional)')); ?></label>
        <input
          type="date"
          id="due_date"
          name="due_date"
          value="<?php e($currentDueDate); ?>"
        >
      </div>

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

  <aside id="app-details-aside-section">
    <div class="app-details-aside-section">
      <hgroup>
        <h2><?php e((string) ($handover['debitor_name'] ?? '—')); ?></h2>
        <p><?php e((string) ($handover['domain_url'] ?? '—')); ?></p>
        <p><?php e($productName ?? ''); ?></p>
      </hgroup>

      <div class="badge-list">
        <span class="badge" data-variant="<?php e(HandoverService::statusVariant($currentStatus)); ?>">
          <?php e(HandoverService::statusLabel($currentStatus)); ?>
        </span>
      </div>

      <?php if ($hasAssignee): ?>
        <hr>
        <p><small><?php e(t('Currently assigned to')); ?></small></p>
        <p><strong><?php e((string) ($handover['assigned_to_label'] ?? '#' . $currentAssignedTo)); ?></strong></p>
        <?php if ($currentDueDate !== ''): ?>
          <p><small><?php e(t('Due date')); ?>: <?php e($currentDueDate); ?></small></p>
        <?php endif; ?>

        <form method="post" style="margin-top: 8px;">
          <input type="hidden" name="action" value="resend">
          <?php \MintyPHP\Session::getCsrfInput(); ?>
          <button type="submit" class="secondary outline small">
            <i class="bi bi-envelope-arrow-up"></i> <?php e(t('Resend notification')); ?>
          </button>
        </form>
      <?php endif; ?>
    </div>
  </aside>
</div>
