<?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 global payout
$stmt = $pdo->prepare("SELECT offer_payout, offer_currency FROM partners_offers WHERE offer_id = ?");
$stmt->execute([$offerId]);
$offer = $stmt->fetch(PDO::FETCH_ASSOC);

// Handle global payout update
if (isset($_POST['update_global'])) {
    $stmt = $pdo->prepare("UPDATE partners_offers SET offer_payout = ?, offer_currency = ? WHERE offer_id = ?");
    $stmt->execute([$_POST['offer_payout'], $_POST['offer_currency'], $offerId]);
    echo "<p class='success'>✅ Global payout updated.</p>";
    $offer['offer_payout'] = $_POST['offer_payout'];
    $offer['offer_currency'] = $_POST['offer_currency'];
}

// Handle custom payout insert/update
if (isset($_POST['affiliate_id'])) {
    $sql = "INSERT INTO partners_offer_payouts (offer_id, affiliate_id, custom_payout, currency, notes)
            VALUES (?, ?, ?, ?, ?)
            ON DUPLICATE KEY UPDATE custom_payout = VALUES(custom_payout), currency = VALUES(currency), notes = VALUES(notes)";
    $stmt = $pdo->prepare($sql);
    $stmt->execute([
        $offerId,
        $_POST['affiliate_id'],
        $_POST['custom_payout'],
        $_POST['currency'],
        $_POST['notes']
    ]);
    echo "<p class='success'>✅ Custom payout saved.</p>";
}

// Fetch custom payouts
$stmt = $pdo->prepare("SELECT p.*, u.username FROM partners_offer_payouts p JOIN users u ON p.affiliate_id = u.id WHERE p.offer_id = ?");
$stmt->execute([$offerId]);
$payouts = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html>
<head>
    <title><?php echo SITE_TITLE . ' | Offer Payouts'; ?></title>
    <link rel="stylesheet" href="css/admin.css">
</head>
<body>
<div class="container">
<h2>💰 Global Payout for Offer #<?php echo htmlspecialchars($offerId); ?></h2>
<form method="post">
    <label>Payout:<br>
        <input name="offer_payout" value="<?php echo htmlspecialchars($offer['offer_payout']); ?>" placeholder="Payout">
    </label><br>
    <label>Currency:<br>
        <input name="offer_currency" value="<?php echo htmlspecialchars($offer['offer_currency']); ?>" placeholder="Currency">
    </label><br>
    <button name="update_global" type="submit">Update Global Payout</button>
</form>

<hr><h2>🎯 Custom Payouts per Affiliate</h2>
<form method="post">
    <label>Affiliate ID:<br>
        <input name="affiliate_id" placeholder="Affiliate ID">
    </label><br>
    <label>Custom Payout:<br>
        <input name="custom_payout" placeholder="Custom Payout">
    </label><br>
    <label>Currency:<br>
        <input name="currency" placeholder="Currency">
    </label><br>
    <label>Notes:<br>
        <textarea name="notes" placeholder="Notes (optional)"></textarea>
    </label><br>
    <button type="submit">Save Custom Payout</button>
</form>

<hr><h3>📋 Custom Payouts</h3>
<table><tr><th>Affiliate</th><th>Payout</th><th>Currency</th><th>Notes</th></tr>
<?php
foreach ($payouts as $p) {
    echo "<tr>
        <td>" . htmlspecialchars($p['username']) . " (" . htmlspecialchars($p['affiliate_id']) . ")</td>
        <td>" . htmlspecialchars($p['custom_payout']) . "</td>
        <td>" . htmlspecialchars($p['currency']) . "</td>
        <td><textarea readonly style='width:300px;height:40px'>" . htmlspecialchars($p['notes']) . "</textarea></td>
    </tr>";
}
?>
</table>
</div>
</body>
</html>