<?php
ob_start();

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);
$conn = $partners->conn;

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

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

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

// Fetch affiliate record
$sql = "SELECT * FROM partners_affiliate WHERE affiliate_id = ?";
$stmt = $conn->prepare($sql);
$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);
} else {
	$group = "";
	$affiliate = "";
}
$stmt->close();

// Main control flow
switch ($mode) {

	case "submit":
		$group = isset($_REQUEST['group']) ? $_REQUEST['group'] : '';

		if (AssignAffiliateToGroup($conn, $affiliateId, $group)) {
			$to = getAffiliateMail($conn, $affiliateId);

			ApproveAffiliate($conn, $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($conn);
			include "affiliate_approve_form.php";
		}
		break;

	case "setCommission":
		$group = isset($_REQUEST['group']) ? $_REQUEST['group'] : '';

		if (AssignAffiliateToGroup($conn, $affiliateId, $group)) {
			header("Location: affiliate_approve.php?msg=assigned&id=$affiliateId&page=$page&mode=TierGroup");
			exit;
		} else {
			$msg = "Unknown Error. Operation Failed!";
			$groupList = ListAllAffiliateGroups($conn);
			include "affiliate_approve_form.php";
		}
		break;

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

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

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

// List all affiliate groups
function ListAllAffiliateGroups($conn)
{
	$sql = "SELECT * FROM partners_affiliategroup";
	$result = $conn->query($sql);

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

// Assign a group to the affiliate
function AssignAffiliateToGroup($conn, $affiliateId, $group)
{
	$sql = "UPDATE partners_affiliate SET affiliate_group = ? WHERE affiliate_id = ?";
	$stmt = $conn->prepare($sql);
	$stmt->bind_param("si", $group, $affiliateId);
	$result = $stmt->execute();
	$stmt->close();
	return $result;
}

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

// Approve affiliate
function ApproveAffiliate($conn, $affiliateId)
{
	$sql = "UPDATE partners_affiliate SET affiliate_status = 'approved' WHERE affiliate_id = ?";
	$stmt = $conn->prepare($sql);
	$stmt->bind_param("i", $affiliateId);
	$stmt->execute();
	$stmt->close();
}
?>
