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

$code = isset($_GET['code']) ? $_GET['code'] : '';
$safe_code = urlencode($code);

// Use EXACT SAME PARAMETERS as print_barcode1.php
$barcode_url =
    "BCG/html/image.php?filetype=PNG"
    . "&dpi=72"
    . "&scale=1"
    . "&rotation=0"
    . "&font_family=Arial.ttf"
    . "&font_size=8"
    . "&text={$safe_code}"
    . "&thickness=30"
    . "&start=NULL"
    . "&code=BCGcode128";
?>
<?php include('header.php'); ?>

<div class="page-title">
    <div class="title_left">
        <h3>
            <small>Home / Books</small> View 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> Barcode Image</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">

                <center>
                    <div style="width: 450px; background:#fff; margin-top:40px; border-radius:8px; box-shadow:1px 1px 5px rgba(0,0,0,0.2); padding:20px;">

                        <h3>Generated Barcode</h3>

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

                        <!-- BARCODE IMAGE (same natural size as print_barcode1.php) -->
                        <img
                            id="barcodeImg"
                            src="<?php echo htmlspecialchars($barcode_url, ENT_QUOTES, 'UTF-8'); ?>"
                            alt="Barcode"
                            style="border:2px solid black; padding:10px;"
                        />

                        <br><br>

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

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

                        <a class="btn btn-primary" href="book.php">
                            <i class="icon-ok"></i> OK
                        </a>

                        <br><br>

                    </div>
                </center>

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

<script>
// Same URL used for screen and print
const BARCODE_URL = <?php echo json_encode($barcode_url); ?>;

function printBarcode() {
    var win = window.open('', '_blank', 'width=600,height=400');

    win.document.write(`
        <html>
        <head>
            <title>Print Barcode</title>
            <style>
                @page {
                    margin: 0; /* minimal margins for label/sticker */
                }
                body {
                    margin: 0;
                    padding: 0;
                    text-align: center;
                    font-family: Arial, sans-serif;
                }
                h2 {
                    margin: 5px 0 5px 0;
                    font-size: 14px;
                    font-weight: bold;
                }
                img {
                    margin: 0 auto;
                    display: block;
                    /* no width/height here: use natural size from generator */
                }
            </style>
        </head>
        <body>
            <h2>MILIND COLLEGE</h2>
            <img src="` + BARCODE_URL + `" alt="Barcode">
        </body>
        </html>
    `);

    win.document.close();

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

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