<?php

/**
 * @var array|null $page
 * @var bool $canEdit
 * @var bool $editMode
 * @var string $contentJson
 * @var string $slug
 */

?>
<div class="app-details-container app-page">
  <section>
    <?php if (!$page): ?>
      <p><?php e(t('Page not found')); ?></p>
    <?php else: ?>
      <form method="post" action="<?php e($formAction ?? ''); ?>" class="page-editor" id="page-editor" data-page-editor
        data-can-edit="<?php e($canEdit ? '1' : '0'); ?>" data-edit-mode="<?php e($editMode ? 'edit' : 'view'); ?>"
        data-placeholder="<?php e(t('Start writing...')); ?>" data-header-placeholder="<?php e(t('Heading')); ?>">
        <?php MintyPHP\Session::getCsrfInput(); ?>
        <input type="hidden" name="return" value="<?php e($returnPath ?? ''); ?>">
        <textarea name="content" id="page-content" hidden><?php e($contentJson); ?></textarea>
        <div data-editor-holder></div>
      </form>
      <?php if (file_exists('web/vendor/editorjs/tools/header.umd.js')): ?>
        <script src="<?php e(assetVersion('vendor/editorjs/tools/header.umd.js')); ?>"></script>
      <?php endif; ?>
      <?php if (file_exists('web/vendor/editorjs/tools/list.umd.js')): ?>
        <script src="<?php e(assetVersion('vendor/editorjs/tools/list.umd.js')); ?>"></script>
      <?php endif; ?>
      <?php if (file_exists('web/vendor/editorjs/tools/checklist.umd.js')): ?>
        <script src="<?php e(assetVersion('vendor/editorjs/tools/checklist.umd.js')); ?>"></script>
      <?php endif; ?>
      <?php if (file_exists('web/vendor/editorjs/tools/table.umd.js')): ?>
        <script src="<?php e(assetVersion('vendor/editorjs/tools/table.umd.js')); ?>"></script>
      <?php endif; ?>
      <?php if (file_exists('web/vendor/editorjs/tools/columns.js')): ?>
        <script src="<?php e(assetVersion('vendor/editorjs/tools/columns.js')); ?>"></script>
      <?php endif; ?>
      <?php if (file_exists('web/vendor/editorjs/tools/marker.umd.js')): ?>
        <script src="<?php e(assetVersion('vendor/editorjs/tools/marker.umd.js')); ?>"></script>
      <?php endif; ?>
      <script type="module" src="<?php e(assetVersion('js/components/app-page-editor.js')); ?>"></script>
    <?php endif; ?>
  </section>
  <?php if ($canEdit): ?>
    <aside id="app-details-aside-section">
      <div class="app-details-aside-section app-page-action-card">

        <div class="grid">
          <button type="button" class="secondary outline" data-tooltip-pos="top"
            data-tooltip="<?php e($editMode ? t('View') : t('Edit')); ?>" data-editor-toggle
            data-edit-label="<?php e(t('Edit')); ?>" data-view-label="<?php e(t('View')); ?>">
            <?php if ($editMode): ?>
              <i class="bi bi-eye-fill" data-editor-icon aria-hidden="true"></i>
            <?php else: ?>
              <i class="bi bi-pencil-square" data-editor-icon aria-hidden="true"></i>
            <?php endif; ?>
          </button>
          <button type="submit" form="page-editor" class="primary" data-editor-save>
            <?php e(t('Save')); ?>
          </button>
        </div>

        <?php if (isset($locales, $currentLocale) && is_array($locales) && count($locales) > 1): ?>
          <hr>
          <details>
            <summary><?php e(t('Copy')); ?></summary>
            <hr>
            <form method="post" action="page/copy-language" class="app-page-copy">
              <?php MintyPHP\Session::getCsrfInput(); ?>
              <input type="hidden" name="slug" value="<?php e($slug ?? ''); ?>">
              <input type="hidden" name="from_locale" value="<?php e($currentLocale); ?>">
              <input type="hidden" name="return" value="<?php e($returnPath ?? ''); ?>">
              <label class="app-field">
                <span>
                  <?php e(t('Copy content to')); ?>
                </span>
                <div role="group">
                  <select name="to_locale" required>
                    <?php foreach ($locales as $locale): ?>
                      <?php if ($locale === $currentLocale) {
                        continue;
                      } ?>
                      <option value="<?php e($locale); ?>">
                        <?php
                        $label = strtoupper($locale);
                        if ($locale === 'de') {
                          $label = t('German');
                        } elseif ($locale === 'en') {
                          $label = t('English');
                        }
                        e($label);
                        ?>
                      </option>
                    <?php endforeach; ?>
                  </select>
                  <button type="submit" class="secondary outline">
                    <?php e(t('Copy')); ?>
                  </button>
                </div>
              </label>
            </form>
            <small>
              <?php e(t('Existing content in the target language will be overwritten')); ?>
            </small>
          </details>
          <hr>
        <?php endif; ?>
      </div>
    </aside>
  <?php endif; ?>
</div>
