<?php
session_start();
require_once("../config/config.php");

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

$user_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($user_id <= 0) {
    die("<h2 style='color:#ef4444;text-align:center;padding:100px;'>No User Selected</h2>");
}

// Fetch User Details
$stmt = $conn->prepare("SELECT * FROM users WHERE id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();

if (!$user) {
    die("<h2 style='color:#ef4444;text-align:center;padding:100px;'>User Not Found</h2>");
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User Details - <?= htmlspecialchars($user['full_name']) ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        :root {
            --primary: #a855f7;
            --accent: #c084fc;
            --glass: rgba(255,255,255,0.10);
            --glass-border: rgba(255,255,255,0.20);
        }
        * { margin:0; padding:0; box-sizing:border-box; }
        body {
            font-family: 'Inter', system-ui, sans-serif;
            background: linear-gradient(135deg, rgba(0,0,0,0.78), rgba(0,0,0,0.92)), url('../background.jpeg') center/cover fixed;
            color: #f1f5f9;
            padding: 20px 15px;
            min-height: 100vh;
        }
        .container { max-width: 1100px; margin: auto; }
        .glass-container {
            background: var(--glass);
            backdrop-filter: blur(24px);
            border-radius: 32px;
            border: 1px solid var(--glass-border);
            box-shadow: 0 25px 80px rgba(0,0,0,0.65);
            overflow: hidden;
        }
        .header {
            background: linear-gradient(90deg, rgba(168,85,247,0.35), rgba(147,51,234,0.25));
            padding: 50px 40px;
            text-align: center;
        }
        .header h1 { 
            font-size: clamp(28px, 5.5vw, 40px); 
            font-weight: 700; 
            margin-bottom: 8px;
        }

        .form-area { padding: 45px 40px; }

        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 20px;
            margin-bottom: 40px;
        }
        .info-card {
            background: rgba(255,255,255,0.08);
            border: 1px solid rgba(255,255,255,0.18);
            border-radius: 20px;
            padding: 25px;
        }
        .info-label {
            font-size: 0.95rem;
            color: #c4b5fd;
            margin-bottom: 6px;
        }
        .info-value {
            font-size: 1.15rem;
            color: #e0e7ff;
        }

        .status-badge {
            display: inline-block;
            padding: 10px 24px;
            border-radius: 50px;
            font-weight: 700;
            font-size: 1.05rem;
        }
        .status-Active { background: #22c55e; color: white; }
        .status-Inactive { background: #64748b; color: white; }
        .status-Suspended { background: #ef4444; color: white; }

        .role-badge {
            display: inline-block;
            padding: 8px 20px;
            background: rgba(168,85,247,0.25);
            color: #e0e7ff;
            border-radius: 50px;
            font-weight: 600;
        }

        .detail-box {
            background: rgba(255,255,255,0.06);
            border-radius: 16px;
            padding: 25px;
            margin-bottom: 30px;
        }

        .btn {
            padding: 14px 28px;
            border-radius: 14px;
            text-decoration: none;
            font-weight: 700;
            display: inline-flex;
            align-items: center;
            gap: 10px;
            margin-right: 12px;
        }
        .btn-edit {
            background: linear-gradient(135deg, var(--primary), var(--accent));
            color: white;
        }
        .btn-back {
            background: rgba(255,255,255,0.1);
            color: #e0e7ff;
            border: 1px solid rgba(255,255,255,0.3);
        }

        @media (max-width: 768px) {
            .form-area { padding: 30px 20px; }
            .info-grid { grid-template-columns: 1fr; }
        }
    </style>
</head>
<body>
<?php include '../config/header.php'; ?>

<div class="container">
    <div class="glass-container">
        <div class="header">
            <a href="user-list.php" style="color:#c4b5fd; text-decoration:none; position:absolute; left:40px;">
                ← Back to Users
            </a>
            <h1><i class="fas fa-user"></i> User Details</h1>
            <p style="margin-top:10px; opacity:0.9;"><?= htmlspecialchars($user['user_code']) ?></p>
        </div>

        <div class="form-area">

            <!-- Status -->
            <div style="text-align:center; margin-bottom:35px;">
                <span class="status-badge status-<?= htmlspecialchars($user['status']) ?>">
                    <?= htmlspecialchars($user['status']) ?>
                </span>
            </div>

            <!-- Main Info -->
            <div class="info-grid">
                <div class="info-card">
                    <div class="info-label">Full Name</div>
                    <div class="info-value"><?= htmlspecialchars($user['full_name']) ?></div>
                </div>
                <div class="info-card">
                    <div class="info-label">Email Address</div>
                    <div class="info-value"><?= htmlspecialchars($user['email']) ?></div>
                </div>
                <div class="info-card">
                    <div class="info-label">Phone Number</div>
                    <div class="info-value"><?= htmlspecialchars($user['phone'] ?? '—') ?></div>
                </div>
                <div class="info-card">
                    <div class="info-label">Role</div>
                    <div class="info-value">
                        <span class="role-badge"><?= htmlspecialchars(ucwords($user['role'])) ?></span>
                    </div>
                </div>
            </div>

            <!-- Additional Details -->
            <div class="info-grid">
                <div class="info-card">
                    <div class="info-label">Department</div>
                    <div class="info-value"><?= htmlspecialchars($user['department'] ?? '—') ?></div>
                </div>
                <div class="info-card">
                    <div class="info-label">User Code</div>
                    <div class="info-value"><strong><?= htmlspecialchars($user['user_code']) ?></strong></div>
                </div>
                <div class="info-card">
                    <div class="info-label">Account Created</div>
                    <div class="info-value">
                        <?= $user['created_at'] ? date('d M Y, h:i A', strtotime($user['created_at'])) : '—' ?>
                    </div>
                </div>
                <div class="info-card">
                    <div class="info-label">Last Updated</div>
                    <div class="info-value">
                        <?= $user['updated_at'] ? date('d M Y, h:i A', strtotime($user['updated_at'])) : 'Never' ?>
                    </div>
                </div>
            </div>

            <!-- Action Buttons -->
            <div style="text-align:center; margin-top:50px;">
                <a href="edit_user.php?id=<?= $user_id ?>" class="btn btn-edit">
                    <i class="fas fa-edit"></i> Edit User
                </a>
                <a href="user-list.php" class="btn btn-back">
                    <i class="fas fa-arrow-left"></i> Back to User List
                </a>
            </div>
        </div>
    </div>
</div>

</body>
</html>

<?php $conn->close(); ?>