<?php

declare(strict_types=1);

use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Encodings\ANSEL;
use Fisharebest\Webtrees\Encodings\ASCII;
use Fisharebest\Webtrees\Encodings\UTF16BE;
use Fisharebest\Webtrees\Encodings\UTF8;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\View;

/**
 * @var array<string> $download_filenames
 * @var Tree          $tree
 * @var bool          $zip_available
 */

?>

<div class="row mb-3">
    <label class="col-sm-3" for="format">
        <?= I18N::translate('Format') ?>
    </label>

    <div class="col-sm-9">
        <div class="form-check">
            <input class="form-check-input" type="radio" name="format" id="format-gedcom" value="gedcom" checked="checked" data-wt-extension=".ged">

            <label class="form-check-label" for="format-gedcom">
                <?= I18N::translate('GEDCOM') ?>
            </label>
        </div>

        <div class="form-check">
            <input class="form-check-input" type="radio" name="format" id="format-zip" value="zip" data-wt-extension=".zip" <?= $zip_available ? '' : 'disabled="disabled"'?>>

            <label class="form-check-label" for="format-zip">
                <?= /* I18N: ZIP = file format */ I18N::translate('ZIP') ?>
            </label>
        </div>

        <div class="form-check">
            <input class="form-check-input" type="radio" name="format" id="format-zipmedia" value="zipmedia" data-wt-extension=".zip" <?= $zip_available ? '' : 'disabled="disabled"'?>>

            <label class="form-check-label" for="format-zipmedia">
                <?= /* I18N: ZIP = file format */ I18N::translate('ZIP') ?>
                <?= I18N::translate('(includes media files)') ?>
            </label>
        </div>

        <div class="form-check">
            <input class="form-check-input" type="radio" name="format" id="format-gdz" value="gedzip" data-wt-extension=".gdz" <?= $zip_available ? '' : 'disabled="disabled"'?>>

            <label class="form-check-label" for="format-gdz">
                <?= /* I18N: GEDZIP = file format */ I18N::translate('GEDZIP') ?>
                <?= I18N::translate('(includes media files)') ?>
            </label>
        </div>
    </div>
</div>

<div class="row mb-3">
    <label class="col-sm-3" for="filename">
        <?= I18N::translate('Filename') ?>
    </label>

    <div class="col-sm-9">
        <div class="input-group">
            <?= view('components/select', ['name' => 'filename', 'id' => 'filename', 'selected' => 0, 'options' => $download_filenames]) ?>
            <span class="input-group-text" id="extension">.ged</span>
        </div>
    </div>
</div>

<div class="row mb-3">
    <label class="col-sm-3" for="encoding">
        <?= I18N::translate('Character encoding') ?>
    </label>

    <div class="col-sm-9">
        <?= view('components/select', ['name' => 'encoding', 'id' => 'encoding', 'selected' => UTF8::NAME, 'options' => [UTF8::NAME => 'UTF-8', UTF16BE::NAME => 'UNICODE (UTF16-BE)', ANSEL::NAME => 'ANSEL', ASCII::NAME => 'ASCII', 'CP1252' => 'ANSI (CP1252)']]) ?>
    </div>
</div>

<div class="row mb-3">
    <label class="col-sm-3" for="line_endings">
        <?= I18N::translate('Line endings') ?>
    </label>

    <div class="col-sm-9">
        <?= view('components/radios-inline', ['name' => 'line_endings', 'options' => ['CRLF' => 'CR/LF (windows)', 'LF' => 'LF (unix)'], 'selected' => 'CRLF']) ?>
    </div>
</div>

<div class="row mb-3">
    <label class="col-sm-3" for="privacy">
        <?= I18N::translate('Apply privacy settings') ?>
    </label>

    <div class="col-sm-9">
        <?php if (Auth::isAdmin()) : ?>
            <?= view('components/radios-inline', ['name' => 'privacy', 'options' => ['none' => I18N::translate('None'), 'gedadmin' => I18N::translate('Manager'), 'user' => I18N::translate('Member'), 'visitor' => I18N::translate('Visitor')], 'selected' => 'none']) ?>
        <?php elseif (Auth::isManager($tree)) : ?>
            <?= view('components/radios-inline', ['name' => 'privacy', 'options' => ['gedadmin' => I18N::translate('Manager'), 'user' => I18N::translate('Member'), 'visitor' => I18N::translate('Visitor')], 'selected' => 'gedadmin']) ?>
        <?php elseif (Auth::isMember($tree)) : ?>
            <?= view('components/radios-inline', ['name' => 'privacy', 'options' => ['user' => I18N::translate('Member'), 'visitor' => I18N::translate('Visitor')], 'selected' => 'user']) ?>
        <?php else : ?>
            <input type="hidden" name="privacy" value="visitor">
        <?php endif ?>
    </div>
</div>

<?php View::push('javascript') ?>
<script>
  'use strict';

  document.querySelectorAll('[name=format]').forEach(element => element.addEventListener('change', event => document.getElementById('extension').innerText = event.target.dataset.wtExtension));

  // Firefox will reload the page and change the selected item.
  document.getElementById('extension').innerText = document.querySelector('[name=format]:checked').dataset.wtExtension;

</script>
<?php View::endpush(); ?>
