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

if (!isset($_SESSION['admin_id']) || !in_array($_SESSION['admin_role'], ['manager','superadmin'])) {
    die("Access denied");
}
?>
<!DOCTYPE html>
<html>
<head>
    <title><?php echo SITE_TITLE . ' | Affiliate Terms'; ?></title>
    <link rel="stylesheet" href="css/admin.css">
</head>
<body>
<div class="container">
<?php
$affId = $_GET['id'] ?? 0;
if (!$affId) die("Missing affiliate ID");

// Handle update
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $stmt = $pdo->prepare("REPLACE INTO affiliate_terms 
        (affiliate_id, offer_id, geo_restrictions, traffic_restrictions, compliance_notes, enforced_by) 
        VALUES (?, ?, ?, ?, ?, ?)");
    $stmt->execute([
        $affId,
        $_POST['offer_id'],
        $_POST['geo_restrictions'],
        $_POST['traffic_restrictions'],
        $_POST['compliance_notes'],
        $_SESSION['admin_id']
    ]);
    echo "<p class='success'>📜 Terms updated.</p>";
}

// Fetch offers
$stmt = $pdo->query("SELECT offer_id, offer_name FROM partners_offers ORDER BY offer_name");
$offers = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Fetch current terms
$stmt = $pdo->prepare("SELECT t.*, o.offer_name, a.username FROM affiliate_terms t 
                       JOIN partners_offers o ON t.offer_id = o.offer_id 
                       JOIN admin_users a ON t.enforced_by = a.id 
                       WHERE t.affiliate_id = ? ORDER BY t.enforced_at DESC");
$stmt->execute([$affId]);
$terms = $stmt->fetchAll(PDO::FETCH_ASSOC);

// UI
echo "<h2>📜 Terms for Affiliate #" . htmlspecialchars($affId) . "</h2>
<form method='post'>
    <label>Offer:
        <select name='offer_id'>";
foreach ($offers as $o) {
    echo "<option value='" . htmlspecialchars($o['offer_id']) . "'>" . htmlspecialchars($o['offer_name']) . " (#" . htmlspecialchars($o['offer_id']) . ")</option>";
}
echo "</select></label><br>
    <label>Geo Restrictions:<br>
        <textarea name='geo_restrictions' placeholder='Geo restrictions'></textarea>
    </label><br>
    <label>Traffic Restrictions:<br>
        <textarea name='traffic_restrictions' placeholder='Traffic restrictions'></textarea>
    </label><br>
    <label>Compliance Notes:<br>
        <textarea name='compliance_notes' placeholder='Compliance notes'></textarea>
    </label><br>
    <button type='submit'>Update Terms</button>
</form>";

echo "<hr><h3>🔍 Current Overrides</h3>
<table><tr>
    <th>Offer</th><th>Geo</th><th>Traffic</th><th>Compliance</th><th>By</th><th>Time</th>
</tr>";
foreach ($terms as $t) {
    echo "<tr>
        <td>" . htmlspecialchars($t['offer_name']) . "</td>
        <td><textarea readonly style='width:200px;height:40px'>" . htmlspecialchars($t['geo_restrictions']) . "</textarea></td>
        <td><textarea readonly style='width:200px;height:40px'>" . htmlspecialchars($t['traffic_restrictions']) . "</textarea></td>
        <td><textarea readonly style='width:200px;height:40px'>" . htmlspecialchars($t['compliance_notes']) . "</textarea></td>
        <td>" . htmlspecialchars($t['username']) . "</td>
        <td>{$t['enforced_at']}</td>
    </tr>";
}
echo "</table>";
?>
</div>
</body>
</html>