<?php
/**
 * BIS Campaign - slim navigation (customers, users, email campaigns only).
 */
include_once __DIR__ . "/../../function/classify_function.php";

$groupid = $_SESSION['user_group'];

if (!isset($sidebar_active)) {
    $current_script = basename(parse_url($_SERVER['PHP_SELF'] ?? $_SERVER['SCRIPT_NAME'] ?? '', PHP_URL_PATH) ?: '');
    if (!empty($_GET['token'])) {
        $_SESSION['bis_sidebar_token'] = trim((string) base64_decode($_GET['token']));
    }
    $current_token = trim((string) ($_SESSION['bis_sidebar_token'] ?? base64_decode($_GET['token'] ?? '')));

    if ($current_script === 'customer_index.php') {
        $sidebar_active = 'customers';
    } elseif ($current_script === 'user_index.php') {
        $sidebar_active = 'users';
    } elseif ($current_script === 'bulkcampaign_index.php') {
        if ($current_token === 'bulk_email_report') {
            $sidebar_active = 'campaign_report';
        } elseif ($current_token === 'email_queue_report') {
            $sidebar_active = 'email_queue';
        } else {
            $sidebar_active = 'email_campaign';
        }
    } elseif ($current_script === 'admin_index.php') {
        $smtp_tokens = [
            'view_imap_smtp', 'new_imap', 'new_smtp',
            'new_mail_sign', 'mail_sign', 'spam_mail', 'new_spam_mail', 'reply_template',
        ];
        $sidebar_active = in_array($current_token, $smtp_tokens, true)
            ? 'email_smtp'
            : 'campaign_templates';
    }
}

$sidebar_link_class = static function (string $key, bool $is_sub = false): string {
    global $sidebar_active;
    $classes = ['nav-link'];
    if (($sidebar_active ?? '') === $key) {
        $classes[] = 'active';
        $classes[] = 'text-white';
    } else {
        $classes[] = $is_sub ? 'text-white-50' : 'text-white';
    }
    return implode(' ', $classes);
};

$web_consumer_home   = base64_encode('web_consumer_home');
$web_userhome        = base64_encode('web_userhome');
$web_bulksms         = base64_encode('web_bulksms');
$bulk_email_report   = base64_encode('bulk_email_report');
$email_queue_report  = base64_encode('email_queue_report');
$email_sms_template  = base64_encode('email_sms_template');
$view_imap_smtp      = base64_encode('view_imap_smtp');

$bisLogoFile = defined('BIS_LOGO_FILE') ? BIS_LOGO_FILE : 'bis-research-logo.png';
$bisLogoPath = dirname(__DIR__, 2) . '/public/images/' . $bisLogoFile;
$pdfLogoBase64 = '';
if (is_readable($bisLogoPath)) {
    $pdfLogoBase64 = 'data:image/png;base64,' . base64_encode((string) file_get_contents($bisLogoPath));
}
?>
<div id="pdf-logo-base64" style="display:none;"><?= htmlspecialchars($pdfLogoBase64, ENT_QUOTES, 'UTF-8') ?></div>
<div class="d-flex flex-column flex-shrink-0 p-3 text-dark bg-dark sidebar" style="min-height:100vh;">
    <a href="#">
        <span class="fs-4">
            <img
                class="vision2"
                src="<?=$SiteURL?>public/images/bis-research-logo.png"
                alt="BIS Research"
                style="max-width:180px;height:auto;background:#fff;padding:6px;border-radius:4px;"
                onerror="this.onerror=null;this.src='https://bisresearch.com/img/logo.png';"
            >
        </span>
    </a>
    <p style="color:#8fa4b8;font-size:11px;margin:4px 0 0 8px;">BIS Campaign</p>
    <hr>
    <ul class="nav nav-pills flex-column mb-auto">
        <li class="nav-item">
            <a href="customer_index.php?token=<?php echo $web_consumer_home; ?>"
               class="<?php echo $sidebar_link_class('customers'); ?>">
                <i class="fa fa-users"></i> Customers
            </a>
        </li>
        <li class="nav-item">
            <a href="user_index.php?token=<?php echo $web_userhome; ?>"
               class="<?php echo $sidebar_link_class('users'); ?>">
                <i class="fa fa-user"></i> Users
            </a>
        </li>
        <li class="nav-item">
            <a href="bulkcampaign_index.php?token=<?php echo $web_bulksms; ?>&campaign=Email"
               class="<?php echo $sidebar_link_class('email_campaign'); ?>">
                <i class="fa fa-envelope"></i> Email Campaign
            </a>
        </li>
        <li class="nav-item sidebar-sub-item">
            <a href="bulkcampaign_index.php?token=<?php echo $bulk_email_report; ?>"
               class="<?php echo $sidebar_link_class('campaign_report', true); ?>">
                Campaign Report
            </a>
        </li>
        <li class="nav-item sidebar-sub-item">
            <a href="bulkcampaign_index.php?token=<?php echo $email_queue_report; ?>"
               class="<?php echo $sidebar_link_class('email_queue', true); ?>">
                Email Queue
            </a>
        </li>
        <li class="nav-item sidebar-sub-item">
            <a href="admin_index.php?token=<?php echo $email_sms_template; ?>"
               class="<?php echo $sidebar_link_class('campaign_templates', true); ?>">
                Campaign Templates
            </a>
        </li>
        <li class="nav-item sidebar-sub-item">
            <a href="admin_index.php?token=<?php echo $view_imap_smtp; ?>"
               class="<?php echo $sidebar_link_class('email_smtp', true); ?>">
                Email / SMTP Settings
            </a>
        </li>
    </ul>
    <hr>
    <div class="dropdown">
        <a href="#" class="d-flex align-items-center text-white text-decoration-none dropdown-toggle" data-bs-toggle="dropdown">
            <span class="d-none d-sm-inline mx-1"><?php echo htmlspecialchars($_SESSION['full_name'] ?? $_SESSION['logged'] ?? ''); ?></span>
        </a>
        <ul class="dropdown-menu dropdown-menu-dark text-small shadow">
            <li><a class="dropdown-item" href="../web_logout.php">Logout</a></li>
        </ul>
    </div>
</div>
