<?php



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

use Duplicator\Controllers\PackagesPageController;
use Duplicator\Models\TemplateEntity;
use Duplicator\Views\UI\UiDialog;

/**
 * Variables
 *
 * @var \Duplicator\Core\Controllers\ControllersManager $ctrlMng
 * @var \Duplicator\Core\Views\TplMng  $tplMng
 */

$requirements = $tplMng->getDataValueArrayRequired('requirements');
$blur         = $tplMng->getDataValueBool('blur');

$manual_template = TemplateEntity::getManualTemplate();

$tplMng->render('admin_pages/packages/setup/section-requirements');
$form_action_url = PackagesPageController::getInstance()->getPackageBuildS2Url();
?>
<form
    id="dup-form-opts"
    class="<?php echo ($blur ? 'dup-mock-blur' : ''); ?>"
    method="post"
    action="<?php echo esc_attr($form_action_url); ?>"
    data-parsley-validate data-parsley-ui-enabled="true">
    <?php $tplMng->getAction(PackagesPageController::ACTION_UPDATE_TEMPLATE)->getActionNonceFileds(); ?>
    <div class="dupli-general-area">
        <?php
        $tplMng->render(
            'admin_pages/packages/setup/name-format-controls',
            [
                'nameFormat' => $manual_template->package_name_format,
                'notes'      => $manual_template->notes,
            ]
        );
        ?>
        <?php do_action('duplicator_package_general_area_after', $manual_template); ?>
    </div>

    <?php
    $tplMng->render('admin_pages/packages/setup/section_storages');
    $tplMng->render(
        'parts/packages/filters/section_filters',
        [
            'isTemplateEdit' => false,
            'template'       => $manual_template,
        ]
    );
    $tplMng->render(
        'parts/packages/filters/section_installer',
        ['template' => $manual_template]
    );
    ?>

    <div class="dup-button-footer">
        <input
            type="button"
            value="<?php esc_attr_e("Reset", 'duplicator-pro') ?>"
            class="button hollow secondary small" <?php echo ($requirements['Success']) ? '' : 'disabled="disabled"'; ?>
            onClick="DupliJs.Pack.ResetSettings()">&nbsp;
        <input
            id="button-next"
            type="submit"
            value="<?php esc_attr_e("Next", 'duplicator-pro') ?> &#9654;"
            class="button primary small" <?php echo ($requirements['Success']) ? '' : 'disabled="disabled"'; ?>>
    </div>
</form>

<!-- CACHE PROTECTION: If the back-button is used from the scanner page then we need to
refresh page in-case any filters where set while on the scanner page -->
<form id="cache_detection">
    <input type="hidden" id="cache_state" name="cache_state" value="" />
</form>
<?php
$confirm1               = new UiDialog();
$confirm1->title        = __('Would you like to continue', 'duplicator-pro');
$confirm1->message      = __('This will clear all of the current backup settings.', 'duplicator-pro');
$confirm1->progressText = __('Please Wait...', 'duplicator-pro');
$confirm1->jsCallback   = 'DupliJs.Pack.ResetSettingsRun()';
$confirm1->initConfirm();
?>
<script>
    jQuery(function($) {
        DupliJs.Pack.BeforeSubmit = function(e) {
            $('#mu-exclude option').each(function() {
                $(this).prop('selected', true);
            });

            DupliJs.Pack.FillExcludeTablesList();

            return true;
        };

        $('#dup-form-opts').submit(function() {
            return DupliJs.Pack.BeforeSubmit();
        })

        DupliJs.Pack.ResetSettings = function() {
            <?php $confirm1->showConfirm(); ?>
        };

        DupliJs.Pack.ResetSettingsRun = function() {
            $('#dup-form-opts')[0].reset();
            setTimeout(function() {
                tb_remove();
            }, 800);
        }
    });

    //INIT
    jQuery(document).ready(function($) {
        DupliJs.Pack.checkPageCache = function() {
            var $state = $('#cache_state');
            if ($state.val() == "") {
                $state.val("fresh-load");
            } else {
                $state.val("cached");
                <?php $redirect = PackagesPageController::getInstance()->getPackageBuildS1Url(); ?>
                window.location.href = '<?php echo esc_js($redirect); ?>';
            }
        }

        // Default EnableTemplate — may be overridden by addons
        if (typeof DupliJs.Pack.EnableTemplate !== 'function') {
            DupliJs.Pack.EnableTemplate = function() {
                DupliJs.EnableInstallerPassword();
                DupliJs.Pack.ToggleFileFilters();
                DupliJs.Pack.ToggleDBFilters();
                DupliJs.Pack.ToggleActiveThemes();
                DupliJs.Pack.ToggleActivePlugins();
                DupliJs.Pack.ToggleDBExcluded();
                DupliJs.Pack.ToggleNoPrefixTables(false);
                DupliJs.Pack.ToggleNoSubsiteExistsTables(false);
            }
        }

        DupliJs.Pack.checkPageCache();
        DupliJs.Pack.EnableTemplate();
    });
</script>