<?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 Test Links'; ?></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 new test link
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_test'])) {
    $stmt = $pdo->prepare("INSERT INTO affiliate_test_links 
        (affiliate_id, offer_id, test_url, status, notes, added_by) 
        VALUES (?, ?, ?, ?, ?, ?)");
    $stmt->execute([
        $affId,
        $_POST['offer_id'],
        $_POST['test_url'],
        $_POST['status'],
        $_POST['notes'],
        $_SESSION['admin_id']
    ]);
    echo "<p class='success'>🧪 Test link added.</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 test links
$stmt = $pdo->prepare("SELECT t.*, o.offer_name, a.username FROM affiliate_test_links t 
                       JOIN partners_offers o ON t.offer_id = o.offer_id 
                       JOIN admin_users a ON t.added_by = a.id 
                       WHERE t.affiliate_id = ? ORDER BY t.added_at DESC");
$stmt->execute([$affId]);
$tests = $stmt->fetchAll(PDO::FETCH_ASSOC);

// UI
echo "<h2>🧪 QA Test Links 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>Test URL: <input name='test_url' placeholder='Test URL'></label><br>
    <label>Status:
        <select name='status'>
            <option value='pending'>Pending</option>
            <option value='passed'>Passed</option>
            <option value='failed'>Failed</option>
        </select>
    </label><br>
    <label>Notes:<br>
        <textarea name='notes' placeholder='Notes'></textarea>
    </label><br>
    <button name='add_test' type='submit'>Add Test Link</button>
</form>";

echo "<hr><h3>🔍 Test History</h3>
<table><tr>
    <th>Offer</th><th>URL</th><th>Status</th><th>Notes</th><th>By</th><th>Time</th>
</tr>";
foreach ($tests as $t) {
    echo "<tr>
        <td>" . htmlspecialchars($t['offer_name']) . "</td>
        <td><a href='" . htmlspecialchars($t['test_url']) . "' target='_blank'>Link</a></td>
        <td>" . htmlspecialchars($t['status']) . "</td>
        <td><textarea readonly style='width:200px;height:40px'>" . htmlspecialchars($t['notes']) . "</textarea></td>
        <td>" . htmlspecialchars($t['username']) . "</td>
        <td>{$t['added_at']}</td>
    </tr>";
}
echo "</table>";
?>
</div>
</body>
</html>