<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) {
    header('Location: login.php');
    exit;
}
require_once '../src/config/db.php';
require_once '../src/includes/functions.php';
require_once 'includes/lang.php';

$users = get_all_users($pdo);
$unreadMessagesCount = $pdo->query("SELECT COUNT(*) FROM contact_messages WHERE is_read = 0")->fetchColumn();
?>
<!DOCTYPE html>
<html lang="<?php echo $t['lang']; ?>" dir="<?php echo $t['dir']; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $t['users_title']; ?> - VAULTIXAI</title>
    <link rel="stylesheet" href="<?php echo $bootstrapCSS; ?>" crossorigin="anonymous">
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>

    <!-- ═══ SIDEBAR ════════════════════════════════════════════ -->
    <div class="sidebar">
        <div class="sidebar-brand">
            <h2>VAULTIXAI</h2>
        </div>
        <nav>
            <ul>
                <li><a href="dashboard.php"><span class="nav-icon">🏠</span><?php echo $t['admin_dashboard']; ?></a></li>
                <li><a href="pages.php"><span class="nav-icon">📄</span><?php echo $t['admin_pages']; ?></a></li>
                <li><a href="certifications.php"><span class="nav-icon">🏆</span><?php echo $t['admin_certifications']; ?></a></li>
                <li><a href="services.php"><span class="nav-icon">⚙️</span><?php echo $t['admin_services']; ?></a></li>
                <li><a href="users.php" class="active"><span class="nav-icon">👥</span><?php echo $t['admin_users']; ?></a></li>
            </ul>
        </nav>
        <a href="<?php echo lang_switch_url($t['lang_switch_code']); ?>" class="sidebar-lang-btn">
            🌐 <?php echo $t['lang_switch_label']; ?>
        </a>
        <div class="sidebar-footer">
            <a href="logout.php"><span class="nav-icon">🚪</span><?php echo $t['admin_logout']; ?></a>
        </div>
    </div>

    <!-- ═══ MAIN CONTENT ════════════════════════════════════════ -->
    <div class="main-content">

        <div class="topbar">
            <h1>👥 <?php echo $t['users_title']; ?></h1>
            <a href="add_user.php" class="btn btn-add px-4 py-2">
                ➕ <?php echo $t['users_add_btn']; ?>
            </a>
        </div>

        <?php if (isset($_GET['success'])): ?>
            <div class="alert alert-success fw-bold mb-4">
                ✅ <?php switch ($_GET['success']) {
                    case 'add': echo $t['success_user_added']; break;
                    case 'edit': echo $t['success_updated']; break;
                    case 'delete': echo $t['success_deleted']; break;
                } ?>
            </div>
        <?php endif; ?>

        <?php if (isset($_GET['error'])): ?>
            <div class="alert alert-danger fw-bold mb-4">
                ⚠️ <?php switch ($_GET['error']) {
                    case 'self_delete': echo $t['error_self_delete']; break;
                    case 'not_found': echo $t['error_not_found']; break;
                } ?>
            </div>
        <?php endif; ?>

        <div class="table-card">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0" id="usersTable">
                    <thead>
                        <tr>
                            <th style="width:60px;"><?php echo $t['users_table_id']; ?></th>
                            <th><?php echo $t['users_table_name']; ?></th>
                            <th><?php echo $t['users_table_username']; ?></th>
                            <th><?php echo $t['users_table_email']; ?></th>
                            <th><?php echo $t['users_table_role']; ?></th>
                            <th style="width:200px; text-align:center;"><?php echo $t['users_table_actions']; ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($users)): ?>
                            <tr>
                                <td colspan="6" class="text-center text-muted py-5">
                                    😶 <?php echo $t['users_no_users']; ?>
                                </td>
                            </tr>
                        <?php else: ?>
                            <?php foreach ($users as $user): ?>
                            <tr>
                                <td class="text-muted small fw-bold">#<?php echo $user['id']; ?></td>
                                <td>
                                    <div class="d-flex align-items-center gap-2">
                                        <div class="rounded-circle d-inline-flex align-items-center justify-content-center fw-bold text-white"
                                             style="width:34px;height:34px;background:linear-gradient(135deg,#0f2044,#1a3260);font-size:.8rem;flex-shrink:0;">
                                            <?php echo mb_strtoupper(mb_substr($user['full_name'], 0, 1)); ?>
                                        </div>
                                        <div>
                                            <div class="fw-bold text-dark"><?php echo htmlspecialchars($user['full_name']); ?></div>
                                            <?php if ($user['username'] === $_SESSION['admin_username']): ?>
                                                <span class="badge bg-success-subtle text-success px-2 py-1 rounded-pill" style="font-size:.7rem;">
                                                    <?php echo $isRTL ? 'أنت' : 'You'; ?>
                                                </span>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <code class="text-muted" style="font-size:.85rem;"><?php echo htmlspecialchars($user['username']); ?></code>
                                </td>
                                <td class="text-muted small"><?php echo htmlspecialchars($user['email'] ?? '—'); ?></td>
                                <td>
                                    <span class="badge rounded-pill px-3 py-2"
                                          style="background:rgba(212,150,15,.12);color:#92600a;font-size:.78rem;font-weight:700;">
                                        <?php echo htmlspecialchars($user['role'] ?? ($isRTL ? 'مسؤول' : 'Admin')); ?>
                                    </span>
                                </td>
                                <td style="text-align:center;">
                                    <a href="edit_user.php?id=<?php echo $user['id']; ?>" class="btn btn-edit btn-sm px-3 py-1 me-1">
                                        ✏️ <?php echo $t['users_edit_btn']; ?>
                                    </a>
                                    <a href="delete_user.php?id=<?php echo $user['id']; ?>"
                                       class="btn btn-delete btn-sm px-3 py-1" data-confirm="<?php echo htmlspecialchars($t['users_delete_confirm']); ?>">
                                        🗑️ <?php echo $t['users_delete_btn']; ?>
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>

    </div><!-- /.main-content -->

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
    <script src="assets/js/main.js"></script>
</body>
</html>
