<?php
require_once __DIR__ . '/../init.php';

// Only admin can access
if ($_SESSION['role'] !== 'admin') {
    die('Access denied');
}

// Activate all inactive products
$pdo = $database->getPDO();
$stmt = $pdo->prepare("UPDATE products SET is_active = 1 WHERE is_active = 0 OR is_active IS NULL");
$stmt->execute();
$updatedCount = $stmt->rowCount();

// Get result
$allProducts = $database->select(
    "SELECT id, name, is_active FROM products ORDER BY created_at DESC LIMIT 20"
);

?>
<!DOCTYPE html>
<html>
<head>
    <title>Fix Products</title>
    <style>
        body { font-family: Arial; padding: 20px; background: #f5f5f5; }
        .container { max-width: 600px; margin: 0 auto; background: white; padding: 20px; border-radius: 8px; }
        .success { color: green; padding: 10px; background: #e8f5e9; border-radius: 4px; margin-bottom: 20px; }
        table { width: 100%; border-collapse: collapse; }
        th, td { padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }
        th { background: #6C5CE7; color: white; }
        .active { color: green; }
    </style>
</head>
<body>
    <div class="container">
        <h1>Fix Products Status</h1>
        <div class="success">
            ✅ Selesai! <?php echo $updatedCount > 0 ? "$updatedCount produk telah diaktifkan!" : "Semua produk sudah aktif."; ?>
        </div>

        <h3>20 Produk Terbaru:</h3>
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Nama Produk</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($allProducts as $prod): ?>
                <tr>
                    <td><?php echo $prod['id']; ?></td>
                    <td><?php echo htmlspecialchars($prod['name']); ?></td>
                    <td class="<?php echo $prod['is_active'] == 1 ? 'active' : 'inactive'; ?>">
                        <?php echo $prod['is_active'] == 1 ? '✅ Aktif' : '❌ Tidak Aktif'; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

        <p style="margin-top: 20px; text-align: center;">
            <a href="<?php echo APP_URL; ?>/products/index.php" style="background: #6C5CE7; color: white; padding: 10px 20px; text-decoration: none; border-radius: 4px;">
                👉 Lihat Daftar Produk
            </a>
        </p>
    </div>
</body>
</html>
