<?php
/**
 * Aysa Cron Status Display
 * 
 * Shows the status of automated association processing
 */

if (!defined('ABSPATH')) {
    exit;
}

$next_scheduled = wp_next_scheduled('aysa_process_association_tasks');
$last_processing = get_option('aysa_last_association_processing');
$remaining_count = get_option('aysa_remaining_association_count', 0);
?>

<div class="aysa-cron-status-container">
    <div class="aysa-section-header">
        <h3>🤖 Automated Association Processing</h3>
        <p>Automatic keyword association runs every hour to process unlinked keywords</p>
    </div>
    
    <div class="aysa-cron-status-grid">
        <div class="aysa-status-card">
            <div class="aysa-status-icon">⏰</div>
            <div class="aysa-status-content">
                <h4>Next Processing</h4>
                <p class="aysa-status-value">
                    <?php 
                    if ($next_scheduled) {
                        echo date('Y-m-d H:i:s', $next_scheduled);
                    } else {
                        echo 'Not scheduled';
                    }
                    ?>
                </p>
            </div>
        </div>
        
        <div class="aysa-status-card">
            <div class="aysa-status-icon">📊</div>
            <div class="aysa-status-content">
                <h4>Keywords Remaining</h4>
                <p class="aysa-status-value"><?php echo intval($remaining_count); ?></p>
            </div>
        </div>
        
        <div class="aysa-status-card">
            <div class="aysa-status-icon">🕒</div>
            <div class="aysa-status-content">
                <h4>Last Processing</h4>
                <p class="aysa-status-value">
                    <?php 
                    if ($last_processing) {
                        echo date('Y-m-d H:i:s', strtotime($last_processing));
                    } else {
                        echo 'Never';
                    }
                    ?>
                </p>
            </div>
        </div>
        
        <div class="aysa-status-card">
            <div class="aysa-status-icon">⚙️</div>
            <div class="aysa-status-content">
                <h4>Status</h4>
                <p class="aysa-status-value">
                    <?php 
                    if ($next_scheduled) {
                        echo '<span class="aysa-status-active">Active</span>';
                    } else {
                        echo '<span class="aysa-status-inactive">Inactive</span>';
                    }
                    ?>
                </p>
            </div>
        </div>
    </div>
    
    <div class="aysa-cron-actions">
        <button id="aysa-trigger-cron" class="aysa-btn aysa-btn-primary">
            🚀 Trigger Processing Now
        </button>
        <button id="aysa-refresh-status" class="aysa-btn aysa-btn-secondary">
            🔄 Refresh Status
        </button>
    </div>
    
    <div class="aysa-cron-info">
        <h4>How it works:</h4>
        <ol>
            <li><strong>Extract Keywords:</strong> Gets the number of keywords needing association from tasks</li>
            <li><strong>Reassociate:</strong> Runs the same process as "Reassociate Unlinked" button</li>
            <li><strong>Update Status:</strong> Checks remaining keywords and updates task descriptions</li>
        </ol>
        
        <div class="aysa-cron-note">
            <strong>Note:</strong> The cron job runs automatically every hour. You can also trigger it manually using the button above.
        </div>
    </div>
</div>

<style>
.aysa-cron-status-container {
    background: #fff;
    border-radius: 8px;
    padding: 24px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    margin: 20px 0;
}

.aysa-section-header h3 {
    color: #1e293b;
    margin: 0 0 8px 0;
    font-size: 20px;
}

.aysa-section-header p {
    color: #64748b;
    margin: 0 0 24px 0;
}

.aysa-cron-status-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}

.aysa-status-card {
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    padding: 16px;
    display: flex;
    align-items: center;
    gap: 12px;
}

.aysa-status-icon {
    font-size: 24px;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #fff;
    border-radius: 50%;
    border: 1px solid #e2e8f0;
}

.aysa-status-content h4 {
    margin: 0 0 4px 0;
    font-size: 14px;
    color: #64748b;
    font-weight: 500;
}

.aysa-status-value {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    color: #1e293b;
}

.aysa-status-active {
    color: #059669;
}

.aysa-status-inactive {
    color: #dc2626;
}

.aysa-cron-actions {
    display: flex;
    gap: 12px;
    margin-bottom: 24px;
}

.aysa-btn {
    padding: 10px 16px;
    border: none;
    border-radius: 6px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

.aysa-btn-primary {
    background: #3b82f6;
    color: white;
}

.aysa-btn-primary:hover {
    background: #2563eb;
}

.aysa-btn-secondary {
    background: #f1f5f9;
    color: #475569;
    border: 1px solid #e2e8f0;
}

.aysa-btn-secondary:hover {
    background: #e2e8f0;
}

.aysa-cron-info {
    background: #f0f9ff;
    border: 1px solid #bae6fd;
    border-radius: 6px;
    padding: 16px;
}

.aysa-cron-info h4 {
    margin: 0 0 12px 0;
    color: #0c4a6e;
}

.aysa-cron-info ol {
    margin: 0 0 16px 0;
    padding-left: 20px;
}

.aysa-cron-info li {
    margin-bottom: 4px;
    color: #0f172a;
}

.aysa-cron-note {
    background: #fef3c7;
    border: 1px solid #fbbf24;
    border-radius: 4px;
    padding: 12px;
    color: #92400e;
    font-size: 14px;
}
</style>

<script>
jQuery(document).ready(function($) {
    $('#aysa-trigger-cron').on('click', function() {
        const $btn = $(this);
        $btn.prop('disabled', true).text('🔄 Processing...');
        
        $.ajax({
            url: ajaxurl,
            type: 'POST',
            data: {
                action: 'aysa_trigger_cron_processing',
                nonce: '<?php echo wp_create_nonce('aysa_cron_nonce'); ?>'
            },
            success: function(response) {
                if (response.success) {
                    alert('Association processing triggered successfully!');
                    location.reload();
                } else {
                    alert('Error: ' + (response.data || 'Unknown error'));
                }
            },
            error: function() {
                alert('Error triggering cron processing');
            },
            complete: function() {
                $btn.prop('disabled', false).text('🚀 Trigger Processing Now');
            }
        });
    });
    
    $('#aysa-refresh-status').on('click', function() {
        location.reload();
    });
});
</script>
