<?php
require_once '../includes/session.php';
require_once '../includes/constants.php';
require_once '../includes/functions.php';
require_once '../includes/allstripslashes.php';

$partners = new partners();
$partners->connection($host, $user, $pass, $db);

$member = $_POST['member'] ?? $_GET['member'] ?? 'affiliate';
$trans = $_POST['trans'] ?? $_GET['trans'] ?? '';
$page = intval($_GET['page'] ?? 0);
if (empty($page)) {
    $page = $partners->getpage();
}

$memberid = $member . "_id";
$flag = ($member === "merchant") ? 'm' : 'a';

$limit = $lines;
$offset = ($page - 1) * $lines;

$conditions = "adjust_flag = ? AND adjust_memberid = ?";
$params = [$flag, $memberid];
$types = "ss";

if (!empty($trans)) {
    $conditions .= " AND adjust_action = ?";
    $params[] = $trans;
    $types .= "s";
}

$sql = "
    SELECT *, DATE_FORMAT(adjust_date, '%d-%M-%Y') AS DATE
    FROM partners_adjustment, partners_$member
    WHERE $conditions
    ORDER BY adjust_date DESC
    LIMIT ?, ?
";

$params[] = $offset;
$params[] = $limit;
$types .= "ii";

$stmt = $partners->prepare($sql);
$stmt->bind_param($types, ...$params);
$stmt->execute();
$result = $stmt->get_result();
?>

<form method="post" action="index.php?Act=manual_payments&amp;page=1">
    <table width="90%" class="tablewbdr" align="center">
        <tr>
            <td height="25" colspan="2" align="center">
                Member
                <select name="member">
                    <option value="affiliate" <?= ($member === "affiliate") ? "selected" : "" ?>>Affiliate</option>
                    <option value="merchant" <?= ($member === "merchant") ? "selected" : "" ?>>Merchant</option>
                </select>
                &nbsp; Transaction
                <select name="trans">
                    <option value="">--All--</option>
                    <option value="add" <?= ($trans === "add") ? "selected" : "" ?>>Withdraw</option>
                    <option value="deduct" <?= ($trans === "deduct") ? "selected" : "" ?>>Payment</option>
                </select>
                <input type="submit" value="Search" name="s1">
            </td>
        </tr>
    </table>
</form>

<?php if ($result->num_rows > 0): ?>
    <br/>
    <table width="90%" class="tablebdr" align="center">
        <tr><td colspan="4" align="center" class="textred"></td></tr>
        <tr>
            <td class="tdhead" align="center"><?= ucfirst($member) ?></td>
            <td class="tdhead" align="center">Date Of Payment</td>
            <td class="tdhead" align="center">Transaction</td>
            <td class="tdhead" align="center">Amount</td>
        </tr>
        <?php
        $firstname = $member . "_firstname";
        $lastname = $member . "_lastname";

        while ($row = $result->fetch_object()):
            $fullname = trim(stripslashes($row->$firstname)) . ' ' . trim(stripslashes($row->$lastname));
            $date = $row->DATE;
            $amount = $row->adjust_amount;
            $action = ($row->adjust_action === "add") ? "Withdrawed from Admin A/C" : "Added To Admin A/C";
        ?>
            <tr>
                <td align="center"><?= htmlspecialchars($fullname) ?></td>
                <td align="center"><?= htmlspecialchars($date) ?></td>
                <td align="center"><?= htmlspecialchars($action) ?></td>
                <td align="center"><?= htmlspecialchars($amount) ?></td>
            </tr>
        <?php endwhile; ?>
        <tr>
            <td colspan="5" align="right">
                <?php
                $url = "index.php?Act=manual_payments&amp;member=$member&amp;trans=$trans";
                include '../includes/show_pagenos.php';
                ?>
            </td>
        </tr>
    </table>
<?php else: ?>
    <p class="textred" align="center">No Payments</p>
<?php endif; ?>
