<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sistema de Contratos</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link rel="stylesheet" href="style.css">
</head>
<body>
<div class="d-flex">
    <?php if(isset($_SESSION['user_id'])): 
        // Atualizar o papel (role) do usuário do banco de dados para garantir que o menu apareça
        if (!function_exists('get_db_connection')) {
            require_once __DIR__ . '/../src/db.php';
        }
        $pdo = get_db_connection();
        $stmtRole = $pdo->prepare("SELECT role FROM users WHERE id = ?");
        $stmtRole->execute([$_SESSION['user_id']]);
        $dbRole = $stmtRole->fetchColumn();
        $_SESSION['role'] = $dbRole;
    ?>
    <div class="sidebar p-3 d-flex flex-column">
        <div class="sidebar-header">
            Contratos
        </div>
        <hr class="text-white-50">
        <ul class="nav nav-pills flex-column mb-auto">
            <li class="nav-item">
                <a href="dashboard.php" class="nav-link text-white <?= (basename($_SERVER['PHP_SELF']) == 'dashboard.php') ? 'active' : '' ?>">
                    <i class="fas fa-home me-2"></i> Dashboard
                </a>
            </li>
            <?php 
             // Verificação de administrador diretamente do banco de dados para garantir o acesso
             $isAdmin = false;
             if (isset($_SESSION['user_id'])) {
                 if (!function_exists('get_db_connection')) {
                     require_once __DIR__ . '/../src/db.php';
                 }
                 $pdo = get_db_connection();
                 $stmtCheck = $pdo->prepare("SELECT role FROM users WHERE id = ?");
                 $stmtCheck->execute([$_SESSION['user_id']]);
                 $userRole = $stmtCheck->fetchColumn();
                 if ($userRole === 'admin') {
                     $isAdmin = true;
                 }
             }
             if ($isAdmin): ?>
            <li class="nav-item">
                <a href="admin.php" class="nav-link text-white <?= (basename($_SERVER['PHP_SELF']) == 'admin.php') ? 'active' : '' ?>">
                    <i class="fas fa-user-shield me-2"></i> Painel Administrativo
                </a>
            </li>
            <?php endif; ?>
            <li class="nav-item">
                <a href="units.php" class="nav-link text-white <?= (basename($_SERVER['PHP_SELF']) == 'units.php' || basename($_SERVER['PHP_SELF']) == 'edit_unit.php') ? 'active' : '' ?>">
                    <i class="fas fa-building me-2"></i> Unidades
                </a>
            </li>
            <li class="nav-item">
                <a href="suppliers.php" class="nav-link text-white <?= (basename($_SERVER['PHP_SELF']) == 'suppliers.php') ? 'active' : '' ?>">
                    <i class="fas fa-truck me-2"></i> Clientes/Fornecedores
                </a>
            </li>
            <li class="nav-item">
                <a href="contracts.php" class="nav-link text-white <?= (basename($_SERVER['PHP_SELF']) == 'contracts.php' || basename($_SERVER['PHP_SELF']) == 'edit_contract.php') ? 'active' : '' ?>">
                    <i class="fas fa-file-contract me-2"></i> Contratos
                </a>
            </li>
            <li class="nav-item">
                <a href="email_contracts.php" class="nav-link text-white <?= (basename($_SERVER['PHP_SELF']) == 'email_contracts.php' || basename($_SERVER['PHP_SELF']) == 'install_email_contracts.php') ? 'active' : '' ?>">
                    <i class="fas fa-envelope-open-text me-2"></i> Contratos por E-mail
                </a>
            </li>
            <li class="nav-item">
                <a href="reports.php" class="nav-link text-white <?= (basename($_SERVER['PHP_SELF']) == 'reports.php') ? 'active' : '' ?>">
                    <i class="fas fa-chart-line me-2"></i> Relatórios
                </a>
            </li>
        </ul>
        <hr class="text-white-50 mt-auto">
        <a href="logout.php" class="nav-link text-danger">
            <i class="fas fa-sign-out-alt me-2"></i> Sair
        </a>
    </div>
    <?php endif; ?>
    <div class="content flex-grow-1">
