<?php
// Include database connection
require_once("db_connect.php"); // Make sure this connects and creates $mysqli

// Get and sanitize input
$id = intval($_GET['affid'] ?? 0);
$msg = htmlspecialchars($_GET['msg'] ?? '');
$amount = htmlspecialchars($_POST['amount'] ?? '');
$act = $_POST['action'] ?? 'add';

// Default current amount
$curr_amount = 0.00;

// Query current affiliate pay amount
$stmt = $mysqli->prepare("SELECT pay_amount FROM affiliate_pay WHERE pay_affiliateid = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->bind_result($pay_amount);

if ($stmt->fetch()) {
    $curr_amount = floatval($pay_amount);
}
$stmt->close();
?>

<form name="adjust" action="adjust_process.php?id=<?= $id ?>" method="post">
    <table width="50%" class="tablebdr" align="center">
        <tr>
            <td height="25" colspan="2" align="center" class="tdhead">Adjust Money - Affiliate</td>
        </tr>
        <tr>
            <td height="25" colspan="2" align="center" class="textred"><?= $msg ?></td>
        </tr>
        <tr>
            <td width="50%" height="25">Current Amount</td>
            <td width="50%" height="25" align="left">: <?= round($curr_amount, 2) ?></td>
        </tr>
        <tr>
            <td height="25">Amount</td>
            <td height="25" align="left">:
                <input type="text" name="amount" value="<?= htmlspecialchars($amount) ?>" />
            </td>
        </tr>
        <tr>
            <td height="25">Action</td>
            <td height="25" align="left">:
                <select name="action">
                    <option value="add" <?= ($act === 'add') ? "selected='selected'" : "" ?>>Add</option>
                    <option value="deduct" <?= ($act === 'deduct') ? "selected='selected'" : "" ?>>Deduct</option>
                </select>
            </td>
        </tr>
        <tr>
            <td height="25" colspan="2" align="center">
                <input type="submit" name="submit" value="Adjust Money" />
            </td>
        </tr>
    </table>
</form>
