<?php

/**
 * @var array<int, string> $errors
 * @var array $form
 * @var array $department
 */

use MintyPHP\Session;

$values = $form ?? $department ?? [];
$pageAuth = is_array($viewAuth['page'] ?? null) ? $viewAuth['page'] : [];
$canUpdateDepartment = (bool) ($pageAuth['can_update_department'] ?? false);
$canDeleteDepartment = (bool) ($pageAuth['can_delete_department'] ?? false);
$isReadOnly = !$canUpdateDepartment;

?>

<div class="app-details-container">
  <section>
    <?php
    $hasTenantOptions = !empty($tenants);
    $breadcrumbs = [
      ['label' => t('Home'), 'path' => 'admin'],
      ['label' => t('Departments'), 'path' => 'admin/departments'],
      ['label' => t('Edit department')],
    ];
    require templatePath('partials/app-breadcrumb.phtml');
    $titlebar = [
      'title' => t('Edit department'),
      'backHref' => 'admin/departments',
      'backTitle' => t('Cancel'),
      'actions' => $canUpdateDepartment ? [
        [
          'form' => 'department-form',
          'name' => 'action',
          'value' => 'save',
          'class' => 'secondary outline',
          'label' => t('Save'),
          'disabled' => !$hasTenantOptions,
        ],
        [
          'form' => 'department-form',
          'name' => 'action',
          'value' => 'save_close',
          'class' => 'primary',
          'label' => t('Save & close'),
          'disabled' => !$hasTenantOptions,
        ],
      ] : [],
    ];
    require templatePath('partials/app-details-titlebar.phtml');
    ?>
    <?php
    $validationSummaryErrors = $validationSummaryErrors ?? ($errors ?? []);
    require templatePath('partials/app-details-validation-summary.phtml');
    ?>
    <?php if (!empty($warnings)): ?>
      <div class="app-details-warnings">
        <div class="notice" data-variant="info">
          <ul>
            <?php foreach ($warnings as $warning): ?>
              <li><?php e($warning); ?></li>
            <?php endforeach; ?>
          </ul>
        </div>
      </div>
    <?php endif; ?>

    <?php
    $detailsOpenAll = false;
    $isReadOnly = $isReadOnly ?? false;
    $showDangerZone = $canDeleteDepartment;
    $dangerZoneDeleteFormId = $showDangerZone ? 'department-delete-form' : '';
    $dangerZoneWarning = t('This will permanently delete this department.');
    $dangerZoneActionLabel = t('Delete department');
    require __DIR__ . '/_form.phtml';
    ?>
    <?php if ($showDangerZone): ?>
      <form
        id="department-delete-form"
        method="post"
        action="admin/departments/delete/<?php e($values['uuid'] ?? ''); ?>"
        hidden
        data-detail-confirm-message="<?php e(t('Delete this department?')); ?>"
        data-detail-action-kind="delete"
      >
        <?php Session::getCsrfInput(); ?>
      </form>
    <?php endif; ?>
  </section>
  <aside id="app-details-aside-section">
    <div class="app-details-aside-section">
      <hgroup>
        <h2><?php e($values['description'] ?? ''); ?></h2>
        <p><?php e(t('Department')); ?></p>
      </hgroup>
      <div class="badge-list">
        <?php if ((string) ($values['active'] ?? '1') === '0'): ?>
          <span class="badge" data-variant="danger"><?php e(t('Inactive')); ?></span>
        <?php else: ?>
          <span class="badge" data-variant="success"><?php e(t('Active')); ?></span>
        <?php endif; ?>
      </div>
      <?php if (!empty($values['code']) || !empty($values['cost_center'])): ?>
        <hr>
        <?php if (!empty($values['code'])): ?>
          <p><small><?php e(t('Code')); ?></small><br><?php e($values['code']); ?></p>
        <?php endif; ?>
        <?php if (!empty($values['cost_center'])): ?>
          <p><small><?php e(t('Cost center')); ?></small><br><?php e($values['cost_center']); ?></p>
        <?php endif; ?>
      <?php endif; ?>
      <?php
      $asideAuditDetailsName = 'department-audit';
      $asideAuditCreated = (string) ($values['created'] ?? '');
      $asideAuditCreatedByLabel = (string) ($values['created_by_label'] ?? '');
      $asideAuditCreatedById = $values['created_by'] ?? null;
      $asideAuditCreatedByUuid = (string) ($values['created_by_uuid'] ?? '');
      $asideAuditModified = (string) ($values['modified'] ?? '');
      $asideAuditModifiedByLabel = (string) ($values['modified_by_label'] ?? '');
      $asideAuditModifiedById = $values['modified_by'] ?? null;
      $asideAuditModifiedByUuid = (string) ($values['modified_by_uuid'] ?? '');

      $asideIdsDetailsName = 'department-ids';
      $asideIdValue = $values['id'] ?? '';
      $asideUuidValue = (string) ($values['uuid'] ?? '');
      ?>
      <hr>
      <?php require templatePath('partials/app-details-aside-audit.phtml'); ?>
      <hr>
      <?php require templatePath('partials/app-details-aside-ids.phtml'); ?>
      <hr>
    </div>
  </aside>
</div>
