<?php
require_once '../includes/constants.php';
require_once '../includes/session.php';
require_once '../includes/functions.php';

if (!isset($_SESSION['admin_id']) || !in_array($_SESSION['admin_role'], ['manager','superadmin'])) {
    die("Access denied");
}

$offerId = $_GET['id'] ?? 0;
if (!$offerId) die("Missing offer ID");

// Fetch offer
$stmt = $pdo->prepare("SELECT * FROM partners_offers WHERE offer_id = ?");
$stmt->execute([$offerId]);
$offer = $stmt->fetch(PDO::FETCH_ASSOC);

// Count modules
$counts = [];
foreach ([
    'partners_offer_postbacks' => 'postbacks',
    'partners_offer_modules' => 'modules',
    'partners_offer_notes' => 'notes',
    'partners_offer_files' => 'files',
    'partners_offer_tracking' => 'tracking',
    'partners_offer_tests' => 'tests',
    'partners_offer_terms' => 'terms',
    'partners_offer_delivery' => 'delivery',
    'partners_offer_caps' => 'caps',
    'partners_offer_payouts' => 'payouts',
    'partners_offer_contracts' => 'contracts',
    'partners_offer_history' => 'history'
] as $table => $key) {
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM $table WHERE offer_id = ?");
    $stmt->execute([$offerId]);
    $counts[$key] = $stmt->fetchColumn();
}
?>
<!DOCTYPE html>
<html>
<head>
    <title><?php echo SITE_TITLE . ' | Offer Dashboard'; ?></title>
    <link rel="stylesheet" href="css/admin.css">
</head>
<body>
<div class="container">
<h2>🧭 Offer Dashboard for #<?php echo htmlspecialchars($offerId); ?></h2>
<p><strong>Offer Name:</strong> <?php echo htmlspecialchars($offer['offer_name']); ?></p>
<p><strong>Status:</strong> <?php echo htmlspecialchars($offer['offer_status']); ?></p>
<p><strong>Global Payout:</strong> <?php echo htmlspecialchars($offer['offer_payout']) . ' ' . htmlspecialchars($offer['offer_currency']); ?></p>
<hr>

<table>
<tr><th>Module</th><th>Entries</th><th>Link</th></tr>
<?php
foreach ($counts as $key => $count) {
    $label = ucfirst($key);
    $link = "admin_offer_{$key}.php?id=" . htmlspecialchars($offerId);
    echo "<tr>
        <td>" . htmlspecialchars($label) . "</td>
        <td>" . htmlspecialchars($count) . "</td>
        <td><a href='" . htmlspecialchars($link) . "'>Manage</a></td>
    </tr>";
}
?>
</table>
</div>
</body>
</html>