<?php
/**
 * Admin Paneli - Stok Alarmları (Gelince Haber Ver) Listesi
 * Konum: /magaza/admin/stok-alarmlari/index.php
 */
require_once '../../includes/db.php';

// Hangi ürünü kaç kişi bekliyor? (En çok beklenen en üstte)
$sql = "SELECT p.id as product_id, p.name as product_name, p.stock_quantity, p.status as p_status, 
               COUNT(sa.id) as bekleyen_kisi 
        FROM stock_alerts sa 
        JOIN products p ON sa.product_id = p.id 
        WHERE sa.status = 0 
        GROUP BY sa.product_id 
        ORDER BY bekleyen_kisi DESC";

$stmt = $db->query($sql);
$alarmlar = $stmt->fetchAll(PDO::FETCH_ASSOC);

$page_title = "Stok Alarmları (Bekleyenler)";
require_once '../header.php';
?>

<div class="container-fluid px-4">
    <h1 class="mt-4">Stok Alarmları</h1>
    <ol class="breadcrumb mb-4">
        <li class="breadcrumb-item active">Hangi ürünü kaç kişi bekliyor?</li>
    </ol>

    <div class="card mb-4 shadow-sm border-0">
        <div class="card-header bg-dark text-white">
            <i class="bi bi-bell-fill text-warning me-1"></i> Bekleyen Talepler
        </div>
        <div class="card-body table-responsive">
            <table class="table table-striped table-hover align-middle">
                <thead>
                    <tr>
                        <th>Ürün Adı</th>
                        <th class="text-center">Mevcut Stok</th>
                        <th class="text-center">Ürün Durumu</th>
                        <th class="text-center text-danger"><i class="bi bi-people-fill"></i> Bekleyen Kişi</th>
                        <th class="text-end">İşlemler</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (count($alarmlar) > 0): ?>
                        <?php foreach ($alarmlar as $row): ?>
                            <tr>
                                <td class="fw-bold text-primary"><?= htmlspecialchars($row['product_name']) ?></td>
                                <td class="text-center">
                                    <?php if($row['stock_quantity'] > 0): ?>
                                        <span class="badge bg-success"><?= $row['stock_quantity'] ?> Adet</span>
                                    <?php else: ?>
                                        <span class="badge bg-danger">Tükendi (0)</span>
                                    <?php endif; ?>
                                </td>
                                <td class="text-center">
                                    <?php if($row['p_status'] == 1): ?>
                                        <span class="badge bg-primary">Satışa Açık</span>
                                    <?php else: ?>
                                        <span class="badge bg-secondary">Satışa Kapalı</span>
                                    <?php endif; ?>
                                </td>
                                <td class="text-center text-danger fw-bold fs-5">
                                    <?= $row['bekleyen_kisi'] ?>
                                </td>
                                <td class="text-end">
                                    <a href="detay.php?id=<?= $row['product_id'] ?>" class="btn btn-sm btn-info text-white fw-bold shadow-sm">
                                        <i class="bi bi-eye"></i> Kimler Bekliyor?
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="5" class="text-center py-5 text-muted">
                                <i class="bi bi-check2-circle fs-2 d-block mb-2 text-success"></i>
                                Bekleyen stok talebi bulunmuyor.
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php require_once '../footer.php'; ?>