<?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");

// Handle insert/update
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $stmt = $pdo->prepare("INSERT INTO partners_offer_redirects 
        (offer_id, redirect_type, redirect_url, notes, device_type, referrer_pattern, subid_pattern, traffic_source, expires_at, priority, advertiser_visible)
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    $stmt->execute([
        $offerId,
        $_POST['redirect_type'],
        $_POST['redirect_url'],
        $_POST['notes'],
        $_POST['device_type'],
        $_POST['referrer_pattern'],
        $_POST['subid_pattern'],
        $_POST['traffic_source'],
        $_POST['expires_at'] ?: null,
        $_POST['priority'] ?? 0,
        isset($_POST['advertiser_visible']) ? 1 : 0
    ]);
    echo "<p class='success'>✅ Redirect saved.</p>";
}

// Fetch redirects
$stmt = $pdo->prepare("SELECT * FROM partners_offer_redirects WHERE offer_id = ? ORDER BY priority DESC, created_at DESC");
$stmt->execute([$offerId]);
$redirects = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
    <title><?php echo SITE_TITLE . ' | Offer Redirects'; ?></title>
    <link rel="stylesheet" href="css/admin.css">
</head>
<body>
<div class="container">
<h2>🔀 Redirect Settings for Offer #<?php echo htmlspecialchars($offerId); ?></h2>
<form method="post">
    <label>Redirect Type:
        <select name="redirect_type">
            <option value="fallback">Fallback</option>
            <option value="override">Override</option>
            <option value="test">Test Link</option>
        </select>
    </label><br>
    <label>Redirect URL:<br>
        <input name="redirect_url" placeholder="Redirect URL" required>
    </label><br>
    <label>Device Type:
        <select name="device_type">
            <option value="any">Any Device</option>
            <option value="mobile">Mobile</option>
            <option value="desktop">Desktop</option>
            <option value="tablet">Tablet</option>
        </select>
    </label><br>
    <label>Referrer Pattern:<br>
        <input name="referrer_pattern" placeholder="Referrer pattern (optional)">
    </label><br>
    <label>SubID Pattern:<br>
        <input name="subid_pattern" placeholder="SubID pattern (optional)">
    </label><br>
    <label>Traffic Source:<br>
        <input name="traffic_source" placeholder="Traffic source (optional)">
    </label><br>
    <label>Expiration:<br>
        <input name="expires_at" type="datetime-local" placeholder="Expiration (optional)">
    </label><br>
    <label>Priority:<br>
        <input name="priority" type="number" placeholder="Priority (0 = lowest)" value="0">
    </label><br>
    <label><input type="checkbox" name="advertiser_visible" checked> Visible to Advertiser</label><br>
    <label>Notes:<br>
        <textarea name="notes" placeholder="Notes (optional)"></textarea>
    </label><br>
    <button type="submit">Save Redirect</button>
</form>

<hr><h2>📋 Existing Redirects</h2>
<table><tr>
    <th>Type</th><th>Device</th><th>Priority</th><th>Expires</th>
    <th>Referrer</th><th>SubID</th><th>Source</th><th>Visible</th>
    <th>URL</th><th>Notes</th>
</tr>
<?php
foreach ($redirects as $r) {
    echo "<tr>
        <td>" . htmlspecialchars($r['redirect_type']) . "</td>
        <td>" . htmlspecialchars($r['device_type']) . "</td>
        <td>" . htmlspecialchars($r['priority']) . "</td>
        <td>" . ($r['expires_at'] ? htmlspecialchars($r['expires_at']) : '-') . "</td>
        <td>" . htmlspecialchars($r['referrer_pattern']) . "</td>
        <td>" . htmlspecialchars($r['subid_pattern']) . "</td>
        <td>" . htmlspecialchars($r['traffic_source']) . "</td>
        <td>" . ($r['advertiser_visible'] ? '👁️' : '🙈') . "</td>
        <td><textarea readonly style='width:300px;height:40px'>" . htmlspecialchars($r['redirect_url']) . "</textarea></td>
        <td><textarea readonly style='width:300px;height:40px'>" . htmlspecialchars($r['notes']) . "</textarea></td>
    </tr>";
}
?>
</table>
</div>
</body>
</html>