<?php
require('fpdf.php'); // Include FPDF library
include('config.php');
session_start();

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

// Fetch distinct values for filters (if needed for other parts of the page)
$titles_sql = "SELECT DISTINCT title FROM documents";
$titles_result = $conn->query($titles_sql);

$statuses_sql = "SELECT DISTINCT status FROM document_status WHERE status IN ('Read', 'Not Read', 'Submitted', 'Not Submitted')";
$statuses_result = $conn->query($statuses_sql);

$usernames_sql = "SELECT DISTINCT username FROM users";
$usernames_result = $conn->query($usernames_sql);

// Handle PDF generation if ID is provided
if (isset($_GET['id'])) {
    $user_id = $_GET['id'];

    // Fetch user details
    $query = "SELECT username, mobileno, address, pancard, aadharcard, photo, designation, division FROM users WHERE id = ?";
    $stmt = $conn->prepare($query);
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows > 0) {
        $user = $result->fetch_assoc();

        // Create PDF
        $pdf = new FPDF();
        $pdf->AddPage();

        // Set title
        $pdf->SetFont('Arial', 'B', 16);
        $pdf->Cell(190, 10, 'User Details Report', 1, 1, 'C');

        // User details
        $pdf->SetFont('Arial', '', 12);
        $pdf->Cell(50, 10, 'Username:', 1);
        $pdf->Cell(140, 10, $user['username'], 1, 1);

        $pdf->Cell(50, 10, 'Mobile No:', 1);
        $pdf->Cell(140, 10, $user['mobileno'], 1, 1);

        $pdf->Cell(50, 10, 'Address:', 1);
        $pdf->Cell(140, 10, $user['address'], 1, 1);

        $pdf->Cell(50, 10, 'PAN Card:', 1);
        $pdf->Cell(140, 10, $user['pancard'], 1, 1);

        $pdf->Cell(50, 10, 'Aadhar Card:', 1);
        $pdf->Cell(140, 10, $user['aadharcard'], 1, 1);

        $pdf->Cell(50, 10, 'Designation:', 1);
        $pdf->Cell(140, 10, $user['designation'], 1, 1);

        $pdf->Cell(50, 10, 'division:', 1);
        $pdf->Cell(140, 10, $user['division'], 1, 1);

        // Image
        $photo_path = !empty($user['photo']) ? 'uploads/' . $user['photo'] : 'uploads/no-photo.jpg';
        if (!file_exists($photo_path)) {
            $photo_path = 'uploads/no-photo.jpg'; // Fallback image
        }
        $pdf->Image($photo_path, 150, 30, 40, 40);

        // Output PDF
        $pdf->Output('D', "User_Details_{$user['username']}.pdf"); // Download the file
        exit(); // Stop further execution after PDF download

    } else {
        $error = "User not found.";
    }

    $stmt->close();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard</title>
    <!-- Include Font Awesome for Icons -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        /* Basic Page Layout */
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            height: 100vh;
        }

        /* Sidebar Styling */
        .sidebar {
            width: 250px;
            background: #2c3e50;
            color: #fff;
            display: flex;
            flex-direction: column;
            padding: 20px;
            transition: transform 0.3s ease;
            position: relative;
        }

        .sidebar.collapsed {
            transform: translateX(-250px);
        }

        .sidebar h2 {
            margin-bottom: 20px;
            font-size: 1rem;
            font-weight: bold;
        }

        .sidebar a {
            display: block;
            padding: 10px 15px;
            color: #fff;
            text-decoration: none;
            margin-bottom: 5px;
            border-radius: 5px;
            transition: 0.3s ease;
        }

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

        /* Toggle Button Styling */
        .toggle-btn {
            position: absolute;
            top: 50%;
            right: -15px;
            transform: translateY(-50%);
            background: #2c3e50;
            color: #fff;
            border: none;
            padding: 10px;
            border-radius: 5px;
            cursor: pointer;
            z-index: 1000;
            transition: background 0.3s ease;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .toggle-btn:hover {
            background: #1abc9c;
        }

        .toggle-btn i {
            font-size: 1.2rem;
        }

        /* Main Content Styling */
        .content {
            flex: 1;
            padding: 20px;
            overflow-y: auto;
            transition: margin-left 0.3s ease;
        }

        .content.expanded {
            margin-left: 0;
        }

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

        h1 {
            font-size: 1.2rem;
            margin-bottom: 20px;
            color: #333;
        }

        /* Form Styling */
        .form-container {
            background: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            max-width: 600px;
            margin: 0 auto;
        }

        .form-group {
            margin-bottom: 15px;
        }

        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        input[type="text"],
        input[type="number"],
        select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        .btn {
            padding: 8px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
        }

        .btn-primary {
            background: #007bff;
            color: white;
        }

        .btn-primary:hover {
            background: #0056b3;
        }

        .error {
            color: red;
            margin-bottom: 15px;
        }
    </style>
</head>

<body>
    <!-- Sidebar -->
    <div class="sidebar" id="sidebar">
        <!-- Toggle Button -->
        <button class="toggle-btn" onclick="toggleSidebar()">
            <i class="fas fa-caret-square-left"></i>
        </button>

        <h2>Admin Dashboard</h2>
        <a href="dashboard.php">🏠 Dashboard</a>
        <a href="manage_users.php">👥 Manage Users</a>
        <a href="manage_documents.php">📁 Manage Documents</a>
        <a href="attendance.php">⏳ Attendance</a>
        <a href="grievance_dashboard.php">⚖️ Grievance Portal</a>
        <a href="leave_dashboard.php">🌴 Leave Portal</a>
        <a href="payroll.php">💸 Payroll</a>
        <a href="pf.php">📚 Performance Management</a>
        <a href="dashboard_training.php">🎓 Training Portal</a>
        <a href="dashboard_employee.php">🙍‍♂️ Personal Portal</a>
        <a href="logout.php" class="text-danger">🚪 Logout</a>
    </div>

    <!-- Main Content -->
    <div class="content" id="content">
        <h1 class="text-primary text-center">Generate User Report</h1>

        <div class="form-container">
            <?php if (isset($error)): ?>
                <div class="error"><?php echo htmlspecialchars($error); ?></div>
            <?php endif; ?>
            
            <form method="get" action="">
                <div class="form-group">
                    <label for="id">User ID:</label>
                    <input type="number" id="id" name="id" required>
                </div>
                
                <button type="submit" class="btn btn-primary">Generate PDF Report</button>
            </form>
        </div>
    </div>

    <script>
        function toggleSidebar() {
            document.getElementById('sidebar').classList.toggle('collapsed');
            document.getElementById('content').classList.toggle('expanded');
        }
    </script>
</body>
</html>