<?php
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 10;
$offset = ($page - 1) * $limit;

// Count Total
$countQuery = "SELECT COUNT(*) FROM queue_ads";
$stmt = $db->query($countQuery);
$totalAds = $stmt->fetchColumn();
$totalPages = ceil($totalAds / $limit);

// Fetch Data
$query = "SELECT * FROM queue_ads ORDER BY created_at DESC LIMIT $limit OFFSET $offset";
$stmt = $db->query($query);
$ads = $stmt->fetchAll();
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h3 class="fw-bold text-secondary"><?php echo __('queue_ads'); ?></h3>
    <button class="btn btn-primary shadow-sm" data-bs-toggle="modal" data-bs-target="#addAdModal" onclick="resetAdModal()">
        <i class="bi bi-plus-lg me-1"></i> <?php echo __('add_ad'); ?>
    </button>
</div>

<div class="card shadow-sm border-0">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light text-secondary">
                    <tr>
                        <th class="px-4 py-3">#</th>
                        <th class="py-3"><?php echo __('ad_text_en'); ?></th>
                        <th class="py-3"><?php echo __('ad_text_ar'); ?></th>
                        <th class="py-3"><?php echo __('status'); ?></th>
                        <th class="py-3 text-end px-4"><?php echo __('actions'); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($ads)): ?>
                    <tr>
                        <td colspan="5" class="text-center py-5 text-muted">
                            <i class="bi bi-megaphone display-4 d-block mb-3"></i>
                            <?php echo __('no_ads_found'); ?>
                        </td>
                    </tr>
                    <?php else: ?>
                    <?php foreach ($ads as $ad): ?>
                    <tr>
                        <td class="px-4 text-secondary"><?php echo $ad['id']; ?></td>
                        <td class="fw-semibold text-dark text-truncate" style="max-width: 200px;" title="<?php echo htmlspecialchars($ad['text_en']); ?>"><?php echo htmlspecialchars($ad['text_en']); ?></td>
                        <td class="text-secondary text-truncate" style="max-width: 200px;" title="<?php echo htmlspecialchars($ad['text_ar']); ?>"><?php echo htmlspecialchars($ad['text_ar']); ?></td>
                        <td>
                            <?php if ($ad['active']): ?>
                                <span class="badge bg-success bg-opacity-10 text-success px-3 py-2 rounded-pill"><?php echo __('active'); ?></span>
                            <?php else: ?>
                                <span class="badge bg-danger bg-opacity-10 text-danger px-3 py-2 rounded-pill"><?php echo __('inactive'); ?></span>
                            <?php endif; ?>
                        </td>
                        <td class="text-end px-4">
                            <div class="btn-group shadow-sm border rounded bg-white">
                                <button class="btn btn-link text-primary py-1 px-2 border-end" 
                                        onclick="showEditAdModal(<?php echo htmlspecialchars(json_encode($ad, JSON_UNESCAPED_UNICODE)); ?>)"
                                        data-bs-toggle="tooltip" title="<?php echo __('edit'); ?>">
                                    <i class="bi bi-pencil-square"></i>
                                </button>
                                <button class="btn btn-link text-danger py-1 px-2" 
                                        onclick="showDeleteAdModal(<?php echo $ad['id']; ?>)"
                                        data-bs-toggle="tooltip" title="<?php echo __('delete'); ?>">
                                    <i class="bi bi-trash3"></i>
                                </button>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        
        <!-- Pagination -->
        <?php if ($totalPages > 1): ?>
        <div class="d-flex justify-content-between align-items-center p-3 border-top">
            <div class="text-muted small">
                <?php echo __('showing'); ?> <?php echo $offset + 1; ?> - <?php echo min($offset + $limit, $totalAds); ?> <?php echo __('of'); ?> <?php echo $totalAds; ?>
            </div>
            <nav aria-label="Page navigation">
                <ul class="pagination pagination-sm mb-0">
                    <li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
                        <a class="page-link" href="?page=<?php echo $page - 1; ?>" aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                    <li class="page-item <?php echo $page == $i ? 'active' : ''; ?>">
                        <a class="page-link" href="?page=<?php echo $i; ?>"><?php echo $i; ?></a>
                    </li>
                    <?php endfor; ?>
                    <li class="page-item <?php echo $page >= $totalPages ? 'disabled' : ''; ?>">
                        <a class="page-link" href="?page=<?php echo $page + 1; ?>" aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
        <?php endif; ?>
    </div>
</div>

<!-- Add/Edit Ad Modal -->
<div class="modal fade" id="addAdModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content border-0 shadow">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title" id="adModalTitle"><?php echo __('add_ad'); ?></h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form method="POST" action="">
                <input type="hidden" name="action" id="adAction" value="add_queue_ad">
                <input type="hidden" name="id" id="adId">
                <div class="modal-body p-4">
                    <div class="mb-3">
                        <label class="form-label"><?php echo __('ad_text_en'); ?> <span class="text-danger">*</span></label>
                        <textarea class="form-control" name="text_en" id="adTextEn" required rows="3"></textarea>
                    </div>
                    <div class="mb-3">
                        <label class="form-label"><?php echo __('ad_text_ar'); ?> <span class="text-danger">*</span></label>
                        <textarea class="form-control" name="text_ar" id="adTextAr" required rows="3"></textarea>
                    </div>
                    <div class="form-check form-switch">
                        <input class="form-check-input" type="checkbox" name="active" id="adActive" checked value="1">
                        <label class="form-check-label" for="adActive"><?php echo __('active'); ?></label>
                    </div>
                </div>
                <div class="modal-footer bg-light">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?php echo __('close'); ?></button>
                    <button type="submit" class="btn btn-primary"><?php echo __('save'); ?></button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Delete Ad Modal -->
<div class="modal fade" id="deleteAdModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content border-0 shadow">
            <div class="modal-header bg-danger text-white">
                <h5 class="modal-title"><?php echo __('delete_ad'); ?></h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form method="POST" action="">
                <input type="hidden" name="action" value="delete_queue_ad">
                <input type="hidden" name="id" id="deleteAdId">
                <div class="modal-body p-4 text-center">
                    <div class="mb-3 text-danger">
                        <i class="bi bi-exclamation-triangle display-1"></i>
                    </div>
                    <p class="mb-0 fs-5"><?php echo __('are_you_sure_delete'); ?></p>
                    <p class="text-muted small"><?php echo __('action_cannot_be_undone'); ?></p>
                </div>
                <div class="modal-footer bg-light justify-content-center">
                    <button type="button" class="btn btn-secondary px-4" data-bs-dismiss="modal"><?php echo __('cancel'); ?></button>
                    <button type="submit" class="btn btn-danger px-4"><?php echo __('delete'); ?></button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
function resetAdModal() {
    document.getElementById('adModalTitle').textContent = '<?php echo __('add_ad'); ?>';
    document.getElementById('adAction').value = 'add_queue_ad';
    document.getElementById('adId').value = '';
    
    document.getElementById('adTextEn').value = '';
    document.getElementById('adTextAr').value = '';
    document.getElementById('adActive').checked = true;
}

function showEditAdModal(ad) {
    document.getElementById('adModalTitle').textContent = '<?php echo __('edit_ad'); ?>';
    document.getElementById('adAction').value = 'edit_queue_ad';
    document.getElementById('adId').value = ad.id;
    
    document.getElementById('adTextEn').value = ad.text_en;
    document.getElementById('adTextAr').value = ad.text_ar;
    document.getElementById('adActive').checked = (ad.active == 1);
    
    var modal = new bootstrap.Modal(document.getElementById('addAdModal'));
    modal.show();
}

function showDeleteAdModal(id) {
    document.getElementById('deleteAdId').value = id;
    var modal = new bootstrap.Modal(document.getElementById('deleteAdModal'));
    modal.show();
}
</script>