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

$userId = $_SESSION['user_id'];

// Get all products count
$allProds = $database->select("SELECT * FROM products WHERE user_id = ?", [$userId]);
$totalAll = count($allProds);

// Get active products count
$activeProd = $database->select("SELECT * FROM products WHERE user_id = ? AND is_active = 1", [$userId]);
$totalActive = count($activeProd);

// Get inactive products count
$inactiveProd = $database->select("SELECT * FROM products WHERE user_id = ? AND is_active = 0", [$userId]);
$totalInactive = count($inactiveProd);

// Get NULL is_active
$nullProd = $database->select("SELECT * FROM products WHERE user_id = ? AND is_active IS NULL", [$userId]);
$totalNull = count($nullProd);

?>
<!DOCTYPE html>
<html>
<head>
    <title>Debug Products</title>
    <style>
        body { font-family: Arial; padding: 20px; background: #f5f5f5; }
        .container { max-width: 800px; margin: 0 auto; background: white; padding: 20px; border-radius: 8px; }
        .stat { padding: 15px; margin: 10px 0; border-radius: 4px; }
        .stat-all { background: #e3f2fd; border-left: 4px solid #2196F3; }
        .stat-active { background: #e8f5e9; border-left: 4px solid #4CAF50; }
        .stat-inactive { background: #fff3e0; border-left: 4px solid #FF9800; }
        .stat-null { background: #f3e5f5; border-left: 4px solid #9C27B0; }
        .stat h3 { margin: 0 0 5px 0; }
        .stat p { margin: 0; font-size: 24px; font-weight: bold; }
        .action-buttons { margin: 20px 0; }
        .btn { padding: 10px 20px; margin: 5px; border: none; border-radius: 4px; cursor: pointer; text-decoration: none; display: inline-block; }
        .btn-primary { background: #2196F3; color: white; }
        .btn-success { background: #4CAF50; color: white; }
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        th, td { padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }
        th { background: #6C5CE7; color: white; }
        .active { color: green; }
        .inactive { color: red; }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔍 Debug Produk</h1>

        <div class="stat stat-all">
            <h3>Total Produk</h3>
            <p><?php echo $totalAll; ?></p>
        </div>

        <div class="stat stat-active">
            <h3>Produk Aktif (is_active = 1)</h3>
            <p><?php echo $totalActive; ?></p>
        </div>

        <div class="stat stat-inactive">
            <h3>Produk Tidak Aktif (is_active = 0)</h3>
            <p><?php echo $totalInactive; ?></p>
        </div>

        <div class="stat stat-null">
            <h3>Produk Status NULL</h3>
            <p><?php echo $totalNull; ?></p>
        </div>

        <div class="action-buttons">
            <h3>Tindakan:</h3>
            <a href="?action=activate" class="btn btn-success">🔄 Aktifkan Semua Produk</a>
        </div>

        <?php if ($_GET['action'] ?? '' === 'activate'): ?>
            <?php
            // Activate all
            $pdo = $database->getPDO();
            $stmt = $pdo->prepare("UPDATE products SET is_active = 1 WHERE user_id = ? AND (is_active = 0 OR is_active IS NULL)");
            $stmt->execute([$userId]);
            $updated = $stmt->rowCount();
            ?>
            <div style="background: #e8f5e9; color: #2e7d32; padding: 15px; border-radius: 4px; margin: 20px 0;">
                ✅ <strong><?php echo $updated; ?> produk telah diaktifkan!</strong>
            </div>
        <?php endif; ?>

        <h3 style="margin-top: 30px;">📋 Daftar Produk Anda:</h3>
        <?php if ($totalAll > 0): ?>
            <table>
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>SKU</th>
                        <th>Nama</th>
                        <th>Harga</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($allProds as $prod): ?>
                    <tr>
                        <td><?php echo $prod['id']; ?></td>
                        <td><?php echo htmlspecialchars($prod['sku']); ?></td>
                        <td><?php echo htmlspecialchars($prod['name']); ?></td>
                        <td>Rp <?php echo number_format($prod['selling_price'], 0, ',', '.'); ?></td>
                        <td class="<?php echo ($prod['is_active'] == 1) ? 'active' : 'inactive'; ?>">
                            <?php
                            if ($prod['is_active'] == 1) {
                                echo '✅ Aktif';
                            } elseif ($prod['is_active'] == 0) {
                                echo '❌ Tidak Aktif';
                            } else {
                                echo '⚠️ NULL';
                            }
                            ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php else: ?>
            <p style="color: #d32f2f;">⚠️ Tidak ada produk di database untuk user ini.</p>
        <?php endif; ?>

        <p style="margin-top: 30px; text-align: center;">
            <a href="<?php echo APP_URL; ?>/products/index.php" class="btn btn-primary">👉 Lihat Daftar Produk</a>
        </p>
    </div>
</body>
</html>
