<?php

use Duplicator\Models\GlobalEntity;
use Duplicator\Models\TemplateEntity;
use Duplicator\Package\Archive\PackageArchive;
use Duplicator\Views\UI\UiViewState;

defined("ABSPATH") or die("");

/**
 * Variables
 *
 * @var \Duplicator\Core\Controllers\ControllersManager $ctrlMng
 * @var \Duplicator\Core\Views\TplMng  $tplMng
 */
$isTemplateEdit = $tplMng->getDataValueBool('isTemplateEdit');
$template       = $tplMng->getDataValueObjRequired('template', TemplateEntity::class);

$archive_format = (GlobalEntity::getInstance()->getBuildMode() == PackageArchive::BUILD_MODE_DUP_ARCHIVE ? 'daf' : 'zip');

$setupParams = [
    'secureOn'   => $template->installer_opts_secure_on,
    'securePass' => $template->installerPassowrd,
];

/**
 * Filter the archive filter tabs.
 *
 * Each tab entry: ['label' => string, 'class' => string, 'callback' => callable]
 * The callback renders the tab content panel.
 *
 * @var array<string, array{label: string, class: string, callback: callable}> $filterTabs
 */
$filterTabs = apply_filters('duplicator_package_setup_filter_tabs', [
    'filters'  => [
        'label'    => __('Filters', 'duplicator-pro'),
        'class'    => 'filter-files-tab',
        'callback' => function () use ($tplMng): void {
            $tplMng->render('parts/packages/filters/section_filters_subtab_filters_files');
        },
    ],
    'security' => [
        'label'    => __('Security', 'duplicator-pro'),
        'class'    => 'archive-setup-tab',
        'callback' => function () use ($tplMng, $setupParams): void {
            $tplMng->render('parts/packages/filters/section_filters_subtab_setup', $setupParams);
        },
    ],
]);
?>
<div class="dup-box dup-archive-filters-wrapper">
    <div class="dup-box-title">
        <i class="far fa-file-archive fa-sm"></i> <?php esc_html_e('Backup', 'duplicator-pro') ?>
        <?php if (!$isTemplateEdit) { ?>
            <sup class="dup-box-title-badge">
                <?php echo esc_html($archive_format); ?>
            </sup>
        <?php } ?>
        &nbsp;&nbsp;
        <?php $tplMng->render('parts/packages/filters/section_filters_incons'); ?>
        <button class="dup-box-arrow">
            <span class="screen-reader-text">
                <?php esc_html_e('Toggle panel:', 'duplicator-pro') ?> <?php esc_html_e('Archive Settings', 'duplicator-pro') ?>
            </span>
        </button>
    </div>

    <div
        id="dup-pack-archive-panel"
        class="dup-box-panel <?php echo UiViewState::getValue('dup-pack-archive-panel') ? '' : 'no-display'; ?>">
        <div data-dupli-tabs="true">
            <ul>
                <?php foreach ($filterTabs as $tab) { ?>
                    <li class="<?php echo esc_attr($tab['class']); ?>">
                        <?php echo esc_html($tab['label']); ?>
                    </li>
                <?php } ?>
            </ul>

            <?php
            foreach ($filterTabs as $tab) {
                call_user_func($tab['callback']);
            }
            ?>
        </div>
    </div>
</div>

<div class="duplicator-error-container"></div>
