<?php

/**
 * WP admin Sync view.
 *
 * @package northmill-online-shared
 */
declare (strict_types=1);
namespace Northmill_Online_Shared_Checkout;

/**
 * View parameters.
 *
 * @var array $params
 */
use Northmill_Online_Shared_Checkout\Northmill_Online_Shared\Modules\Admin\Pages\Page\Admin_Sync\Models\Admin_Sync_Tool_Model;
\defined('ABSPATH') || exit;
?>
<div class="wrap">
	<h2><?php 
echo \esc_html($params['title']);
?></h2>
	<div>
		<?php 
if ($params['has_hub_accounts']) {
    ?>
			<h3><?php 
    \esc_html_e('Automatic Sync Status', 'northmill-online-shared');
    ?></h3>
			<?php 
    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    echo $params['sync_status'];
    ?>
			<hr>

			<?php 
    if (!empty($params['tools'])) {
        echo '<div id="nms-tools-section">';
        /**
         * Tools.
         *
         * @var Admin_Sync_Tool_Model[] $tools
         */
        $tools = $params['tools'];
        foreach ($tools as $tool) {
            $tool->render();
        }
        echo '</div>';
        echo '<hr>';
    }
    ?>

			<h3><?php 
    \esc_html_e('Manually sync all', 'northmill-online-shared');
    ?></h3>

			<form method="POST" action="">

				<table id="nmf-sync-actions">
					<tbody>
						<tr>
							<td>
								<?php 
    $nonce = \wp_create_nonce('mf_ajax_sync_nonce');
    ?>
								<input type="button"
									name="mf_btn_sync"
									id="mf_sync_now"
									data-nonce="<?php 
    echo \esc_attr($nonce);
    ?>"
									class="button button-primary"
									value="<?php 
    \esc_attr_e('Manual sync', 'northmill-online-shared');
    ?>">
								<input type="button"
									name="mf_btn_resume_sync"
									id="mf_sync_resume"
									data-nonce="<?php 
    echo \esc_attr($nonce);
    ?>"
									class="button button-primary"
									style="display: none;"
									value="<?php 
    \esc_attr_e('Attempt Resume Sync', 'northmill-online-shared');
    ?>">
								<p>
									<label for="mf_sync_per_page"><?php 
    \esc_html_e('Items per page', 'northmill-online-shared');
    ?></label>
									<select name="mf_sync_per_page" id="mf_sync_per_page">
										<option value="10">10</option>
										<option value="20" selected>20</option>
										<option value="30">30</option>
										<option value="50">50</option>
										<option value="100">100</option>
										<option value="200">200</option>
									</select>
								</p>
								<p>
									<input type="checkbox" value="1" id="nmf_get_all_data" name="nmf_get_all_data" />
									<label for="nmf_get_all_data"><?php 
    \esc_html_e('Retrieve all data from Hub, not just recent changes.', 'northmill-online-shared');
    ?></label><br>

									<input type="checkbox" value="1" id="mf_sync_auto_resume" name="mf_sync_auto_resume" />
									<label for="mf_sync_auto_resume"><?php 
    \esc_html_e('Auto-attempt resume on error', 'northmill-online-shared');
    ?></label>
								</p>
								<p>
								</p>
							</td>
							<td>
								<p style="margin-top: 0;">
									<strong><?php 
    \esc_html_e('Actions for manual sync', 'northmill-online-shared');
    ?></strong><br>
									<?php 
    \esc_html_e('Leave all enabled for default sync.', 'northmill-online-shared');
    ?><br>
								</p>

								<?php 
    foreach ($params['ajax_syncer_data'] as $ajax_action => $syncer) {
        echo '<input type="checkbox" class="nmf-ajax-sync-filter" id="enabled_' . \esc_attr($ajax_action) . '" checked>';
        echo '<label for="enabled_nmf_sync_groups">' . \esc_html($syncer['title']) . '</label>';
        echo '<br>';
    }
    ?>
							</td>
						</tr>
					<tr>
						<td>
						</td>
						<td>&nbsp;</td>
					</tr>
					</tbody>
				</table>
			</form>

			<!--suppress HtmlFormInputWithoutLabel -->
			<textarea id="mf_sync_status" style="width: 100%;height: 500px;display: none;" disabled></textarea>
		<?php 
} else {
    ?>
			<p>
				<?php 
    \printf(
        // translators: %1$s: opening anchor tag, %2$s: closing anchor tag.
        \esc_html__('No Hub accounts found. Add at least one in %1$ssettings%2$s to get started.', 'northmill-online-shared'),
        '<a href="' . \esc_url($params['settings_url']) . '">',
        '</a>'
    );
    ?>
			</p>
		<?php 
}
?>
	</div>
</div>
<?php 
