<?php
session_start();
require_once '../config/database.php';
require_once '../includes/site_settings.php';

// Fetch site settings
$settings = get_all_settings($pdo);
$site_name = $settings['site_name'] ?? 'বিদ্যা নিকেতন';
$logo_image = $settings['logo_image'] ?? '../assets/images/logo.png';

// Check for student ID
if (!isset($_GET['id'])) {
    die('ছাত্রের আইডি পাওয়া যায়নি।');
}

$student_id = $_GET['id'];

// Fetch student data
$stmt = $pdo->prepare("
    SELECT s.*, c.name as class_name, c.section
    FROM students s
    LEFT JOIN classes c ON s.class_id = c.id
    WHERE s.id = ?
");
$stmt->execute([$student_id]);
$student = $stmt->fetch();

if (!$student) {
    die('ছাত্র খুঁজে পাওয়া যায়নি।');
}

?>
<?php
require_once '../includes/sidebar_layout.php';
$user_role = $_SESSION['user_role'];
$user_name = $_SESSION['user_name'];
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Student ID Card</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="../assets/css/jessoreboard-theme.css" rel="stylesheet">
    <link href="../assets/css/sidebar.css" rel="stylesheet">
    <style>
        @media print {
            .no-print {
                display: none;
            }
            .id-card-container {
                justify-content: flex-start;
            }
        }
        body {
            background-color: #f0f2f5;
            font-family: 'Hind Siliguri', sans-serif;
        }
        .id-card-container {
            display: flex;
            justify-content: center;
            align-items: flex-start;
            padding: 20px;
        }
        .id-card {
            width: 320px;
            height: 500px;
            background: #fff;
            border-radius: 20px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.15);
            padding: 20px;
            text-align: center;
            border: 1px solid #eee;
        }
        .id-card-header img {
            max-height: 60px;
            margin-bottom: 10px;
        }
        .id-card-header h5 {
            font-weight: 700;
            font-size: 1.2rem;
            color: #333;
        }
        .student-photo {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            object-fit: cover;
            border: 5px solid #fff;
            box-shadow: 0 0 15px rgba(0,0,0,0.2);
            margin: 0 auto 15px;
        }
        .student-name {
            font-size: 1.4rem;
            font-weight: 700;
            color: #0056b3;
            margin-bottom: 5px;
        }
        .student-details p {
            margin-bottom: 5px;
            font-size: 0.9rem;
        }
        .id-card-footer {
            margin-top: 20px;
        }
        .signature {
            border-top: 1px dashed #ccc;
            padding-top: 5px;
            margin-top: 25px;
            font-size: 0.8rem;
            color: #555;
        }
        .barcode {
            margin-top: 15px;
        }
    </style>
</head>
<body>
    <div class="sidebar-container">
        <?php echo renderSidebar('index_with_sidebar.php', $user_role, $user_name); ?>
        <div class="main-content">
            <div class="container-fluid">
                <div class="my-3 text-center no-print">
                    <button class="btn btn-primary" onclick="window.print()">
                        <i class="fas fa-print"></i> প্রিন্ট করুন
                    </button>
                    <a href="list.php" class="btn btn-secondary">ছাত্র তালিকায় ফিরে যান</a>
                </div>
                <div class="id-card-container">
                    <div class="id-card">
                        <div class="id-card-header">
                            <img src="../<?php echo htmlspecialchars($logo_image); ?>" alt="Logo">
                            <h5><?php echo htmlspecialchars($site_name); ?></h5>
                        </div>

                        <img src="../<?php echo htmlspecialchars($student['photo'] ?? 'assets/images/default-avatar.png'); ?>" class="student-photo" alt="Student Photo">

                        <h4 class="student-name"><?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?></h4>

                        <div class="student-details">
                            <p><strong>ক্লাস:</strong> <?php echo htmlspecialchars($student['class_name']); ?></p>
                            <p><strong>রোল:</strong> <?php echo htmlspecialchars($student['roll_number']); ?></p>
                            <p><strong>সেশন:</strong> <?php echo htmlspecialchars($student['session']); ?></p>
                            <p><strong>রক্তের গ্রুপ:</strong> <?php echo htmlspecialchars($student['blood_group'] ?? 'N/A'); ?></p>
                        </div>

                        <div class="id-card-footer">
                            <div class="signature">
                                কর্তৃপক্ষ
                            </div>
                            <div class="barcode">
                                <svg id="barcode"></svg>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.5/dist/JsBarcode.all.min.js"></script>
    <script>
        JsBarcode("#barcode", "<?php echo htmlspecialchars($student['id']); ?>", {
            format: "CODE128",
            displayValue: true,
            fontSize: 18,
            width: 2,
            height: 40,
        });
    </script>
</body>
</html>
