<%@ Language=VBScript %>
<%
Option Explicit
Dim objConn, rs
%>
<!DOCTYPE html>
<html>
<head>
    <!--#include file="./includes/sql.asp"-->
    <title>Print Badge</title>
    <script src="https://cdn.jsdelivr.net/npm/jsbarcode@3.11.5/dist/JsBarcode.all.min.js"></script>
    <style>
        @page { size: 2.13in 3.38in; margin: 0; }
        body { font-family: Arial, sans-serif; background: #f0f0f0; margin: 0; padding: 20px; }
        .badge-container { width: 2.13in; height: 3.38in; background: white; margin: 0 auto; border: 1px solid #ccc; display: flex; flex-direction: column; align-items: center; padding: 0.15in; box-sizing: border-box; }
        .model-name { font-size: 12pt; font-weight: bold; text-align: center; margin-bottom: 0.1in; color: #000; }
        .machine-image { max-width: 1.8in; max-height: 1.5in; object-fit: contain; margin-bottom: 0.1in; }
        .barcode-container { text-align: center; margin-top: auto; }
        .barcode-container svg { width: 1.8in; height: 0.9in; }
        .machine-number { font-size: 14pt; font-weight: bold; font-family: monospace; margin-top: 0; }
        .print-btn { display: block; margin: 20px auto; padding: 10px 30px; font-size: 16px; cursor: pointer; background: #667eea; color: white; border: none; border-radius: 5px; }
        @media print { .print-btn { display: none; } .badge-container { border: none; margin: 0; } body { background: white; padding: 0; } }
    </style>
</head>
<body>
<%
Dim machineid, strSQL, machineNumber, modelName, machineImage

machineid = Request.QueryString("machineid")
If machineid = "" Then machineid = "0"
If Not IsNumeric(machineid) Then machineid = "0"

strSQL = "SELECT m.machinenumber, mo.modelnumber, mo.image FROM machines m LEFT JOIN models mo ON m.modelnumberid = mo.modelnumberid WHERE m.machineid = " & CLng(machineid)
Set rs = objConn.Execute(strSQL)

If rs.EOF Then
    machineNumber = "NOT FOUND"
    modelName = ""
    machineImage = ""
Else
    machineNumber = rs("machinenumber") & ""
    modelName = rs("modelnumber") & ""
    machineImage = rs("image") & ""
End If
rs.Close
Set rs = Nothing
objConn.Close
%>
<button class="print-btn" onclick="window.print()">Print Badge</button>
<div class="badge-container">
    <div class="model-name"><%=Server.HTMLEncode(modelName)%></div>
<% If machineImage <> "" Then %>
    <img class="machine-image" src="./images/machines/<%=Server.HTMLEncode(machineImage)%>">
<% End If %>
    <div class="barcode-container">
        <svg id="barcode"></svg>
        <div class="machine-number">*<%=Server.HTMLEncode(machineNumber)%>*</div>
    </div>
</div>
<script>
JsBarcode("#barcode", "<%=Server.HTMLEncode(machineNumber)%>", {format:"CODE39",displayValue:false,width:2,height:70,margin:0});
</script>
</body>
</html>