<?php
session_start();
include 'config.php';

// Enable error reporting for debugging
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

error_log("Attempting to access login_credentials.php at " . date('Y-m-d H:i:s') . " IST. Session data: " . print_r($_SESSION, true));

// Ensure only Admins and Super Admins access this page
if (!isset($_SESSION['username'], $_SESSION['role'], $_SESSION['user_id'])) {
    $_SESSION['error_message'] = 'Please log in to access the admin dashboard.';
    error_log("Redirecting to login.php: Session variables not set at " . date('Y-m-d H:i:s') . " IST");
    header("Location: login.php");
    exit();
}

$role = strtolower(trim($_SESSION['role']));
if (!in_array($role, ['admin', 'superadmin'])) {
    $_SESSION['error_message'] = 'Unauthorized access. Admin or Super Admin role required.';
    error_log("Redirecting to login.php: User role '$role' not authorized. User ID: {$_SESSION['user_id']}, Username: {$_SESSION['username']} at " . date('Y-m-d H:i:s') . " IST");
    header("Location: login.php");
    exit();
}

error_log("Login credentials dashboard accessed by username: {$_SESSION['username']}, role: {$_SESSION['role']}, user_id: {$_SESSION['user_id']} at " . date('Y-m-d H:i:s') . " IST");

// Fetch all users with responsibilities
$users = [];
$users_sql = "SELECT id, username, responsibilities FROM users ORDER BY username";
if ($users_result = $conn->query($users_sql)) {
    while ($row = $users_result->fetch_assoc()) {
        $users[] = $row;
    }
    $users_result->free();
    error_log("Fetched users data: " . print_r($users, true) . " at " . date('Y-m-d H:i:s') . " IST");
} else {
    error_log("Error fetching users: " . $conn->error . " at " . date('Y-m-d H:i:s') . " IST");
    $_SESSION['error_message'] = "Error fetching user credentials.";
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login Credentials - Admin Dashboard</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            display: flex;
            min-height: 100vh;
        }

        /* Sidebar Styles */
        .sidebar {
            width: 250px;
            background: #2c3e50;
            color: #fff;
            padding: 20px 15px;
            position: fixed;
            top: 0;
            bottom: 0;
            z-index: 1000;
            transform: translateX(-250px);
            transition: transform 0.3s ease;
        }

        .sidebar.active {
            transform: translateX(0);
        }

        .sidebar h2 {
            font-size: 1.25rem;
            margin: 0 0 20px;
            text-align: center;
            font-weight: 600;
        }

        .sidebar a {
            color: #fff;
            text-decoration: none;
            padding: 10px 15px;
            display: flex;
            align-items: center;
            gap: 10px;
            border-radius: 4px;
            margin-bottom: 8px;
            font-size: 0.9rem;
            transition: background 0.2s ease;
        }

        .sidebar a:hover {
            background: #1abc9c;
        }

        .sidebar a.active {
            background: #1abc9c;
            font-weight: 500;
        }

        .sidebar .dropdown-menu {
            background: #34495e;
            border: none;
            padding: 0;
            width: 100%;
            margin-left: 10px;
        }

        .sidebar .dropdown-menu a {
            color: #fff;
            padding: 8px 25px;
            font-size: 0.85rem;
        }

        .sidebar .dropdown-menu a:hover {
            background: #1abc9c;
        }

        .sidebar .dropdown-toggle::after {
            margin-left: auto;
        }

        /* Hamburger Button */
        .hamburger {
            position: fixed;
            top: 15px;
            left: 15px;
            z-index: 1100;
            background: #2c3e50;
            color: #fff;
            border: none;
            padding: 10px;
            border-radius: 4px;
            cursor: pointer;
            transition: background 0.2s ease;
        }

        .hamburger:hover {
            background: #1abc9c;
        }

        /* Content Area */
        .content {
            flex: 1;
            padding: 20px;
            margin-left: 0;
            transition: margin-left 0.3s ease;
        }

        .content.active {
            margin-left: 250px;
        }

        h1 {
            font-size: 1.5rem;
            margin-bottom: 20px;
            color: #333;
            text-align: center;
        }

        h2 {
            font-size: 1.2rem;
            color: #2c3e50;
            margin: 20px 0 15px;
        }

        .table {
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            margin-top: 20px;
        }

        .table th,
        .table td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        .table th {
            background: #2c3e50;
            color: #fff;
        }

        /* Responsive Design */
        @media (max-width: 576px) {
            .sidebar {
                width: 200px;
                padding: 15px 10px;
            }

            .sidebar h2 {
                font-size: 1rem;
            }

            .sidebar a {
                font-size: 0.8rem;
                padding: 8px 10px;
            }

            .content.active {
                margin-left: 200px;
            }

            h1 {
                font-size: 1.2rem;
            }

            h2 {
                font-size: 1rem;
            }

            .table th,
            .table td {
                padding: 8px;
                font-size: 0.8rem;
            }
        }
    </style>
</head>
<body>
    <!-- Hamburger Menu -->
    <button class="hamburger" onclick="toggleSidebar()">
        <i class="fas fa-bars"></i>
    </button>

    <!-- Sidebar -->
    <div class="sidebar" id="sidebar">
        <h2>Mypeopleconnect</h2>
        <a href="dashboard_admin.php"><i class="fas fa-home"></i> Organisation Chart</a>
        <div class="dropdown">
            <a class="dropdown-toggle" href="#" data-bs-toggle="dropdown">
                <i class="fas fa-home"></i> DAP
            </a>
            <ul class="dropdown-menu">
                <li><a class="dropdown-item" href="manage_users.php">Employee Details</a></li>
                <li><a class="dropdown-item" href="manage_documents.php">Policies Rolled Out</a></li>
                <li><a class="dropdown-item" href="digital.php">Acceptance Confirmed</a></li>
            </ul>
        </div>
        <div class="dropdown">
            <a class="dropdown-toggle" href="#" data-bs-toggle="dropdown">
                <i class="fas fa-users"></i> CAB
            </a>
            <ul class="dropdown-menu">
                <li><a class="dropdown-item" href="payroll_dashboard.php">Payroll</a></li>
                <li><a class="dropdown-item" href="leave_dashboard.php">Leave</a></li>
                <li><a class="dropdown-item" href="grievance_dashboard.php">Grievance Portal</a></li>
            </ul>
        </div>
        <div class="dropdown">
            <a class="dropdown-toggle" href="#" data-bs-toggle="dropdown">
                <i class="fas fa-user"></i> PEP
            </a>
            <ul class="dropdown-menu">
                <li><a class="dropdown-item" href="individual_rr.php">Individual R & R</a></li>
                <li><a class="dropdown-item" href="individual_kpi.php">Individual KPI</a></li>
                <li><a class="dropdown-item active" href="login_credentials.php">Login Credentials</a></li>
            </ul>
        </div>
        <div class="dropdown">
            <a class="dropdown-toggle" href="#" data-bs-toggle="dropdown">
                <i class="fas fa-book"></i> L&D
            </a>
            <ul class="dropdown-menu">
                <li><a class="dropdown-item" href="incident_employee.php">Incident Portal</a></li>
                <li><a class="dropdown-item" href="training_dashboard.php">Training Portal</a></li>
                <li><a class="dropdown-item" href="rr.php">Rewards and Recognition</a></li>
            </ul>
        </div>
        <div class="dropdown">
            <a class="dropdown-toggle" href="#" data-bs-toggle="dropdown">
                <i class="fas fa-star"></i> AAA
            </a>
            <ul class="dropdown-menu">
                <li><a class="dropdown-item" href="employee_tiy.php">Employee TIY</a></li>
                <li><a class="dropdown-item" href="login_credentials.php">Login Credentials</a></li>
            </ul>
        </div>
        <a href="logout.php" class="text-danger"><i class="fas fa-sign-out-alt"></i> Logout</a>
    </div>

    <!-- Main Content -->
    <div class="content" id="content">
        <div class="container mt-4">
            <h1 class="text-primary text-center mb-4">
                Login Credentials - Welcome <?php echo htmlspecialchars($_SESSION['username']); ?>
            </h1>

            <!-- Display error messages -->
            <?php if (isset($_SESSION['error_message'])): ?>
                <div class="alert alert-danger">
                    <?php echo htmlspecialchars($_SESSION['error_message']); unset($_SESSION['error_message']); ?>
                </div>
            <?php endif; ?>
            <?php if (isset($_SESSION['message'])): ?>
                <div class="alert alert-success">
                    <?php echo htmlspecialchars($_SESSION['message']); unset($_SESSION['message']); ?>
                </div>
            <?php endif; ?>

            <!-- Users Table -->
            <?php if (!empty($users)): ?>
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Username</th>
                                <th>Responsibilities</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($users as $user): ?>
                                <tr>
                                    <td><?php echo htmlspecialchars($user['id']); ?></td>
                                    <td><?php echo htmlspecialchars($user['username']); ?></td>
                                    <td><?php echo nl2br(htmlspecialchars($user['responsibilities'] ?? 'None')); ?></td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php else: ?>
                <div class="alert alert-warning mt-4">
                    No users found in the system.
                </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Scripts -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        function toggleSidebar() {
            document.getElementById('sidebar').classList.toggle('active');
            document.getElementById('content').classList.toggle('active');
        }
    </script>
</body>
</html>