<?php
ob_start();

include '../includes/session.php';
header("Cache-Control: private");
include '../includes/constants.php';
include '../includes/functions.php';
include '../includes/allstripslashes.php';
include '../lang/admin_english.php';
include '../includes/function1.php';

// Create connection
$partners = new partners();
$partners->connection($host, $user, $pass, $db);
$mysqli = $partners->getMysqli(); // Assumes your `partners` class exposes this connection (optional)

// Admin user session
$userobj = new adminuser();
$adminUserId = $_SESSION['ADMINUSERID'] ?? null;

// Default currency symbol
$currency_code = $default_currency_code;
$currSymbol = '';
$currValue = '';
$currCode = '';

if (!empty($currency_code)) {
    $stmt = $mysqli->prepare("SELECT currency_symbol, currency_caption, currency_code FROM partners_currency WHERE currency_code = ?");
    $stmt->bind_param("s", $currency_code);
    $stmt->execute();
    $stmt->bind_result($symbol, $caption, $code);

    if ($stmt->fetch()) {
        $currSymbol = stripslashes($symbol);
        $currValue  = stripslashes($caption);
        $currCode   = stripslashes($code);
        $_SESSION['DEFAULTCURRENCYSYMBOL'] = $currSymbol;
    }

    $stmt->close();
}

// Get Act from GET
$Act = $_GET['Act'] ?? '';
$body_onload = "";

// Handle specific Act conditions
if (in_array($Act, ['link_report', 'referer_report', 'product_report'])) {
    $linkName = ucfirst(str_replace('_report', '', $Act));
    if ($userobj->GetAdminUserLink($linkName, $adminUserId, 5)) {
        $body_onload = "onload='LoadPrgms();'";
    }
} elseif ($Act == "crm") {
    $body_onload = "onload='loadCat();'";
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Affiliate Network Pro - Admin Panel</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="admin.css" />
</head>

<body <?= $body_onload ?>>
<table width="914" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border:#d4d4d4 thin solid;">
    <tr>
        <td valign="top" class="logo-bg"><?php include "header.php"; ?></td>
    </tr>
    <tr>
        <td align="right">
            <?php
            if (!$partners->islogin()) {
                include "admin_login.php";
                exit;
            }

            // Admin session update
            $ip = $_SERVER['REMOTE_ADDR'] ?? '';

            if ($adminUserId === '1') {
                $stmt = $mysqli->prepare("UPDATE partners_admin SET admin_ip = ?, admin_lastLogin = NOW()");
                $stmt->bind_param("s", $ip);
                $stmt->execute();
                $stmt->close();
            }

            $stmt = $mysqli->prepare("UPDATE partners_adminusers SET adminusers_ip = ?, adminusers_lastLogin = NOW() WHERE adminusers_id = ?");
            $stmt->bind_param("si", $ip, $adminUserId);
            $stmt->execute();
            $stmt->close();
            ?>
        </td>
    </tr>
    <tr>
        <td>
            <table width="97%" align="center" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td align="right" width="4"><img src="images/topbar-left.jpg" width="4" height="22" /></td>
                    <td class="top-bar-tile">&nbsp;</td>
                    <td align="left" width="4"><img src="images/topbar-right.jpg" width="4" height="22" /></td>
                </tr>
            </table>
        </td>
    </tr>
    <tr><td align="center" height="5"></td></tr>

    <tr>
        <td align="center" height="315" valign="top" width="100%">
            <?php
            if (!in_array($Act, ['merchants', 'affiliates'])) {
                include "heap_remove.php";
            }
            include "process.php";
            ?>
        </td>
    </tr>
    <tr>
        <td align="center" class="footer-bg"><?php include "footer.php"; ?></td>
    </tr>
</table>
</body>
</html>
