<?php
ob_start();

/***************************************************************************************************
 PROGRAM DESCRIPTION :  PROCESSING PAGE TO VIEW OR EDIT AFFILIATES
***************************************************************************************************/

include_once '../includes/constants.php';
include_once '../includes/functions.php';
include "../mail.php";
include_once '../includes/allstripslashes.php';  

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

/**************** Getting variables ***********/
$page = isset($_GET['page']) ? intval(trim($_GET['page'])) : 1;
$selaction = isset($_POST['selaction']) ? trim($_POST['selaction']) : '';
$sortby = isset($_GET['sortby']) ? $_GET['sortby'] : '';

$aff = explode('~', $selaction);
$affiliateId = isset($aff[0]) ? intval($aff[0]) : 0;
$action = isset($aff[1]) ? $aff[1] : '';

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

/**************** Check selected actions **************/
switch ($action) {
    case 'ViewProfile':
        header("Location: index.php?Act=affiliates&mode=ViewProfile&affid=$affiliateId&page=$page&sortby=$sortby");
        exit;

    case 'ChangePassword':
        header("Location: index.php?Act=affiliates&mode=ChangePassword&affid=$affiliateId&loginflag=a&page=$page&sortby=$sortby");
        exit;

    case 'Reject':
        header("Location: index.php?Act=affiliates&mode=Reject&affid=$affiliateId&loginflag=a&page=$page&sortby=$sortby");
        exit;

    case 'Approve':
        header("Location: index.php?Act=affiliates&mode=Approve&affid=$affiliateId&loginflag=a&page=$page&sortby=$sortby");
        exit;

    case 'Remove':
        header("Location: index.php?Act=affiliates&mode=Remove&affid=$affiliateId&loginflag=a&page=$page&sortby=$sortby");
        exit;

    case 'Suspend':
        $stmt = $conn->prepare("UPDATE partners_affiliate SET affiliate_status = 'suspend' WHERE affiliate_id = ?");
        $stmt->bind_param("i", $affiliateId);
        $stmt->execute();
        $stmt->close();

        MailEvent("Suspend Affiliate", 0, 0, $to, 0); // call mail function
        break;

    case 'adjust':
        header("Location: index.php?Act=adjust_affiliate&affid=$affiliateId");
        exit;

    case 'payment':
        header("Location: index.php?Act=payment_affiliate&affid=$affiliateId");
        exit;

    case 'transaction':
        header("Location: index.php?Act=transaction_affiliate&affid=$affiliateId");
        exit;

    case 'TierGroup':
        header("Location: index.php?Act=affiliates&mode=TierGroup&affid=$affiliateId&loginflag=a&page=$page&sortby=$sortby");
        exit;

    default:
        // No action matched
        break;
}

/**************** Redirect to affiliate list **************/
header("Location: index.php?Act=affiliates&page=$page");
exit;
