<?php

/**
 * @var bool $canUpdateSettings
 */

use MintyPHP\Session;

$canUpdateSettings = (bool) ($canUpdateSettings ?? false);
$hasLogo = app(\MintyPHP\Service\Branding\BrandingLogoService::class)->hasLogo();
$hasFavicon = app(\MintyPHP\Service\Branding\BrandingFaviconService::class)->hasFavicon();
?>

<div class="app-details-container">
  <section>
    <?php
    $titlebar = [
      'title' => t('Branding settings'),
      'backHref' => 'admin/settings',
      'backTitle' => t('Cancel'),
      'actions' => [],
    ];
    require templatePath('partials/app-details-titlebar.phtml');
    ?>

    <?php if ($canUpdateSettings): ?>
      <form id="branding-logo-form" method="post" action="admin/settings/logo"
        enctype="multipart/form-data" data-standard-detail-form="1" hidden>
        <?php Session::getCsrfInput(); ?>
      </form>
      <form id="branding-logo-delete-form" method="post" action="admin/settings/logo-delete" hidden>
        <?php Session::getCsrfInput(); ?>
      </form>
      <form id="branding-favicon-form" method="post" action="admin/settings/favicon"
        enctype="multipart/form-data" hidden>
        <?php Session::getCsrfInput(); ?>
      </form>
      <form id="branding-favicon-delete-form" method="post" action="admin/settings/favicon-delete" hidden>
        <?php Session::getCsrfInput(); ?>
      </form>
    <?php endif; ?>

    <div class="grid">
      <details class="app-details-card" name="settings-branding-logo" data-details-key="settings-branding-logo" open>
        <summary>
          <span class="app-details-card-summary-title"><?php e(t('App logo')); ?></span>
          <span class="app-details-card-summary-meta">
            <span class="badge" data-variant="neutral"><?php e($hasLogo ? t('Configured') : t('Default')); ?></span>
          </span>
        </summary>
        <div class="app-details-card-container">
          <?php
          $fileUpload = [
              'name' => 'logo',
              'accept' => 'image/svg+xml,image/png,image/jpeg,image/webp',
              'hint' => t('Allowed file types: SVG, PNG, JPG, WEBP'),
              'currentSrc' => $hasLogo ? appLogoUrl(256) : '',
              'currentLabel' => t('App logo'),
              'formId' => $canUpdateSettings ? 'branding-logo-form' : '',
              'deleteFormId' => $hasLogo && $canUpdateSettings ? 'branding-logo-delete-form' : '',
              'deleteConfirm' => t('Remove the app logo?'),
          ];
          require templatePath('partials/app-file-upload.phtml');
          ?>
          <?php if ($canUpdateSettings): ?>
            <div class="app-form-actions">
              <button type="submit" form="branding-logo-form" name="action" value="save" class="secondary outline">
                <?php e(t('Save')); ?>
              </button>
              <button type="submit" form="branding-logo-form" name="action" value="save_close" class="primary">
                <?php e(t('Save & close')); ?>
              </button>
            </div>
          <?php endif; ?>
        </div>
      </details>

      <details class="app-details-card" name="settings-branding-favicon" data-details-key="settings-branding-favicon" open>
        <summary>
          <span class="app-details-card-summary-title"><?php e(t('Favicon')); ?></span>
          <span class="app-details-card-summary-meta">
            <span class="badge" data-variant="neutral"><?php e($hasFavicon ? t('Configured') : t('Default')); ?></span>
          </span>
        </summary>
        <div class="app-details-card-container">
          <?php
          $fileUpload = [
              'name' => 'favicon',
              'accept' => 'image/png',
              'hint' => t('PNG, square recommended (icons are center-cropped)'),
              'currentSrc' => $hasFavicon ? asset('favicon/favicon-32x32.png') : '',
              'currentLabel' => t('Favicon'),
              'formId' => $canUpdateSettings ? 'branding-favicon-form' : '',
              'deleteFormId' => $hasFavicon && $canUpdateSettings ? 'branding-favicon-delete-form' : '',
              'deleteConfirm' => t('Remove the favicon?'),
          ];
          require templatePath('partials/app-file-upload.phtml');
          ?>
          <?php if ($canUpdateSettings): ?>
            <div class="app-form-actions">
              <button type="submit" form="branding-favicon-form" name="action" value="save" class="secondary outline">
                <?php e(t('Save')); ?>
              </button>
              <button type="submit" form="branding-favicon-form" name="action" value="save_close" class="primary">
                <?php e(t('Save & close')); ?>
              </button>
            </div>
          <?php endif; ?>
        </div>
      </details>
    </div>
  </section>
  <aside id="app-details-aside-section">
    <div class="app-details-aside-section"></div>
  </aside>
</div>
