<?php
$base_dir = rtrim(str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'] ?? '/')), '/');
$static_url = ($base_dir === '' || $base_dir === '.') ? '/assets/' : $base_dir . '/assets/';
$username = isset($_SESSION['username']) ? $_SESSION['username'] : 'Usuário';
$isAdmin = !empty($_SESSION['admin']) ? 'Administrador' : (!empty($_SESSION['dealer']) ? 'Revendedor' : 'Usuário');
$balance = isset($_SESSION['balance']) ? $_SESSION['balance'] : 0;
?>
<div id="kt_app_header" class="app-header" data-kt-sticky="true" data-kt-sticky-activate="{default: true, lg: true}"
    data-kt-sticky-name="app-header-sticky" data-kt-sticky-offset="{default: '200px', lg: '300px'}">
    <div class="app-container container-fluid d-flex flex-stack" id="kt_app_header_container">
        <div class="d-flex d-lg-none align-items-center me-lg-20 gap-2">
            <div class="btn btn-icon btn-color-gray-500 btn-active-color-primary w-35px h-35px" id="kt_app_sidebar_toggle">
                <i class="ki-outline ki-abstract-14 lh-0 fs-1"></i>
            </div>
            <a href="dns.php"><img alt="Logo" src="<?php echo $static_url; ?>media/logos/logo.png" class="h-40px" /></a>
        </div>

        <div class="d-flex flex-stack flex-lg-row-fluid" id="kt_app_header_wrapper">
            <div class="d-flex align-items-center gap-3">
                <div class="d-none d-lg-block">
                    <div class="fw-bolder fs-4 text-gray-900">TV VIP • Central de Controle</div>
                    <div class="text-gray-500 fs-8">Aplicativo, listas e personalização em uma única operação.</div>
                </div>
            </div>

            <div class="app-navbar flex-shrink-0 gap-2 gap-lg-4">
                <div class="app-navbar-item ms-lg-5" id="kt_header_user_menu_toggle">
                    <div class="d-flex align-items-center cursor-pointer" data-kt-menu-trigger="{default: 'click', lg: 'hover'}" data-kt-menu-attach="parent" data-kt-menu-placement="bottom-end">
                        <div class="text-end d-none d-sm-flex flex-column justify-content-center me-3">
                            <span class="text-gray-500 fs-8 fw-bold">Bem-vindo</span>
                            <span class="text-gray-800 fs-7 fw-bold d-block"><?php echo htmlspecialchars($username); ?></span>
                        </div>
                        <div class="symbol symbol-circle symbol-40px position-relative">
                            <img src="<?php echo $static_url; ?>media/logos/logo.png" alt="TV VIP" style="object-fit:contain;background:#080808;padding:4px;" />
                            <span class="position-absolute translate-middle bottom-0 mb-1 start-100 ms-n1 bg-success rounded-circle h-8px w-8px"></span>
                        </div>
                    </div>

                    <div class="menu menu-sub menu-sub-dropdown menu-column menu-rounded menu-gray-800 menu-state-bg menu-state-color fw-semibold py-4 fs-6 w-275px" data-kt-menu="true">
                        <div class="menu-item px-3">
                            <div class="menu-content d-flex align-items-center px-3">
                                <div class="symbol symbol-50px me-5"><img alt="TV VIP" src="<?php echo $static_url; ?>media/logos/logo.png" style="object-fit:contain;background:#080808;padding:5px;" /></div>
                                <div class="d-flex flex-column">
                                    <div class="fw-bold d-flex align-items-center fs-5"><?php echo htmlspecialchars($username); ?></div>
                                    <span class="badge badge-light-warning fw-bold fs-8 px-2 py-1 mt-1"><?php echo htmlspecialchars($isAdmin); ?></span>
                                    <?php if ((float)$balance > 0): ?><span class="text-muted fs-8 mt-1">Créditos: <?php echo htmlspecialchars((string)$balance); ?></span><?php endif; ?>
                                </div>
                            </div>
                        </div>
                        <div class="separator my-2"></div>
                        <div class="menu-item px-5"><a href="users.php" class="menu-link px-5">Meu perfil</a></div>
                        <div class="menu-item px-5"><a href="logout.php" class="menu-link px-5 text-danger">Sair</a></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
