<?php
// Include DB connection
require_once("db_connect.php"); // Ensure this sets up $mysqli

// Sanitize input
$id = intval($_GET['merid'] ?? 0);
$msg = htmlspecialchars($_GET['msg'] ?? '');
$amount = htmlspecialchars($_POST['amount'] ?? '');
$act = $_POST['action'] ?? 'add';

$curr_amount = 0.00;

// Fetch current merchant pay amount
$stmt = $mysqli->prepare("SELECT pay_amount FROM merchant_pay WHERE pay_merchantid = ?");
$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_process1.php?id=<?= $id ?>" method="post">
    <table width="50%" class="tablebdr" align="center">
        <tr>
            <td height="25" colspan="2" class="tdhead heading-3" style="text-align:center">Adjust Money - Merchant</td>
        </tr>
        <tr>
            <td height="25" colspan="2" align="center" class="textred"><?= $msg ?></td>
        </tr>
        <tr>
            <td width="51%" height="25">Current Amount</td>
            <td width="49%" 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="<?= $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>
<br />
