<?php

/**
 * This view is used to list all staging sites, when create a new staging site button
 * @see \WPStaging\Staging\Ajax\Listing::ajaxListing
 *
 * @var array  $stagingSites
 * @var string $iconPath
 * @var bool   $error - true when staging site option is corrupted
 * @var        $license
 */

use WPStaging\Core\WPStaging;
use WPStaging\Framework\Assets\Assets;
use WPStaging\Framework\Facades\Hooks;
use WPStaging\Framework\Notices\CliIntegrationNotice;
use WPStaging\Framework\TemplateEngine\TemplateEngine;

$isPro = WPStaging::isPro();
/** @var CliIntegrationNotice $cliNotice */
$cliNotice = WPStaging::make(CliIntegrationNotice::class);
include WPSTG_VIEWS_DIR . 'job/modal/success.php';
include WPSTG_VIEWS_DIR . 'job/modal/process.php';

// Will show a locked message if the process is locked
require WPSTG_VIEWS_DIR . 'job/locked.php';
$assets = WPStaging::make(Assets::class);
$newStagingFeatureEnabled = (defined('WPSTG_NEW_STAGING') && WPSTG_NEW_STAGING);
?>
<div id="wpstg-step-1">
    <div class="wpstg-staging-actions">
        <?php if ($newStagingFeatureEnabled) : ?>
            <button
                id="wpstg-new-staging"
                class="wpstg-btn wpstg-btn-md wpstg-btn-primary wpstg-px-3"
                <?php echo $error ? 'disabled' : '' ?>
            >
                <svg class="wpstg-btn-icon-sm" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6"></path>
                </svg>
                <?php echo esc_html__("Create Staging Site", "wp-staging") ?>
            </button>
        <?php else : ?>
            <button
                id="wpstg-new-clone"
                class="wpstg-next-step-link wpstg-btn wpstg-btn-md wpstg-btn-primary wpstg-px-3"
                data-action="wpstg_scanning"
                <?php echo $error ? 'disabled' : ''; ?>
            >
                <svg class="wpstg-btn-icon-sm" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6"></path>
                </svg>
                <?php echo esc_html__("Create Staging Site", "wp-staging"); ?>
            </button>
        <?php endif; ?>

        <!-- CLI Dock Slot - populated after banner collapse or server-side when banner was dismissed -->
        <div id="wpstg-cli-dock-slot" class="wpstg-cli-dock-slot"><?php $cliNotice->maybeRenderDockCta(); ?></div>
    </div>
</div>

<?php if (WPStaging::isOnWordPressPlayground()) : ?>
<span class="wpstg-error-msg" id="wpstg-clone-id-error">
    <?php
    esc_html_e("A staging site may not work properly due to limitations in WordPress Playground. However, creating one can still be valuable for demonstrating the functionality.", "wp-staging");
    ?>
</span>
<?php endif; ?>

<?php if (!empty($stagingSites)) : ?>
    <!-- Existing Clones -->
    <div id="wpstg-existing-clones">
        <h3>
            <?php esc_html_e("Your Staging Sites:", "wp-staging"); ?>
        </h3>
        <?php foreach ($stagingSites as $stagingSite) :
            $stagingSiteItem = $stagingSite->toListableItem();
            include WPSTG_VIEWS_DIR . 'staging/staging-site-list-item.php';
        endforeach ?>
        <div class="wpstg-fs-14" id="info-block-how-to-push">
            <?php esc_html_e("How to:", "wp-staging"); ?> <a href="https://wp-staging.com/docs/copy-staging-site-to-live-site/" target="_blank"><?php esc_html_e("Push staging site to production", "wp-staging"); ?></a>
        </div>
    </div>
    <!-- /Existing Clones -->
<?php endif ?>

<div id="wpstg-no-staging-site-results" class="wpstg-clone" <?php echo ($stagingSites !== [] || $error) ? 'style="display: none;"' : ''; ?> >
    <img class="wpstg--dashicons" src="<?php echo esc_url($iconPath); ?>" alt="cloud">
    <div class="no-staging-site-found-text">
        <?php esc_html_e('No Staging Site found. Create your first Staging Site above!', 'wp-staging'); ?>
    </div>
</div>

<?php if ($error) : ?>
    <div class="wpstg-clone wpstg-clone-error wpstg--error">
        <h4><?php echo esc_html__("Staging Sites Error: ", "wp-staging"); ?></h4>
        <p><?php esc_html_e('Staging sites data is corrupted. See the option below to fix it. Contact WP Staging support for more info!', 'wp-staging'); ?></p>
        <button id="wpstg-fix-staging-sites-option" class="wpstg-button wpstg-mr-10px">
            <?php echo esc_html__("Report and fix this issue", "wp-staging"); ?>
        </button>
        <button id="wpstg-report-corrupted-staging-sites" class="wpstg-button">
            <?php echo esc_html__("Report this issue only", "wp-staging"); ?>
        </button>
        <p>
        <?php echo sprintf(
            esc_html__("Note: This fix will create a backup of the corrupted staging site options and clean the listed staging sites. You will still be able to access your existing staging sites and can %s.", "wp-staging"),
            "<a href='https://wp-staging.com/docs/reconnect-staging-site-to-production-website/' target='_blank'>" . esc_html__('reconnect them to the production website', 'wp-staging') . "</a>"
        ); ?>
        </p>
    </div>
<?php endif; ?>

<?php Hooks::doAction(TemplateEngine::HOOK_RENDER_PRO_TEMPLATES); ?>

<!-- Remove Clone -->
<div id="wpstg-removing-clone">

</div>
<!-- /Remove Clone -->
