<?php
if (!defined('ABSPATH')) exit;

function acwbsrh_schedule_page() {
    // Show any settings messages
    settings_errors('acwbsrh_schedule_messages'); 

    // Get next scheduled cron time
    $acwbsrh_next = wp_next_scheduled('acwbsrh_auto_generate_blog');

    // Get latest blog post
    $acwbsrh_latest_post = get_posts([
        'post_type'      => 'post',
        'posts_per_page' => 1,
        'orderby'        => 'date',
        'order'          => 'DESC',
    ]);

    ?>
    <div class="wrap">
        <h1>⏱️ Scheduled Blog Overview</h1>

        <div class="notice notice-info" style="margin-top: 20px;">
            <p><strong>Note:</strong> This page shows your most recent and upcoming automated blog activity. The schedule is based on your <a href="<?php echo esc_url(admin_url('admin.php?page=acwbsrh-main')); ?>">Settings</a> (Blog Frequency) and runs relative to your last use of <a href="<?php echo esc_url(admin_url('admin.php?page=acwbsrh-manual')); ?>">“Generate Blog Now”</a>.</p>
        </div>

        <form method="post" style="margin-top: 20px;">
            <?php wp_nonce_field('acwbsrh_pause_cron_action'); ?>
            <input type="submit" name="acwbsrh_pause_cron" class="button button-secondary" value="⏸️ Pause Auto Blog Schedule">
        </form>

        <!-- Next Scheduled Blog -->
        <div class="postbox" style="padding: 20px; margin-top: 30px;">
            <h2 class="hndle">📅 Next Scheduled Blog</h2>
            <?php if ($acwbsrh_next): ?>
                <p>
                    <strong>Scheduled Server Time:</strong>
                   <?php
					// ISO8601 format (HTML5 datetime attribute) — safe with gmdate
					$acwbsrh_datetime_attr = gmdate('c', $acwbsrh_next);

					// Display in human-readable format (localized), safe with date_i18n
					$acwbsrh_datetime_display = date_i18n('d-m-Y H:i:s', $acwbsrh_next);
					?>

					<span id="acwbsrh-countdown-time" data-time="<?php echo esc_attr($acwbsrh_datetime_attr); ?>">
						<?php echo esc_html($acwbsrh_datetime_display); ?>
					</span>
                </p>
                <p id="acwbsrh-countdown-display" style="font-weight: bold; color: #007cba;"></p>
                <p class="description">Note: This is your server time and may differ from your local timezone.</p>
            <?php else: ?>
                <p><em>No blog post is currently scheduled.</em></p>
            <?php endif; ?>
        </div>

        <!-- Last Generated Blog -->
        <div class="postbox" style="padding: 20px; margin-top: 20px;">
            <h2 class="hndle">🕒 Last Generated Blog</h2>
            <?php if (!empty($acwbsrh_latest_post)) :
                $acwbsrh_post   = $acwbsrh_latest_post[0];
                $acwbsrh_title  = esc_html($acwbsrh_post->post_title);
                $acwbsrh_date   = get_the_date('d-m-Y H:i:s', $acwbsrh_post);
                $acwbsrh_status = get_post_status($acwbsrh_post);
                $acwbsrh_link   = ($acwbsrh_status === 'publish') ? get_permalink($acwbsrh_post) : get_preview_post_link($acwbsrh_post);
            ?>
                <p>
                    <strong>Title:</strong> 
					<a href="<?php echo esc_url($acwbsrh_link); ?>" target="_blank"><?php echo esc_html($acwbsrh_title); ?></a>
                    <?php if ($acwbsrh_status !== 'publish'): ?>
                        <em style="color: orange;">(Draft)</em>
                    <?php endif; ?>
                </p>
                <p><strong>Date Created:</strong> <?php echo esc_html($acwbsrh_date); ?></p>
                <p><strong>Status:</strong> <?php echo esc_html(ucfirst($acwbsrh_status)); ?></p>
            <?php else: ?>
                <p><strong>No blog posts generated yet.</strong></p>
            <?php endif; ?>
        </div>
    </div>
    <?php
}