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

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

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$user = mysqli_query($conn, "SELECT * FROM users WHERE id = $id");
$user_data = mysqli_fetch_assoc($user);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View User - Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body>
    <?php include "includes/sidebar.php"; ?>
    
    <div class="main-content">
        <div class="container-fluid">
            <h2 class="mb-4">User Details</h2>
            
            <div class="card">
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <h5>User Information</h5>
                            <p><strong>ID:</strong> <?php echo $user_data['id']; ?></p>
                            <p><strong>Name:</strong> <?php echo htmlspecialchars($user_data['name']); ?></p>
                            <p><strong>Email:</strong> <?php echo htmlspecialchars($user_data['email']); ?></p>
                            <p><strong>Phone:</strong> <?php echo htmlspecialchars($user_data['phone'] ?? 'N/A'); ?></p>
                            <p><strong>Registered:</strong> <?php echo date('M d, Y', strtotime($user_data['created_at'])); ?></p>
                            <p><strong>Status:</strong> 
                                 <?php if(isset($user_data['status']) && $user_data['status'] == 'blocked'): ?>
                                     <span class="badge bg-danger"><i class="fas fa-ban me-1"></i>Blocked</span>
                                 <?php else: ?>
                                     <span class="badge bg-success"><i class="fas fa-check-circle me-1"></i>Active</span>
                                 <?php endif; ?>
                             </p>
                        </div>
                        <div class="col-md-6">
                            <h5>Address</h5>
                            <p><?php echo nl2br(htmlspecialchars($user_data['address'] ?? 'Not provided')); ?></p>
                        </div>
                    </div>
                    
                    <div class="d-flex gap-2 mt-4">
                        <a href="manage-users.php" class="btn btn-secondary">
                            <i class="fas fa-arrow-left me-2"></i>Back to Users
                        </a>
                        <?php if(isset($user_data['status']) && $user_data['status'] == 'blocked'): ?>
                            <a href="unblock-user.php?id=<?php echo $user_data['id']; ?>" 
                               class="btn btn-success"
                               onclick="return confirm('Are you sure you want to unblock this user account?')">
                                <i class="fas fa-unlock me-2"></i>Unblock User Account
                            </a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>