<?php
/**
 * Enterprise HRM ERP System - Site Switcher (Super Admin Only)
 */

if (!defined('HRM_ACCESS')) {
    die('Direct access not allowed');
}

/**
 * Get site switcher HTML (for Super Admin and HR Admin with multiple branches)
 */
function getSiteSwitcher() {
    startSession();
    
    if (!isset($_SESSION['user_id'])) {
        return '';
    }
    
    try {
        $db = getDB();
        $currentSiteId = $_SESSION['site_id'] ?? null;
        $sites = [];
        
        // Super Admin can see all sites
        if (isSuperAdmin()) {
            $stmt = $db->prepare("SELECT site_id, site_name, site_code FROM sites WHERE site_status = 'active' ORDER BY site_name");
            $stmt->execute();
            $sites = $stmt->fetchAll();
            
            $html = '<div style="margin-right: 15px;">';
            $html .= '<select id="siteSwitcher" class="form-control" style="width: 200px; display: inline-block;" onchange="switchSite(this.value)">';
            $html .= '<option value="">All Sites</option>';
            foreach ($sites as $site) {
                $selected = ($currentSiteId == $site['site_id']) ? 'selected' : '';
                $html .= '<option value="' . $site['site_id'] . '" ' . $selected . '>' . htmlspecialchars($site['site_name']) . '</option>';
            }
            $html .= '</select>';
            $html .= '</div>';
            $html .= '<script>
            function switchSite(siteId) {
                window.location.href = "' . BASE_URL . '/admin/site_switch.php?site_id=" + (siteId || "");
            }
            </script>';
            
            return $html;
        }
        
        // HR Admin can switch between their assigned branches
        if (isHRAdmin() || (isset($_SESSION['role_code']) && strtolower($_SESSION['role_code']) === 'hr_admin')) {
            // Get user's accessible sites
            $stmt = $db->prepare("
                SELECT s.site_id, s.site_name, s.site_code, us.is_primary
                FROM user_sites us
                INNER JOIN sites s ON us.site_id = s.site_id
                WHERE us.user_id = ? AND s.site_status = 'active'
                ORDER BY us.is_primary DESC, s.site_name
            ");
            $stmt->execute([getCurrentUserId()]);
            $sites = $stmt->fetchAll();
            
            // Only show switcher if user has multiple branches
            if (count($sites) > 1) {
                $html = '<div style="margin-right: 15px;">';
                $html .= '<select id="siteSwitcher" class="form-control" style="width: 200px; display: inline-block;" onchange="switchSite(this.value)">';
                foreach ($sites as $site) {
                    $selected = ($currentSiteId == $site['site_id']) ? 'selected' : '';
                    $label = htmlspecialchars($site['site_name']);
                    if ($site['is_primary'] == 1) {
                        $label .= ' (Primary)';
                    }
                    $html .= '<option value="' . $site['site_id'] . '" ' . $selected . '>' . $label . '</option>';
                }
                $html .= '</select>';
                $html .= '</div>';
                $html .= '<script>
                function switchSite(siteId) {
                    window.location.href = "' . BASE_URL . '/admin/site_switch.php?site_id=" + siteId;
                }
                </script>';
                
                return $html;
            }
        }
        
        return '';
    } catch (Exception $e) {
        error_log("Error loading sites for switcher: " . $e->getMessage());
        return '';
    }
}

