<?php


$query = "SELECT * FROM `administratives` LIMIT 1";
$run_query = mysqli_query($connection, $query);
if ($run_query && mysqli_num_rows($run_query) > 0) {
    $result = mysqli_fetch_assoc($run_query);
    $school_name   = $result['school_name'] ?? 'SCHOOL NAME';
    $school_motto  = $result['school_motto'] ?? '';
    $school_logo   = $result['school_logo'] ?? '';
    $school_address= $result['school_address'] ?? '';
    $school_phone  = $result['school_phone'] ?? '';
    $school_mail   = $result['school_mail'] ?? '';
    $school_stamp  = $result['school_stamp'] ?? '';
    $signature     = $result['signature'] ?? 'assets/signature.png';
}

// ====== FETCH CLASSES ======
$query = "SELECT * FROM `classes`";
$run_query = mysqli_query($connection, $query);
$classes = [];
while ($row = mysqli_fetch_assoc($run_query)) {
    $classes[] = $row['classes'];
}

// ====== PROCESS REQUEST ======
$students = [];
$is_class_selection = false;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $reg_number = $_POST['reg_number'] ?? null;
    $class_id   = $_POST['class_id'] ?? null;

    if (!empty($reg_number)) {
        $stmt = $connection->prepare("
            SELECT id, firstname, othername, lastname, reg_number, class, passport, date_of_reg 
            FROM students WHERE reg_number = ?
        ");
        $stmt->bind_param("s", $reg_number);
        $stmt->execute();
        $result = $stmt->get_result();
        while ($row = $result->fetch_assoc()) {
            $students[] = $row;
        }
    } elseif (!empty($class_id)) {
        $is_class_selection = true;
        $stmt = $connection->prepare("
            SELECT id, firstname, othername, lastname, reg_number, class, passport, date_of_reg 
            FROM students WHERE class = ? AND status = 'active'
        ");
        $stmt->bind_param("s", $class_id);
        $stmt->execute();
        $result = $stmt->get_result();
        while ($row = $result->fetch_assoc()) {
            $students[] = $row;
        }
    }
}
?>

<style>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap');

body {
    font-family: 'Poppins', sans-serif;
    background: #eef2f7;
}

/* === CARD SIZE === */
.id-card,
.id-card-back {
    width: 430px;
    min-height: 260px;
    border-radius: 12px;
    margin: 20px auto;
    overflow: hidden;
    background: #ffffff;
    box-shadow: 0 4px 10px rgba(0,0,0,0.15);
    position: relative;
}

/* === FRONT TOP BLUE BAR === */
.id-card-header {
    background: #0d6efd;
    height: 70px;
    border-radius: 12px 12px 0 0;
    display: flex;
    align-items: center;
    padding-left: 15px;
    gap: 15px;
}
.id-card-header img {
    width: 55px;
    height: 55px;
    border-radius: 6px;
    object-fit: cover;
    background: #fff;
}
.header-text {
    text-align: center;
}
.header-text .name {
    color: #fff;
    font-size: 19px;
    font-weight: 700;
}
.header-text .address {
    color: #e8e8e8;
    font-size: 13px;
    margin-top: -4px;
}

/* === WATERMARK (FRONT ONLY) === */
.id-card::before {
    content: "";
    position: absolute;
    inset: 0;
    background-image: url('../<?= $school_logo ?>');
    background-repeat: no-repeat;
    background-position: center;
    background-size: 350px;
    opacity: 0.05;
    z-index: 0;
}

/* === BODY === */
.id-card-body {
    padding: 15px 20px;
    position: relative;
    z-index: 2;
    display: flex;
    justify-content: space-between;
}

/* Student details */
.student-details div {
    margin-bottom: 4px;
    font-size: 13px;
}

/* Passport */
.passport-box {
    width: 120px;
    height: 140px;
    border: 3px solid #0d6efd;
    border-radius: 8px;
    overflow: hidden;
}
.passport-box img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* === BACK SIDE === */
.id-card-back {
    background: linear-gradient(135deg, #0d6efd 0%, #6ea8fe 100%);
    color: white;
    padding: 20px;
}
.id-card-back * {
    text-align: center;
}
.qr-container img {
    width: 90px;
    padding: 5px;
    background: #fff;
    border-radius: 6px;
}
.signature-container img {
    width: 100px;
    margin-top: 10px;
}

/* Print */
@media print {
    .no-print { display: none; }
    .id-card, .id-card-back {
        box-shadow: none;
        page-break-inside: avoid;
    }
}
</style>




<div class="container">
    <div class="no-print mb-4">
        <h3 class="text-center fw-bold text-primary mb-3">STUDENT ID CARD GENERATOR</h3>

        <form method="POST" class="row g-4">
            <div class="col-md-4">
                <label>Single Student (Reg Number)</label>
                <input type="text" name="reg_number" class="form-control" placeholder="Enter Reg Number">
            </div>

            <div class="col-md-4">
                <label>Bulk (By Class)</label>
                <select name="class_id" class="form-control">
                    <option value="">Select Class</option>
                    <?php foreach ($classes as $cls): ?>
                        <option value="<?= htmlspecialchars($cls) ?>"><?= htmlspecialchars($cls) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="col-md-4 d-flex align-items-end">
                <button type="submit" class="btn btn-primary w-100">Generate ID Cards</button>
            </div>
        </form>
    </div>


    <div class="<?= $is_class_selection ? 'd-flex flex-wrap justify-content-center' : 'row' ?>">

    <?php if (!empty($students)): ?>
        <?php foreach ($students as $student):

            $fullname = htmlspecialchars($student['firstname'] . ' ' . $student['lastname']);
            $regno = htmlspecialchars($student['reg_number']);
            $datofreg = htmlspecialchars($student['date_of_reg']);
            $class = htmlspecialchars($student['class']);

            $qrdata = urlencode($regno);

            $qrurl = "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=$qrdata";

            $passportPath =  ($student['passport'] ?: "default.jpg");
           // $passportPath = "/{$passportPath}";

        ?>

       <!-- FRONT -->
<div class="id-card">
    <div class="id-card-header">
        <img src="../<?= htmlspecialchars($school_logo) ?>" alt="School Logo">
        <div class="header-text">
            <div class="name"><?= htmlspecialchars($school_name) ?></div>
            <div class="address">STUDENT/PUPIL IDENTITY CARD</div>
        </div>
    </div>

    <div class="id-card-body">
        <div class="student-details">
            <div><b>Name:</b> <?= $fullname ?></div>
            <div><b>Role:</b> Student/Pupil</div>
            <div><b>Class:</b> <?= $class ?></div>
            <div><b>Reg No:</b> <?= $regno ?></div>
            <div><b>Issue Date:</b> <?= $datofreg ?></div>
        </div>

        <div class="passport-box">
            <img src="../<?= $passportPath ?>" alt="Passport">
        </div>
    </div>
</div>

<!-- BACK -->
<div class="id-card-back">
    <div class="qr-container">
        <img src="<?= $qrurl ?>" alt="QR Code">
    </div>

    <div class="signature-container">
        <img src="../<?= htmlspecialchars($school_stamp) ?>" alt="School Stamp">
        <p>Authorized Signature</p>
    </div>

    <p style="font-size:12px; margin-top:10px;">
        This ID card remains the property of <?= htmlspecialchars($school_name) ?>.<br>
        Lost cards should be reported immediately.
    </p>
</div>

        <?php endforeach; ?>

    <?php elseif ($_SERVER['REQUEST_METHOD'] === 'POST'): ?>
        <p class="text-danger fw-semibold text-center">No student found.</p>
    <?php endif; ?>

    </div>

    <?php if (!empty($students)): ?>
    <div class="text-center no-print mt-4">
        <button class="btn btn-success px-5" onclick="window.print()">Print ID Cards</button>
    </div>
    <?php endif; ?>
</div>
