<?php
session_start();
require_once 'db.php'; // assumes $conn is defined here

$flag = $_GET['flag'] ?? '';
$msg  = $_GET['msg'] ?? '';
$id   = $_SESSION['AFFILIATEID'] ?? '';
$name = $_SESSION['AFFILIATENAME'] ?? '';

$query = "
    SELECT t.*, j.joinpgm_affiliateid, j.joinpgm_merchantid 
    FROM partners_transaction t 
    JOIN partners_joinpgm j ON t.transaction_joinpgmid = j.joinpgm_id 
    WHERE t.transaction_status = 'approved' 
    AND t.transaction_dateofpayment = '0000-00-00'
";

$result = $conn->query($query);
if (!$result) {
    die("Database query failed: " . $conn->error);
}

if ($result->num_rows > 0): ?>
    <br/>
    <table width="90%" class="tablebdr" align="center">
        <tr>
            <td colspan="5" align="center" class="textred"><?= htmlspecialchars($msg) ?></td>
        </tr>
        <tr>
            <td class="tdhead" align="center">Affiliate</td>
            <td class="tdhead" align="center">Merchant</td>
            <td class="tdhead" align="center">Transaction</td>
            <td class="tdhead" align="center">Amount</td>
            <td class="tdhead" align="center">Do Payment</td>
        </tr>

        <?php while ($row = $result->fetch_object()):
            $image = ($row->transaction_status === 'reversed') 
                ? "<img src='../images/reversesale.gif' height='15' width='15'>" 
                : "";

            // Get merchant name
            $merchant = '';
            $stmtMerchant = $conn->prepare("SELECT merchant_firstname, merchant_lastname FROM partners_merchant WHERE merchant_id = ?");
            $stmtMerchant->bind_param("i", $row->joinpgm_merchantid);
            $stmtMerchant->execute();
            $resMerchant = $stmtMerchant->get_result();
            if ($resMerchant->num_rows > 0) {
                $m = $resMerchant->fetch_object();
                $merchant = htmlspecialchars($m->merchant_firstname . ' ' . $m->merchant_lastname);
            }

            // Get affiliate name
            $affiliate = '';
            $stmtAff = $conn->prepare("SELECT affiliate_firstname, affiliate_lastname FROM partners_affiliate WHERE affiliate_id = ?");
            $stmtAff->bind_param("i", $row->joinpgm_affiliateid);
            $stmtAff->execute();
            $resAff = $stmtAff->get_result();
            if ($resAff->num_rows > 0) {
                $a = $resAff->fetch_object();
                $affiliate = htmlspecialchars($a->affiliate_firstname . ' ' . $a->affiliate_lastname);
            }

            ?>
            <tr>
                <td align="center"><?= $affiliate ?></td>
                <td align="center"><?= $merchant ?></td>
                <td align="center"><?= htmlspecialchars($row->transaction_type) . $image ?></td>
                <td align="center"><?= htmlspecialchars($row->transaction_amttobepaid) ?></td>
                <td align="center">
                    <form action="payment_process.php" method="POST" style="margin:0;">
                        <input type="hidden" name="affiliateid" value="<?= htmlspecialchars($row->joinpgm_affiliateid) ?>">
                        <input type="hidden" name="merchantid" value="<?= htmlspecialchars($row->joinpgm_merchantid) ?>">
                        <input type="hidden" name="transid" value="<?= htmlspecialchars($row->transaction_id) ?>">
                        <input type="hidden" name="amount" value="<?= htmlspecialchars($row->transaction_amttobepaid) ?>">
                        <button type="submit">Do Payment</button>
                    </form>
                </td>
            </tr>
        <?php endwhile; ?>
    </table>
<?php else: ?>
    <p class="textred" align="center">No Pending Payments</p>
<?php endif; ?>
