<?php
/**************************************************************************/
/*     AUTHOR         :  PHP                                              */
/*     PROGRAMMER     :  SMA                                              */
/*     SCRIPT NAME    :  affiliateReferrals.php                           */
/*     CREATED ON     :  20/AUG/2009                                      */
/*                                                                        */
/*     DESCRIPTION     :  Affiliate Referrals                             */
/**************************************************************************/

require_once '../includes/config.php'; // Ensure DB connection and session start
require_once '../includes/functions.php'; // Any shared global functions

$page = isset($page) ? intval($page) : $partners->getpage(); // Get current page
$offset = ($page - 1) * $lines;

echo '<br />
<table width="100%" align="center">
    <tr>
        <td width="100%" align="center" class="tdhead"><b>Affiliate Referrals</b></td>
    </tr>
</table>';

$sql_count = "SELECT DISTINCT affiliate_parentid FROM partners_affiliate WHERE affiliate_parentid != 0";
$res_page = mysqli_query($conn, $sql_count);
$_SESSION['SESS_TOTALCOUNT'] = mysqli_num_rows($res_page);

$sql = $sql_count . " LIMIT $offset, $lines";
$res = mysqli_query($conn, $sql);

if (mysqli_num_rows($res)) {
?>
    <table border="0" cellpadding="1" cellspacing="1" width="90%" align="center" class="tablebdr">
        <tr>
            <td width="43%" class="tdhead" align="center"><b>Affiliate</b></td>
            <td width="34%" class="tdhead" align="center"><b>Join Date</b></td>
            <td width="23%" class="tdhead" align="center"><b>Referral Count</b></td>
            <td width="23%" class="tdhead" align="center">&nbsp;</td>
        </tr>
<?php
    $i = 0;
    while ($row = mysqli_fetch_object($res)) {
        $affiliateParentid = intval($row->affiliate_parentid);

        // Get affiliate parent info
        $sqlAffiliate = "SELECT * FROM partners_affiliate WHERE affiliate_id = $affiliateParentid";
        $resAffiliate = mysqli_query($conn, $sqlAffiliate);
        if (!$resAffiliate || mysqli_num_rows($resAffiliate) == 0) continue;

        $rowAffiliate = mysqli_fetch_object($resAffiliate);
        $affiliateId = $rowAffiliate->affiliate_id;
        $affiliateCompany = htmlspecialchars($rowAffiliate->affiliate_company);
        $joinDate = htmlspecialchars($rowAffiliate->affiliate_date);

        // Get referral count
        $sqlReferral = "SELECT COUNT(affiliate_id) AS referralCount FROM partners_affiliate WHERE affiliate_parentid = $affiliateParentid";
        $resReferral = mysqli_query($conn, $sqlReferral);
        $rowReferral = mysqli_fetch_object($resReferral);
        $referralCount = intval($rowReferral->referralCount);

        $classid = ($i % 2 == 1) ? "grid2" : "grid1";
?>
        <tr class="<?= $classid ?>">
            <td width="43%" height="25" align="left" style="padding-left:5px;">
                <a href="#" onclick="viewLink(<?= $affiliateId ?>)"><?= $affiliateCompany ?></a>
            </td>
            <td width="34%" height="25" align="center"><?= $joinDate ?></td>
            <td width="23%" height="25" align="center"><?= $referralCount ?></td>
            <td width="23%" height="25" align="center">
                <a href="index.php?Act=referralDetails&affiliateParentid=<?= $affiliateParentid ?>&company=<?= urlencode($affiliateCompany) ?>">Details</a>
            </td>
        </tr>
<?php
        $i++;
    }
?>
        <tr>
            <td colspan="4" align="center">
                <?php include '../includes/paging.php'; ?>
            </td>
        </tr>
    </table>
<?php
} else {
?>
    <table width="100%" align="center">
        <tr>
            <td align="center" class="textred">No Records Found</td>
        </tr>
    </table>
<?php
}
?>

<script language="javascript" type="text/javascript">
    function viewLink(affiliateid) {
        var url = "viewprofile_affiliate.php?id=" + affiliateid;
        var nw = window.open(url, 'new', 'height=450,width=450,scrollbars=no');
        nw.focus();
    }
</script>
