<?php
ob_start();
//=============================================================================//
// Script Name: return_world.php                                               //
// Last Modified: 20/08/2025 (Modernized)                                      //
//=============================================================================//

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

// Create database connection using mysqli
$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$mode = isset($_GET['mode']) ? $_GET['mode'] : '';
$returnUrl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'index.php';

switch ($mode) {
    case "approvePgm":
        $stmt = $conn->prepare("UPDATE partners_program SET program_status = 'active' WHERE program_status = 'inactive'");
        $stmt->execute();
        $stmt->close();
        break;

    case "rejectPgm":
        $stmt = $conn->prepare("UPDATE partners_program SET program_status = 'inactive' WHERE program_status = 'active'");
        $stmt->execute();
        $stmt->close();
        break;

    case "approveMerchants":
        $stmt = $conn->prepare("UPDATE partners_merchant SET merchant_status = 'approved' WHERE merchant_status = 'waiting'");
        $stmt->execute();
        $stmt->close();
        break;

    case "approveAffiliates":
        $stmt = $conn->prepare("UPDATE partners_affiliate SET affiliate_status = 'approved' WHERE affiliate_status = 'waiting'");
        $stmt->execute();
        $stmt->close();
        break;

    case "rejectMerchants":
        $result = $conn->query("SELECT merchant_id AS id FROM partners_merchant WHERE merchant_status = 'waiting'");
        if ($result && $result->num_rows > 0) {
            $stmt = $conn->prepare("DELETE FROM partners_login WHERE login_id = ? AND login_flag = 'm'");
            while ($row = $result->fetch_assoc()) {
                $stmt->bind_param("i", $row['id']);
                $stmt->execute();
            }
            $stmt->close();
        }
        $conn->query("DELETE FROM partners_merchant WHERE merchant_status = 'waiting'");
        break;

    case "rejectAffiliates":
        $result = $conn->query("SELECT affiliate_id AS id FROM partners_affiliate WHERE affiliate_status = 'waiting'");
        if ($result && $result->num_rows > 0) {
            $stmt = $conn->prepare("DELETE FROM partners_login WHERE login_id = ? AND login_flag = 'a'");
            while ($row = $result->fetch_assoc()) {
                $stmt->bind_param("i", $row['id']);
                $stmt->execute();
            }
            $stmt->close();
        }
        $conn->query("DELETE FROM partners_affiliate WHERE affiliate_status = 'waiting'");
        break;
}

$conn->close();

header("Location: $returnUrl");
exit;
?>
