<?php
declare(strict_types=1);
require_once 'graphs.php';
require_once 'partners.php';

$MERCHANTID = isset($_REQUEST['merchants']) ? intval($_REQUEST['merchants']) : 0;
$programId  = isset($_REQUEST['programs']) ? intval($_REQUEST['programs']) : 0;

$from = $_REQUEST['txtfrom'] ?? '';
$to   = $_REQUEST['txtto'] ?? '';

$partners = new partners();
$fromDate = $toDate = '';

if ($from !== '' && $to !== '') {
    $fromDate = $partners->date2mysql($from);
    $toDate   = $partners->date2mysql($to);
}

if (empty($programId)) {
    $programId = 'All';
}

$graphobj = new graphs();
$result = $graphobj->GetAllMerchants();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Affiliate At a Glance</title>
</head>
<body>
<iframe width="168" height="175" name="gToday:normal:agenda.js" id="gToday:normal:agenda.js" src="../cal/ipopeng.htm" scrolling="no" frameborder="0" style="border:2px ridge; visibility:visible; z-index:999; position:absolute; left:-500px; top:0px;">
</iframe>

<br>
<form name="Affiliate" method="post" action="">
    <table border="0" cellpadding="0" cellspacing="0" class="tablebdr" align="center" width="60%">
        <tr>
            <td colspan="2" align="center">
                <table border="0" cellpadding="0" cellspacing="0" width="100%">
                    <tr>
                        <td align="center" class="tdhead"><b>Affiliate At a Glance</b></td>
                        <td align="right" class="tdhead">
                            <a href="#" onclick="popWin('../help_affiliateGlance.php?user=Admin');">
                                <img src="../images/help.jpg" border="0" alt="Help">&nbsp;Help&nbsp;
                            </a>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>

        <tr>
            <td width="50%" align="center">
                <table cellpadding="0" cellspacing="0" width="100%">
                    <tr><td colspan="2" align="center"><b>For Period</b></td></tr>
                    <tr>
                        <td align="right">From&nbsp;&nbsp;&nbsp;&nbsp;</td>
                        <td align="left"><input type="text" name="txtfrom" size="18" value="<?= htmlspecialchars($from) ?>" onfocus="from_date(); return false;"></td>
                    </tr>
                    <tr>
                        <td align="right">To&nbsp;&nbsp;&nbsp;&nbsp;</td>
                        <td align="left"><input type="text" name="txtto" size="18" value="<?= htmlspecialchars($to) ?>" onfocus="to_date(); return false;"></td>
                    </tr>
                    <tr><td colspan="2">&nbsp;</td></tr>
                    <tr>
                        <td colspan="2" align="center">
                            <input name="currValue" type="hidden" value="">
                            <input type="submit" name="sub" value="View">
                        </td>
                    </tr>
                </table>
            </td>
            <td width="50%" align="center" valign="top">
                <table cellpadding="0" cellspacing="0" width="100%">
                    <tr><td colspan="2">&nbsp;</td></tr>
                    <tr>
                        <td align="right"><b>Merchant</b>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                        <td align="left">
                            <select name="merchants" onchange="document.Affiliate.submit()">
                                <option value="">Select One Merchant</option>
                                <?php
                                if ($result) {
                                    foreach ($graphobj->merId as $i => $id) {
                                        $selected = ($MERCHANTID == $id) ? 'selected' : '';
                                        echo "<option value=\"$id\" $selected>" . htmlspecialchars($graphobj->merCompany[$i]) . "</option>";
                                    }
                                }
                                ?>
                            </select>
                        </td>
                    </tr>
                    <?php
                    if ($MERCHANTID !== 0) {
                        $result_prg = $graphobj->GetAllPrograms($MERCHANTID);
                        ?>
                        <tr>
                            <td align="right"><b>Programs</b>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                            <td align="left">
                                <select name="programs" onchange="document.Affiliate.submit()">
                                    <option value="All">All Programs</option>
                                    <?php
                                    if ($result_prg) {
                                        foreach ($graphobj->pgmId as $i => $id) {
                                            $selected = ($programId == $id) ? 'selected' : '';
                                            echo "<option value=\"$id\" $selected>" . htmlspecialchars($graphobj->pgmName[$i]) . "</option>";
                                        }
                                    }
                                    ?>
                                </select>
                            </td>
                        </tr>
                        <?php
                    }
                    ?>
                </table>
            </td>
        </tr>
    </table>
    <br>

    <?php if ($MERCHANTID !== 0): ?>
        <?php
        $graphobj->FindAffiliateClickPercentage($MERCHANTID, $programId, $fromDate, $toDate);
        $rowcount = $graphobj->rowcount;
        $total = $graphobj->total;
        $totalSale = $graphobj->total_sale;
        ?>
        <table border="0" cellpadding="0" cellspacing="0" class="tablebdr" align="center" width="95%">
            <tr>
                <td colspan="2" align="center">
                    <table width="100%" align="center">
                        <?php if ($rowcount > 0): ?>
                            <tr>
                                <td>
                                    <table width="100" align="left">
                                        <tr><td><b>Affiliate</b></td></tr>
                                    </table>
                                    <table cellpadding="0" cellspacing="2" align="left" width="300">
                                        <tr><td align="center"><b>Click Representation</b></td></tr>
                                    </table>
                                    <table cellpadding="0" cellspacing="2" align="center" width="300">
                                        <tr><td align="center"><b>Sales Representation</b></td></tr>
                                    </table>
                                </td>
                            </tr>
                            <?php foreach ($graphobj->affId as $i => $affId): ?>
                                <?php
                                $affiliateClick = $graphobj->affClick[$i];
                                $affiliateCompany = htmlspecialchars($graphobj->affCompany[$i]);

                                $clickPercent = ($total > 0) ? ($affiliateClick * 100) / $total : 0;
                                $clickInTable = (300 * $clickPercent) / 100;
                                $balance = 300 - $clickInTable;

                                $affiliateSale = $graphobj->affSale[$i];
                                $salePercent = ($totalSale > 0) ? ($affiliateSale * 100) / $totalSale : 0;
                                $saleInTable = (300 * $salePercent) / 100;
                                $balanceSale = 300 - $saleInTable;
                                ?>
                                <table width="100" align="left" cellpadding="0" cellspacing="1">
                                    <tr><td align="center"><?= $affiliateCompany ?></td></tr>
                                </table>
                                <table cellpadding="0" cellspacing="1" align="left" width="300" bgcolor="silver">
                                    <tr>
                                        <td width="<?= (int)$clickInTable ?>" bgcolor="#000099" style="color:#FFFFFF;" height="15">
                                            <?= ($clickInTable > 0) ? $affiliateClick : '' ?>
                                        </td>
                                        <td width="<?= (int)$balance ?>" bgcolor="silver"></td>
                                    </tr>
                                </table>
                                <table cellpadding="0" cellspacing="1" align="center" width="300" bgcolor="silver">
                                    <tr>
                                        <td width="<?= (int)$saleInTable ?>" bgcolor="#000099" style="color:#FFFFFF;" height="15">
                                            <?= ($saleInTable > 0) ? $affiliateSale : '' ?>
                                        </td>
                                        <td width="<?= (int)$balanceSale ?>" bgcolor="silver"></td>
                                    </tr>
                                </table>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <tr><td class="textred">Sorry! Merchant has no Affiliates for the selected program</td></tr>
                        <?php endif; ?>
                    </table>
                </td>
            </tr>
            <tr><td colspan="2">&nbsp;</td></tr>
        </table>
    <?php endif; ?>
</form>
<br>

<script>
    function popWin(url) {
        let nw = window.open(url, 'new', 'height=400,width=400,scrollbars=yes');
        nw.focus();
    }

    function from_date() {
        gfPop.fStartPop(document.Affiliate.txtfrom, Date);
    }

    function to_date() {
        gfPop.fStartPop(document.Affiliate.txtto, Date);
    }
</script>
</body>
</html>
