<?php
ob_start();

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : '';
$group = isset($_REQUEST['group']) ? intval($_REQUEST['group']) : 0;

if ($mode === "ajax") {
    include '../includes/session.php';
    include '../includes/constants.php';
    include_once '../includes/functions.php';
    include '../includes/allstripslashes.php';

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

$commisionDetails = GetGroupCommission($group, $partners->conn);
$groupDetails = getGroupDetails($group, $partners->conn);

if ($group > 0 && $groupDetails):
?>
    <table cellpadding="0" cellspacing="0" border="0" align="center" width="100%">
        <tr>
            <td align="right" width="40%" height="30">Number of Levels</td>
            <td width="5%">&nbsp;:&nbsp;</td>
            <td align="left"><?php echo htmlspecialchars($groupDetails['level']); ?></td>
        </tr>

        <?php for ($i = 1; $i <= $groupDetails['level']; $i++): ?>
            <tr>
                <td align="right" width="40%" height="30"><b>Level <?php echo $i; ?></b></td>
                <td align="center" width="5%">&nbsp;:&nbsp;</td>
                <td align="left">
                    <?php
                    $commission = isset($commisionDetails[$i]['commission']) ? $commisionDetails[$i]['commission'] : '0';
                    $type = isset($commisionDetails[$i]['type']) ? $commisionDetails[$i]['type'] : '';
                    echo htmlspecialchars($commission) . '&nbsp;' . ($type == "percentage" ? "%" : "$");
                    ?>
                </td>
            </tr>
        <?php endfor; ?>
    </table>
<?php endif; ?>


<?php
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// FUNCTIONS
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// Get the commission details set for a group
function GetGroupCommission($groupId, $conn)
{
    $rows = [];
    $sql = "SELECT commission_amount, commission_type FROM partners_affiliategroup_commission WHERE commission_groupid = ?";
    $stmt = $conn->prepare($sql);
    if ($stmt) {
        $stmt->bind_param("i", $groupId);
        $stmt->execute();
        $stmt->bind_result($amount, $type);
        $i = 1;
        while ($stmt->fetch()) {
            $rows[$i]['commission'] = $amount;
            $rows[$i]['type'] = $type;
            $i++;
        }
        $stmt->close();
    }
    return $rows;
}

// Get the details of the selected Affiliate Group
function getGroupDetails($groupId, $conn)
{
    $sql = "SELECT affiliategroup_title, affiliategroup_levels FROM partners_affiliategroup WHERE affiliategroup_id = ?";
    $stmt = $conn->prepare($sql);
    if ($stmt) {
        $stmt->bind_param("i", $groupId);
        $stmt->execute();
        $stmt->bind_result($title, $levels);
        if ($stmt->fetch()) {
            $result = [
                'grouptitle' => $title,
                'level' => $levels
            ];
            $stmt->close();
            return $result;
        }
        $stmt->close();
    }
    return false;
}
?>
