<?php
/************************************************************************/
/*     PROGRAMMER     :  SMA                                            */
/*     SCRIPT NAME    :  AffiliateGroupList.php                         */
/*     CREATED ON     :  27/JULY/2009                                   */
/*	 	AffiliateGroup Management section. 								*/
/************************************************************************/

require_once "db_connect.php"; // assumes $mysqli is created in this file
?>

<br/>
<table border="0" cellpadding="0" cellspacing="0" class="tablebdr" width="65%" align="center">
    <tr>
        <td align="center" colspan="5" class="tdhead"><b>Existing Affiliate Groups</b></td>
    </tr>

    <?php
    if ($groupList = ListAllAffiliateGroups($mysqli)) {
    ?>
        <tr>
            <td width="45%" align="left" height="25"><b>Group Title</b></td>
            <td width="15%" align="left" height="25"><b>Levels</b></td>
            <td colspan="3" height="25">&nbsp;</td>
        </tr>
        <?php foreach ($groupList as $group): ?>
            <tr>
                <td align="left" height="25"><?= htmlspecialchars($group['groupTitle']) ?></td>
                <td align="left" height="25"><?= htmlspecialchars($group['groupLevels']) ?></td>
                <td width="20%" height="25">
                    <a href="index.php?Act=AffiliateGroup&mode=setCommission&groupId=<?= urlencode($group['groupId']) ?>">Set Commission</a>
                </td>
                <td width="10%" height="25">
                    <a href="index.php?Act=AffiliateGroup&mode=edit&groupId=<?= urlencode($group['groupId']) ?>">Edit</a>
                </td>
                <td width="10%" height="25">
                    <?php if ($group['groupId'] != '1'): ?>
                        <a href="#" onclick="return confirmDelete(<?= (int)$group['groupId'] ?>);">Delete</a>
                    <?php else: ?>
                        <b>Default</b>
                    <?php endif; ?>
                </td>
            </tr>
        <?php endforeach; ?>
    <?php
    } else {
    ?>
        <tr>
            <td align="center" colspan="5" class="textred" height="50" valign="middle"><b>No Affiliate Groups found</b></td>
        </tr>
    <?php
    }
    ?>
</table>

<script>
function confirmDelete(groupId) {
    if (confirm("Are you sure to delete this group?")) {
        window.location = "index.php?Act=AffiliateGroup&mode=delete&groupId=" + groupId;
        return true;
    }
    return false;
}
</script>

<?php
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	FUNCTION: ListAllAffiliateGroups
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function ListAllAffiliateGroups($mysqli)
{
    $sql = "SELECT affiliategroup_id, affiliategroup_title, affiliategroup_levels FROM partners_affiliategroup";
    $result = $mysqli->query($sql);

    if ($result && $result->num_rows > 0) {
        $rows = [];
        while ($row = $result->fetch_assoc()) {
            $rows[] = [
                'groupId'      => $row['affiliategroup_id'],
                'groupTitle'   => stripslashes($row['affiliategroup_title']),
                'groupLevels'  => $row['affiliategroup_levels'],
            ];
        }
        return $rows;
    }
    return false;
}
?>
