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

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

$flag = isset($_GET['flag']) ? $_GET['flag'] : '';
$id = $_SESSION['AFFILIATEID'];
$name = $_SESSION['AFFILIATENAME'];
$msg = isset($_GET['msg']) ? htmlspecialchars($_GET['msg']) : '';

if (empty($page)) {
    $page = $partners->getpage();
}

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

// Get all approved transactions with no admin payment date
$query = "
    SELECT t.*, j.joinpgm_affiliateid, j.joinpgm_merchantid
    FROM partners_transaction AS t
    INNER JOIN partners_joinpgm AS j ON t.transaction_joinpgmid = j.joinpgm_id
    WHERE t.transaction_status = 'approved' AND t.transaction_adminpaydate = '0000-00-00'
    LIMIT ?, ?
";

$stmt = $partners->mysqli->prepare($query);
$stmt->bind_param("ii", $offset, $lines);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0):
?>
<br/>
<table width="90%" class="tablebdr" align="center">
    <tr>
        <td colspan="5" align="center" class="textred"><?php echo $msg; ?></td>
    </tr>
    <tr>
        <td height="25" class="tdhead" align="center">Affiliate</td>
        <td height="25" class="tdhead" align="center">Merchant</td>
        <td height="25" class="tdhead" align="center">Transaction</td>
        <td height="25" class="tdhead" align="center">Amount</td>
        <td height="25" class="tdhead" align="center">Do Payment</td>
    </tr>
    <?php
    while ($row = $result->fetch_object()):
        $image = '';
        if ($row->transaction_status === 'reversed') {
            $image = "<img src='../images/reversesale.gif' height='15' width='15' alt='Reversed'>";
        }

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

        // Get affiliate name
        $affiliate = '';
        $affiliate_stmt = $partners->mysqli->prepare("SELECT affiliate_firstname, affiliate_lastname FROM partners_affiliate WHERE affiliate_id = ?");
        $affiliate_stmt->bind_param("i", $row->joinpgm_affiliateid);
        $affiliate_stmt->execute();
        $affiliate_result = $affiliate_stmt->get_result();
        if ($affiliate_result->num_rows > 0) {
            $a = $affiliate_result->fetch_object();
            $affiliate = htmlspecialchars(stripslashes($a->affiliate_firstname . " " . $a->affiliate_lastname));
        }
        $affiliate_stmt->close();
        ?>
        <tr>
            <td height="25" align="center"><?php echo $affiliate; ?></td>
            <td height="25" align="center"><?php echo $merchant; ?></td>
            <td height="25" align="center"><?php echo htmlspecialchars($row->transaction_type) . $image; ?></td>
            <td height="25" align="center"><?php echo htmlspecialchars($row->transaction_admin_amount); ?></td>
            <td height="25" align="center">
                <a href="adminpayment_process.php?affiliateid=<?php echo urlencode($row->joinpgm_affiliateid); ?>&merchantid=<?php echo urlencode($row->joinpgm_merchantid); ?>&transid=<?php echo urlencode($row->transaction_id); ?>&amount=<?php echo urlencode($row->transaction_admin_amount); ?>">doPayments</a>
            </td>
        </tr>
    <?php endwhile; ?>
    <tr>
        <td height="25" colspan="5" align="right">
            <?php
            $url = "index.php?Act=admin_payments";
            include '../includes/show_pagenos.php';
            ?>
        </td>
    </tr>
</table>
<?php else: ?>
    <p class="textred" align="center">No Pending Payments</p>
<?php endif; ?>
