<?php
session_start();
include 'layouts/dbconn.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

// Check if user is admin
$user_role = $_SESSION['role_name'] ?? '';
if ($user_role !== 'Super Admin') {
    header('Location: index.php');
    exit();
}

$user_id = $_GET['id'] ?? '';

if (empty($user_id)) {
    header('Location: list_users.php');
    exit();
}

// Get user details
$sql = "SELECT u.*, r.role_name
        FROM users u
        LEFT JOIN roles r ON u.role_id = r.id
        WHERE u.id = ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();

if (!$user) {
    header('Location: list_users.php');
    exit();
}

// Get activity log
$sql_activity = "SELECT * FROM activity_log 
                 WHERE user_id = ? 
                 ORDER BY logged_at DESC 
                 LIMIT 20";

$stmt_activity = $conn->prepare($sql_activity);
$stmt_activity->bind_param("i", $user_id);
$stmt_activity->execute();
$result_activity = $stmt_activity->get_result();
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'User Details')); ?>
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>

        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <?php includeFileWithVariables('layouts/page-title.php', array('pagetitle' => 'System', 'title' => 'User Details')); ?>

                    <div class="row mb-3">
                        <div class="col-12">
                            <a href="list_users.php" class="btn btn-light">
                                <i class="ri-arrow-left-line me-1"></i>Back to Users
                            </a>
                            <a href="add_user.php?id=<?php echo $user_id; ?>" class="btn btn-primary ms-2">
                                <i class="ri-edit-line me-1"></i>Edit User
                            </a>
                            <a href="reset_user_password.php?id=<?php echo $user_id; ?>" class="btn btn-warning ms-2">
                                <i class="ri-lock-password-line me-1"></i>Reset Password
                            </a>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-xl-8">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">
                                        <?php echo htmlspecialchars($user['first_name'] . ' ' . $user['last_name']); ?>
                                    </h4>
                                </div>
                                <div class="card-body">
                                    <div class="row mb-3">
                                        <div class="col-md-6">
                                            <p class="mb-2"><strong>Username:</strong> <?php echo htmlspecialchars($user['username']); ?></p>
                                            <p class="mb-2"><strong>Email:</strong> <?php echo htmlspecialchars($user['email']); ?></p>
                                            <p class="mb-2"><strong>Phone:</strong> <?php echo htmlspecialchars($user['phone'] ?? 'N/A'); ?></p>
                                        </div>
                                        <div class="col-md-6">
                                            <p class="mb-2"><strong>Role:</strong> 
                                                <span class="badge bg-soft-primary"><?php echo htmlspecialchars($user['role_name'] ?? 'N/A'); ?></span>
                                            </p>
                                            <p class="mb-2"><strong>Created:</strong> <?php echo date('M d, Y', strtotime($user['created_at'])); ?></p>
                                            <p class="mb-2"><strong>Last Login:</strong> 
                                                <?php echo $user['last_login'] ? date('M d, Y H:i', strtotime($user['last_login'])) : 'Never'; ?>
                                            </p>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="card mt-3">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">Recent Activity</h4>
                                </div>
                                <div class="card-body">
                                    <div class="table-responsive">
                                        <table class="table table-nowrap align-middle mb-0">
                                            <thead>
                                                <tr>
                                                    <th>Date/Time</th>
                                                    <th>Action</th>
                                                    <th>Module</th>
                                                    <th>Record ID</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if ($result_activity && $result_activity->num_rows > 0): ?>
                                                    <?php while($activity = $result_activity->fetch_assoc()): ?>
                                                        <tr>
                                                            <td><?php echo date('M d, Y H:i', strtotime($activity['logged_at'])); ?></td>
                                                            <td><?php echo htmlspecialchars($activity['action']); ?></td>
                                                            <td><?php echo htmlspecialchars($activity['module']); ?></td>
                                                            <td><?php echo htmlspecialchars($activity['record_id'] ?? 'N/A'); ?></td>
                                                        </tr>
                                                    <?php endwhile; ?>
                                                <?php else: ?>
                                                    <tr>
                                                        <td colspan="4" class="text-center py-4">
                                                            <div class="text-muted">No activity found</div>
                                                        </td>
                                                    </tr>
                                                <?php endif; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-xl-4">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">Quick Actions</h4>
                                </div>
                                <div class="card-body">
                                    <a href="add_user.php?id=<?php echo $user_id; ?>" class="btn btn-primary w-100 mb-2">
                                        <i class="ri-edit-line me-1"></i>Edit User
                                    </a>
                                    <a href="reset_user_password.php?id=<?php echo $user_id; ?>" class="btn btn-warning w-100 mb-2">
                                        <i class="ri-lock-password-line me-1"></i>Reset Password
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>

    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
