<?php
ob_start();

require_once '../includes/session.php';
require_once '../includes/constants.php';
require_once '../includes/functions.php';
require_once '../includes/allstripslashes.php';

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

if (!$partners->islogin()) {
    include "admin_login.php";
    exit;
}

$affiliateId = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
$page        = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 0;
$status      = isset($_REQUEST['status']) ? $_REQUEST['status'] : '';
$sortby      = isset($_REQUEST['sortby']) ? $_REQUEST['sortby'] : '';
$mode        = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : '';
$msg         = isset($_REQUEST['msg']) ? $_REQUEST['msg'] : '';
$message     = $msg;

if ($msg == "approved") {
    $msg = "Affiliate Account Approved and Tier Commission Group Assigned";
} elseif ($msg == "assigned") {
    $msg = "Tier Commission Group Assigned to Affiliate";
}

$affiliate = '';
$group = 0;

// Get current affiliate info
$stmt = $partners->db->prepare("SELECT * FROM partners_affiliate WHERE affiliate_id = ?");
$stmt->bind_param("i", $affiliateId);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
    $row = $result->fetch_object();
    $group = $row->affiliate_group;
    $affiliate = stripslashes($row->affiliate_company);
}
$stmt->close();

// Handle mode
switch ($mode) {
    case "submit":
        $group = isset($_REQUEST['group']) ? intval($_REQUEST['group']) : 0;
        if (AssignAffiliateToGroup($affiliateId, $group)) {
            $to = getAffiliateMail($affiliateId);
            ApproveAffiliate($affiliateId);

            include "../mail.php";
            MailEvent("Approve Affiliate", 0, 0, $to, 0);

            header("Location: affiliate_approve.php?msg=approved&id=$affiliateId&page=$page");
            exit;
        } else {
            $msg = "Unknown Error. Operation Failed!";
            $groupList = ListAllAffiliateGroups($partners);
            include "affiliate_approve_form.php";
        }
        break;

    case "setCommission":
        $group = isset($_REQUEST['group']) ? intval($_REQUEST['group']) : 0;
        if (AssignAffiliateToGroup($affiliateId, $group)) {
            header("Location: affiliate_approve.php?msg=assigned&id=$affiliateId&page=$page&mode=TierGroup");
            exit;
        } else {
            $msg = "Unknown Error. Operation Failed!";
            $groupList = ListAllAffiliateGroups($partners);
            include "affiliate_approve_form.php";
        }
        break;

    case "TierGroup":
    default:
        $groupList = ListAllAffiliateGroups($partners);
        include "affiliate_approve_form.php";
        break;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// FUNCTIONS
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function ListAllAffiliateGroups($partners)
{
    $groups = [];
    $sql = "SELECT * FROM partners_affiliategroup";
    $result = $partners->db->query($sql);
    if ($result && $result->num_rows > 0) {
        while ($row = $result->fetch_object()) {
            $groups[] = [
                'groupId' => $row->affiliategroup_id,
                'groupTitle' => stripslashes($row->affiliategroup_title),
                'groupLevels' => $row->affiliategroup_levels
            ];
        }
    }
    return $groups;
}

function AssignAffiliateToGroup($affiliateId, $group)
{
    global $partners;
    $stmt = $partners->db->prepare("UPDATE partners_affiliate SET affiliate_group = ? WHERE affiliate_id = ?");
    $stmt->bind_param("ii", $group, $affiliateId);
    $success = $stmt->execute();
    $stmt->close();
    return $success;
}

function getAffiliateMail($affiliateId)
{
    global $partners;
    $stmt = $partners->db->prepare("SELECT login_email FROM partners_login WHERE login_id = ? AND login_flag = 'a'");
    $stmt->bind_param("i", $affiliateId);
    $stmt->execute();
    $stmt->bind_result($email);
    $stmt->fetch();
    $stmt->close();
    return $email;
}

function ApproveAffiliate($affiliateId)
{
    global $partners;
    $status = 'approved';
    $stmt = $partners->db->prepare("UPDATE partners_affiliate SET affiliate_status = ? WHERE affiliate_id = ?");
    $stmt->bind_param("si", $status, $affiliateId);
    $stmt->execute();
    $stmt->close();
}
?>
