<?php
require_once 'includes/common.php'; // Include DB, class autoload, session check, etc.

$MERCHANTID = isset($_REQUEST['merchants']) ? intval(trim($_REQUEST['merchants'])) : 0;
$programId = isset($_REQUEST['programs']) ? trim($_REQUEST['programs']) : 'All';
$type = isset($_REQUEST['type']) && $_REQUEST['type'] !== '' ? $_REQUEST['type'] : 'click';

$from = isset($_REQUEST['txtfrom']) ? trim($_REQUEST['txtfrom']) : '';
$to = isset($_REQUEST['txtto']) ? trim($_REQUEST['txtto']) : '';

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

$graphobj = new graphs();
$result = $graphobj->GetAllMerchants();
?>
<script type="text/javascript">
    function from_date() {
        gfPop.fStartPop(document.Distribution.txtfrom, Date);
    }

    function to_date() {
        gfPop.fStartPop(document.Distribution.txtto, Date);
    }

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

<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>

<form name="Distribution" method="post" action="">
    <table class="tablebdr" align="center" width="60%" cellpadding="0" cellspacing="0" border="0">
        <tr>
            <td colspan="2" align="center">
                <table width="100%" cellpadding="0" cellspacing="0">
                    <tr>
                        <td class="tdhead" colspan="2" height="25" align="center"><strong>Bubble Plot</strong></td>
                        <td class="tdhead" align="right">
                            <a href="#" onclick="popWin('../help_bubblePlot.php?user=Admin');">
                                <img src="../images/help.jpg" border="0" alt="Help"> Help
                            </a>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>

        <!-- Date Range -->
        <tr>
            <td align="center" width="50%">
                <table align="center" width="100%" cellpadding="0" cellspacing="0">
                    <tr><td colspan="2" height="30" align="center"><strong>For Period</strong></td></tr>
                    <tr>
                        <td align="right" width="50%">From&nbsp;&nbsp;&nbsp;&nbsp;</td>
                        <td align="left"><input type="text" name="txtfrom" size="18" value="<?php echo 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="<?php echo htmlspecialchars($to); ?>" onfocus="to_date(); return false;" /></td>
                    </tr>
                </table>
            </td>

            <!-- Merchant / Type / Program -->
            <td width="50%" align="center" valign="top">
                <table align="center" width="100%" cellpadding="0" cellspacing="0">
                    <tr><td colspan="2">&nbsp;</td></tr>
                    <tr>
                        <td align="right" width="50%"><strong>Merchant</strong>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                        <td align="left">
                            <select name="merchants" onchange="document.Distribution.submit()">
                                <option value="">Select One Merchant</option>
                                <?php
                                if ($result) {
                                    foreach ($graphobj->merId as $i => $merId) {
                                        $selected = ($MERCHANTID == $merId) ? 'selected' : '';
                                        echo "<option value=\"$merId\" $selected>" . htmlspecialchars($graphobj->merCompany[$i]) . "</option>";
                                    }
                                }
                                ?>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td align="right"><strong>Action</strong>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                        <td align="left">
                            <select name="type" onchange="document.Distribution.submit()">
                                <option value="click" <?php if ($type === 'click') echo 'selected'; ?>>Click</option>
                                <option value="sale" <?php if ($type === 'sale') echo 'selected'; ?>>Sale</option>
                            </select>
                        </td>
                    </tr>
                    <?php
                    if ($MERCHANTID) {
                        $result_prg = $graphobj->GetAllPrograms($MERCHANTID);
                        ?>
                        <tr>
                            <td align="right"><strong>Programs</strong>&nbsp;&nbsp;&nbsp;&nbsp;</td>
                            <td align="left">
                                <select name="programs" onchange="document.Distribution.submit()">
                                    <option value="All">All Programs</option>
                                    <?php
                                    if ($result_prg) {
                                        foreach ($graphobj->pgmId as $i => $pgmId) {
                                            $selected = ($programId == $pgmId) ? 'selected' : '';
                                            echo "<option value=\"$pgmId\" $selected>" . htmlspecialchars($graphobj->pgmName[$i]) . "</option>";
                                        }
                                    }
                                    ?>
                                </select>
                            </td>
                        </tr>
                        <?php
                    }
                    ?>
                </table>
            </td>
        </tr>

        <!-- Submit Button -->
        <tr>
            <td colspan="2" align="center" height="25">
                <input type="submit" name="sub" value="View" />
            </td>
        </tr>
    </table>

    <br>

    <?php
    if ($MERCHANTID) {
        $graphobj->GetBubbleReport($MERCHANTID, $programId, $fromDate, $toDate, $type);
        $affiliate_count = $graphobj->rowcount;
        $maxClick = $graphobj->total;
        $maxValue = $graphobj->maxcomm;

        $clicks = $comms = $name = '';

        for ($i = 0; $i < count($graphobj->affId); $i++) {
            $affiliateClick = $graphobj->affClick[$i];
            $affiliateCompany = $graphobj->affCompany[$i];
            $affiliateCommission = $graphobj->affCommn[$i];

            $clickPercent = ($maxClick > 0) ? round(($affiliateClick * 100) / $maxClick, 2) : 0;
            $commPercent = ($maxValue > 0) ? round(($affiliateCommission * 100) / $maxValue, 2) : 0;

            $clicks .= $clickPercent . ",";
            $comms .= $commPercent . ",";
            $name .= $affiliateCompany . ",";
        }

        $clicks = rtrim($clicks, ",");
        $comms = rtrim($comms, ",");
        $name = rtrim($name, ",");
        $xAxis = $type . " made by the Affiliates for the selected Program";
        ?>
        <table class="tablebdr" align="center" width="95%" cellpadding="0" cellspacing="0" border="0">
            <?php if ($affiliate_count > 0): ?>
                <tr>
                    <td colspan="2">
                        <table cellpadding="0" cellspacing="5" align="left">
                            <tr><td><strong>Bubble Plot Description</strong></td></tr>
                            <tr><td><img src="../images/help.jpg" border="0"> The clicks or sales made by the Affiliate is represented along the X-axis</td></tr>
                            <tr><td><img src="../images/help.jpg" border="0"> The Affiliates joined for the selected Program is represented along the Y-axis</td></tr>
                            <tr><td><img src="../images/help.jpg" border="0"> The commission earned by the Affiliate is represented by the diameter of the bubble</td></tr>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <img src="bubble_plot.php?rows=<?php echo urlencode($affiliate_count); ?>&maxComm=<?php echo urlencode($maxValue); ?>&click=<?php echo urlencode($clicks); ?>&comm=<?php echo urlencode($comms); ?>&name=<?php echo urlencode($name); ?>&type=<?php echo urlencode($xAxis); ?>" border="0" alt="Bubble Plot" />
                    </td>
                </tr>
            <?php else: ?>
                <tr><td colspan="2" class="textred" align="center">Sorry! Merchant has no Affiliates for the selected program</td></tr>
            <?php endif; ?>
            <tr><td>&nbsp;</td></tr>
        </table>
        <?php
    }
    ?>
</form>
<br />
<script language="javascript" type="text/javascript">
	function popWin(url)
	{
		nw = open(url,'new','height=400,width=400,scrollbars=yes');
		nw.focus();
	}
</script>			