<?php
// ************************************************************************
// PROGRAMMER     :  SMA
// SCRIPT NAME    :  graph_distribution.php
// CREATED ON     :  05/JULY/2006
// PURPOSE        :  Builds distribution report based on selected merchant
// ************************************************************************

require_once 'graphs.php'; // Ensure this is the modernized class

$MERCHANTID = isset($_REQUEST['merchants']) ? intval($_REQUEST['merchants']) : 0;
$type = isset($_REQUEST['type']) && in_array($_REQUEST['type'], ['click', 'sale', 'commission']) ? $_REQUEST['type'] : 'click';

$graphobj = new graphs();
$result = $graphobj->GetAllMerchants();
?>
<form name="Distribution" method="post" action="">
    <table border="0" cellpadding="0" cellspacing="0" class="tablebdr" align="center" width="60%">
        <tr>
            <td colspan="2" align="center" width="100%">
                <table border="0" cellpadding="0" cellspacing="0" align="center" width="100%">
                    <tr>
                        <td align="center" class="tdhead" colspan="2" height="25" width="95%">
                            <b>Distribution Report</b>
                        </td>
                        <td align="right" class="tdhead" width="5%">
                            <a href="#" onclick="popWin('../help_distribution.php?user=Admin');">
                                <img src="../images/help.jpg" border="0" alt="Help">&nbsp;Help&nbsp;
                            </a>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr><td colspan="2">&nbsp;</td></tr>

        <tr>
            <td width="50%" align="right" height="25">Merchant&nbsp;&nbsp;&nbsp;&nbsp;</td>
            <td width="50%" align="left" height="25">
                <select name="merchants" onchange="document.Distribution.submit()">
                    <option value="">Select One Merchant</option>
                    <?php if ($result): ?>
                        <?php for ($i = 0; $i < count($graphobj->merId); $i++): ?>
                            <option value="<?= $graphobj->merId[$i] ?>" <?= ($MERCHANTID === (int)$graphobj->merId[$i]) ? "selected" : "" ?>>
                                <?= htmlspecialchars($graphobj->merCompany[$i]) ?>
                            </option>
                        <?php endfor; ?>
                    <?php endif; ?>
                </select>
            </td>
        </tr>

        <tr>
            <td align="right" height="25">Action&nbsp;&nbsp;&nbsp;&nbsp;</td>
            <td align="left" height="25">
                <select name="type" onchange="document.Distribution.submit()">
                    <option value="click" <?= ($type === 'click') ? 'selected' : '' ?>>Click</option>
                    <option value="sale" <?= ($type === 'sale') ? 'selected' : '' ?>>Sale</option>
                    <option value="commission" <?= ($type === 'commission') ? 'selected' : '' ?>>Commission</option>
                </select>
            </td>
        </tr>

        <tr><td colspan="2">&nbsp;</td></tr>
    </table>
    <br />

    <?php if ($MERCHANTID > 0): ?>
        <?php
        $graphobj->FindPerformanceAffiliateGroup($MERCHANTID, $type);
        $total = $graphobj->total;

        $affiliate10 = $graphobj->groupCount[0] ?? 0;
        $affiliate20 = $graphobj->groupCount[1] ?? 0;
        $affiliate50 = $graphobj->groupCount[2] ?? 0;
        $affiliate75 = $graphobj->groupCount[3] ?? 0;
        $affiliateAbove = $graphobj->groupCount[4] ?? 0;

        $affiliate10Percent = $affiliate20Percent = $affiliate50Percent = $affiliate75Percent = $affiliateAbovePercent = 0;

        if ($total > 0) {
            $affiliate10Percent = ($affiliate10 * 100) / $total;
            $affiliate20Percent = ($affiliate20 * 100) / $total;
            $affiliate50Percent = ($affiliate50 * 100) / $total;
            $affiliate75Percent = ($affiliate75 * 100) / $total;
            $affiliateAbovePercent = ($affiliateAbove * 100) / $total;
        }

        // Convert percentages to angle degrees (for pie chart)
        $angle10 = ($affiliate10Percent * 360) / 100;
        $angle20 = $angle10 + ($affiliate20Percent * 360) / 100;
        $angle50 = $angle20 + ($affiliate50Percent * 360) / 100;
        $angle75 = $angle50 + ($affiliate75Percent * 360) / 100;
        $angleAbove = $angle75 + ($affiliateAbovePercent * 360) / 100;
        ?>

        <table border="0" cellpadding="0" cellspacing="0" class="tablebdr" align="center" width="80%">
            <tr>
                <td width="80%" align="center">
                    <table width="100%" align="center">
                        <tr>
                            <td>
                                <table cellpadding="0" cellspacing="5" align="left">
                                    <tr>
                                        <td><b>Colors</b></td>
                                        <td><b>Percentage</b></td>
                                        <td><b>Color Description</b></td>
                                    </tr>
                                    <tr>
                                        <td bgcolor="#cc0033" width="50" height="15">&nbsp;</td>
                                        <td align="center"><?= round($affiliate10Percent, 2) ?></td>
                                        <td>Affiliates 1–10</td>
                                    </tr>
                                    <tr>
                                        <td bgcolor="#ffcc00" width="50" height="15">&nbsp;</td>
                                        <td align="center"><?= round($affiliate20Percent, 2) ?></td>
                                        <td>Affiliates 11–20</td>
                                    </tr>
                                    <tr>
                                        <td bgcolor="#3300ff" width="50" height="15">&nbsp;</td>
                                        <td align="center"><?= round($affiliate50Percent, 2) ?></td>
                                        <td>Affiliates 21–50</td>
                                    </tr>
                                    <tr>
                                        <td bgcolor="#009900" width="50" height="15">&nbsp;</td>
                                        <td align="center"><?= round($affiliate75Percent, 2) ?></td>
                                        <td>Affiliates 51–75</td>
                                    </tr>
                                    <tr>
                                        <td bgcolor="#00ccff" width="50" height="15">&nbsp;</td>
                                        <td align="center"><?= round($affiliateAbovePercent, 2) ?></td>
                                        <td>Affiliates from 75</td>
                                    </tr>
                                    <tr>
                                        <td bgcolor="#C0C0C0" width="50" height="15">&nbsp;</td>
                                        <td align="center"><?= ($total == 0) ? "100" : "0" ?></td>
                                        <td>Affiliates not performed the selected Action</td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td align="center">
                                <img src="distributionreport.php?alt=0&day1=<?= round($angle10) ?>&day15=<?= round($angle20) ?>&month1=<?= round($angle50) ?>&month2=<?= round($angle75) ?>&total=<?= round($angleAbove) ?>" alt="Distribution Chart" border="0">
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    <?php endif; ?>
</form>
<br>

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