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

if ($_SESSION['role'] != 'Super Admin') {
    header("Location: dashboard.php");
    exit();
}

if (!isset($_GET['id'])) {
    echo "<div class='alert alert-danger'>Invalid User ID.</div>";
    exit();
}

$user_id = intval($_GET['id']);
$stmt = $conn->prepare("SELECT * FROM users WHERE id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();

if (!$user) {
    echo "<div class='alert alert-danger'>User not found.</div>";
    exit();
}
$stmt->close();
?>

<div class="container mt-4">
    <h2 class="mb-4">User Details</h2>
    <div class="card">
        <div class="card-body">
            <div class="row">
                <!-- Profile Picture -->
                <div class="col-md-4 text-center">
                    <?php 
                    $photo = !empty($user['photo']) ? $user['photo'] : 'uploads/no-photo.jpg';
                    ?>
                    <img src="<?php echo htmlspecialchars($photo); ?>" alt="Profile Picture" class="img-thumbnail" width="200">
                </div>
                
                <!-- User Information -->
                <div class="col-md-8">
                    <table class="table table-bordered">
                        <tr><th>Username</th><td><?php echo htmlspecialchars($user['username']); ?></td></tr>
                        <tr><th>Email</th><td><?php echo htmlspecialchars($user['email'] ?? 'N/A'); ?></td></tr>
                        <tr><th>Role</th><td><?php echo htmlspecialchars($user['role']); ?></td></tr>
                        <tr><th>Mobile No</th><td><?php echo htmlspecialchars($user['mobileno'] ?? 'N/A'); ?></td></tr>
                        <tr><th>division</th><td><?php echo htmlspecialchars($user['division'] ?? 'N/A'); ?></td></tr>
                        <tr><th>Designation</th><td><?php echo htmlspecialchars($user['designation'] ?? 'N/A'); ?></td></tr>
                        <tr><th>Basic Salary</th><td><?php echo htmlspecialchars($user['basic_salary'] ?? '0.00'); ?></td></tr>
                        <tr><th>Date of Birth</th><td><?php echo htmlspecialchars($user['date_of_birth'] ?? 'N/A'); ?></td></tr>
                        <tr><th>Date of Joining</th><td><?php echo htmlspecialchars($user['date_of_joining'] ?? 'N/A'); ?></td></tr>
                        <tr><th>Address</th><td><?php echo nl2br(htmlspecialchars($user['address'] ?? 'N/A')); ?></td></tr>
                        <tr>
                            <th>Responsibilities</th>
                            <td>
                                <?php 
                                if (!empty($user['responsibilities'])) {
                                    $responsibilities = explode("\n", $user['responsibilities']); // Split by new line
                                    echo "<ul>";
                                    foreach ($responsibilities as $responsibility) {
                                        echo "<li>" . htmlspecialchars(trim($responsibility)) . "</li>";
                                    }
                                    echo "</ul>";
                                } else {
                                    echo "N/A";
                                }
                                ?>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
            <a href="manage_users.php" class="btn btn-primary">Back to Users</a>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>
