<?php
include('include/dbcon.php');

$school_number = isset($_GET['school_number']) ? $_GET['school_number'] : '';
$safe_school   = mysqli_real_escape_string($con, $school_number);

// Fetch member from DB
$user = null;
if ($safe_school !== '') {
    $res = mysqli_query($con, "SELECT * FROM user WHERE school_number = '$safe_school' LIMIT 1") or die(mysqli_error($con));
    $user = mysqli_fetch_assoc($res);
}

$safe_code = urlencode($school_number);

// 🔹 Barcode parameters (same size as print_barcode.php)
$barcode_url =
    "BCG/html/image.php?filetype=PNG"
    . "&dpi=72"
    . "&scale=1"
    . "&rotation=0"
    . "&font_family=Arial.ttf"
    . "&font_size=10"
    . "&text={$safe_code}"
    . "&thickness=30"
    . "&start=NULL"
    . "&code=BCGcode128";
?>
<?php include('header.php'); ?>

<div class="page-title">
    <div class="title_left">
        <h3>
            <small>Home / Members</small> Member Barcode
        </h3>
    </div>
</div>
<div class="clearfix"></div>

<div class="row">
    <div class="col-md-12 col-sm-12 col-xs-12">
        <div class="x_panel">
            <div class="x_title">
                <h2><i class="fa fa-barcode"></i> Member Barcode</h2>
                <ul class="nav navbar-right panel_toolbox">
                    <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a></li>
                    <li><a class="close-link"><i class="fa fa-close"></i></a></li>
                </ul>
                <div class="clearfix"></div>
            </div>

            <div class="x_content">

                <?php if (!$user): ?>
                    <div class="alert alert-danger">
                        Member not found for ID: <strong><?php echo htmlspecialchars($school_number); ?></strong>
                    </div>
                    <a href="user.php" class="btn btn-primary">Back to Members</a>
                <?php else: ?>

                <center>
                    <div style="
                        width: 400px;
                        background:#fff;
                        margin-top:40px;
                        border-radius:8px;
                        box-shadow:1px 1px 5px rgba(0,0,0,0.2);
                        padding:20px;
                        text-align:center;
                        font-family:Arial, sans-serif;
                    ">

                        <h3 style="margin: 0 0 10px 0;">Member Barcode</h3>

                        <p style="margin: 0 0 5px 0;">
                            <strong><?php echo htmlspecialchars($user['firstname'] . ' ' . $user['middlename'] . ' ' . $user['lastname']); ?></strong><br>
                            ID: <?php echo htmlspecialchars($user['school_number']); ?>
                        </p>

                        <hr style="border:1px solid #000; width:90%;"/>

                        <!-- Barcode shown on screen -->
                        <img
                            id="memberBarcodeImg"
                            src="<?php echo htmlspecialchars($barcode_url, ENT_QUOTES, 'UTF-8'); ?>"
                            alt="Member Barcode"
                            style="border:2px solid black; padding:10px;"
                        />

                        <br><br>

                        <hr style="border:1px solid #000; width:90%;"/>

                        <button type="button" class="btn btn-success" onclick="printMemberBarcode()">
                            <i class="fa fa-print"></i> Print Barcode
                        </button>

                        <a class="btn btn-primary" href="user.php">
                            <i class="fa fa-check"></i> Done
                        </a>

                        <br><br>

                    </div>
                </center>

                <?php endif; ?>

            </div>
        </div>
    </div>
</div>

<script>
const MEMBER_BARCODE_URL = <?php echo json_encode($barcode_url); ?>;

// 🔹 PRINT BARCODE ONLY — No text, no ID, no college name
function printMemberBarcode() {
    var win = window.open('', '_blank', 'width=600,height=400');

    win.document.write(`
        <html>
        <head>
            <title>Print Barcode</title>
            <style>
                @page {
                    margin: 0;
                }
                body {
                    margin: 0;
                    padding: 10px;
                    text-align: center;
                }
                img {
                    border: none;
                    padding: 0;
                    margin-top: 10px;
                }
            </style>
        </head>
        <body>
            <img src="` + MEMBER_BARCODE_URL + `" alt="Barcode">
        </body>
        </html>
    `);

    win.document.close();
    win.onload = function () {
        setTimeout(() => {
            win.print();
            win.close();
        }, 500);
    };
}
</script>

<?php include('footer.php'); ?>
