<!--#include file="./includes/sql.asp"-->
<%
' printerlinksgenerator.asp
' Generates installation links for all printers in the database
' Shows both single-click launcher links and direct installer command-line parameters

Response.Write("<!DOCTYPE html>")
Response.Write("<html lang='en'>")
Response.Write("<head>")
Response.Write("    <meta charset='UTF-8'>")
Response.Write("    <meta name='viewport' content='width=device-width, initial-scale=1.0'>")
Response.Write("    <title>Printer Installation Link Generator</title>")
Response.Write("    <style>")
Response.Write("        body { font-family: Arial, sans-serif; max-width: 1400px; margin: 20px auto; padding: 20px; background: #f5f5f5; }")
Response.Write("        h1 { color: #003057; border-bottom: 3px solid #0066cc; padding-bottom: 10px; }")
Response.Write("        .printer-table { width: 100%; background: white; border-collapse: collapse; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }")
Response.Write("        .printer-table th { background: #0066cc; color: white; padding: 12px; text-align: left; font-weight: bold; }")
Response.Write("        .printer-table td { padding: 10px; border-bottom: 1px solid #ddd; }")
Response.Write("        .printer-table tr:hover { background: #f8f8f8; }")
Response.Write("        .csf-badge { background: #0066cc; color: white; padding: 3px 8px; border-radius: 3px; font-size: 11px; font-weight: bold; }")
Response.Write("        .copy-btn { background: #0066cc; color: white; border: none; padding: 5px 10px; border-radius: 3px; cursor: pointer; font-size: 12px; }")
Response.Write("        .copy-btn:hover { background: #0052a3; }")
Response.Write("        .link-input { width: 100%; padding: 5px; font-family: 'Courier New', monospace; font-size: 11px; border: 1px solid #ddd; }")
Response.Write("        .filter-box { background: white; padding: 15px; margin-bottom: 20px; border-radius: 5px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }")
Response.Write("        .filter-box input { padding: 8px; width: 300px; border: 1px solid #ddd; border-radius: 3px; }")
Response.Write("        .info-box { background: #fff3cd; border-left: 4px solid #ffc107; padding: 15px; margin-bottom: 20px; }")
Response.Write("        #shareToast { position: fixed; top: 80px; right: 20px; z-index: 9999; min-width: 300px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 15px 20px; border-radius: 8px; box-shadow: 0 4px 20px rgba(0,0,0,0.3); display: none; }")
Response.Write("        @keyframes toastSlideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }")
Response.Write("        @keyframes toastSlideOut { from { transform: translateX(0); opacity: 1; } to { transform: translateX(100%); opacity: 0; } }")
Response.Write("    </style>")
Response.Write("</head>")
Response.Write("<body>")
Response.Write("    <div id='shareToast'><div style='display: flex; align-items: center;'><span style='font-size: 24px; margin-right: 12px;'>&#10003;</span><div><strong style='display: block; margin-bottom: 5px;'>Link Copied!</strong><span style='font-size: 13px; opacity: 0.9;'>This link will show the search term and highlight the result</span></div></div></div>")
Response.Write("    <h1>Printer Installation Link Generator</h1>")

Response.Write("    <div class='info-box'>")
Response.Write("        <strong>How to use:</strong>")
Response.Write("        <ul style='margin: 10px 0;'>")
Response.Write("            <li><strong>Web Link:</strong> Use for website buttons - downloads a .bat file that installs the printer</li>")
Response.Write("            <li><strong>Command Line:</strong> For documentation or manual installation instructions</li>")
Response.Write("            <li><strong>QR Code:</strong> Copy the web link and generate a QR code to print and place on the physical printer</li>")
Response.Write("        </ul>")
Response.Write("    </div>")

Response.Write("    <div class='filter-box'>")
Response.Write("        <label><strong>Filter printers:</strong></label> ")
Response.Write("        <input type='text' id='filterInput' onkeyup='filterTable()' placeholder='Search by name, CSF, location, vendor...' />")
Response.Write("    </div>")

Response.Write("    <table class='printer-table' id='printerTable'>")
Response.Write("        <thead>")
Response.Write("            <tr>")
Response.Write("                <th>Printer Name</th>")
Response.Write("                <th>Location</th>")
Response.Write("                <th>Model</th>")
Response.Write("                <th>Web Link (for buttons)</th>")
Response.Write("                <th>Command Line</th>")
Response.Write("            </tr>")
Response.Write("        </thead>")
Response.Write("        <tbody>")

' Query all active HP and Xerox printers
Dim strSQL, rs
strSQL = "SELECT p.printerid, p.printerwindowsname, p.printercsfname, p.fqdn, p.ipaddress, " & _
         "v.vendor, m.modelnumber, p.isactive, ma.alias, ma.machinenumber " & _
         "FROM printers p " & _
         "LEFT JOIN models m ON p.modelid = m.modelnumberid " & _
         "LEFT JOIN vendors v ON m.vendorid = v.vendorid " & _
         "LEFT JOIN machines ma ON p.machineid = ma.machineid " & _
         "WHERE p.isactive = 1 " & _
         "AND (v.vendor = 'HP' OR v.vendor = 'Xerox') " & _
         "AND ((p.fqdn IS NOT NULL AND p.fqdn != '') OR (p.ipaddress IS NOT NULL AND p.ipaddress != '' AND p.ipaddress != 'USB')) " & _
         "ORDER BY " & _
         "CASE WHEN p.printercsfname IS NOT NULL AND p.printercsfname != '' AND p.printercsfname != 'NONE' THEN 0 ELSE 1 END, " & _
         "p.printercsfname, COALESCE(ma.alias, ma.machinenumber), v.vendor, m.modelnumber"

Set rs = objConn.Execute(strSQL)

Dim printerName, csfName, vendor, model, machineAlias, machineNumber, machineName
Dim webLink, cmdLine

Do While Not rs.EOF
    printerName = rs("printerwindowsname") & ""
    csfName = rs("printercsfname") & ""
    vendor = rs("vendor") & ""
    model = rs("modelnumber") & ""
    machineAlias = rs("alias") & ""
    machineNumber = rs("machinenumber") & ""

    ' Get machine name
    If machineAlias <> "" Then
        machineName = machineAlias
    Else
        machineName = machineNumber
    End If

    ' Generate URLs
    webLink = "https://tsgwp00525.rd.ds.ge.com/shopdb/installprinter.asp?printer=" & Server.URLEncode(printerName)
    cmdLine = "PrinterInstaller.exe /PRINTER=" & printerName

    ' Output table row
    Response.Write("            <tr>")
    Response.Write("                <td>")
    If csfName <> "" And csfName <> "NONE" Then
        Response.Write("                    <span class='csf-badge'>" & Server.HTMLEncode(csfName) & "</span> ")
    End If
    Response.Write(Server.HTMLEncode(printerName))
    Response.Write("                </td>")
    Response.Write("                <td>" & Server.HTMLEncode(machineName) & "</td>")
    Response.Write("                <td>" & Server.HTMLEncode(vendor) & " " & Server.HTMLEncode(model) & "</td>")
    Response.Write("                <td>")
    Response.Write("                    <input type='text' class='link-input' value='" & Server.HTMLEncode(webLink) & "' readonly onclick='this.select()' />")
    Response.Write("                    <button class='copy-btn' onclick='copyToClipboard(""" & Replace(webLink, """", "&quot;") & """)'>Copy</button>")
    Response.Write("                </td>")
    Response.Write("                <td>")
    Response.Write("                    <input type='text' class='link-input' value='" & Server.HTMLEncode(cmdLine) & "' readonly onclick='this.select()' />")
    Response.Write("                    <button class='copy-btn' onclick='copyToClipboard(""" & Replace(cmdLine, """", "&quot;") & """)'>Copy</button>")
    Response.Write("                </td>")
    Response.Write("            </tr>")

    rs.MoveNext
Loop

rs.Close
Set rs = Nothing
objConn.Close

Response.Write("        </tbody>")
Response.Write("    </table>")

Response.Write("    <script>")
Response.Write("        function copyToClipboard(text) {")
Response.Write("            var temp = document.createElement('textarea');")
Response.Write("            temp.value = text;")
Response.Write("            temp.style.position = 'fixed';")
Response.Write("            temp.style.left = '-9999px';")
Response.Write("            document.body.appendChild(temp);")
Response.Write("            temp.select();")
Response.Write("            document.execCommand('copy');")
Response.Write("            document.body.removeChild(temp);")
Response.Write("            showToast();")
Response.Write("        }")
Response.Write("        function showToast() {")
Response.Write("            var toast = document.getElementById('shareToast');")
Response.Write("            toast.style.display = 'block';")
Response.Write("            toast.style.animation = 'toastSlideIn 0.3s ease-out';")
Response.Write("            setTimeout(function() {")
Response.Write("                toast.style.animation = 'toastSlideOut 0.3s ease-out';")
Response.Write("                setTimeout(function() { toast.style.display = 'none'; }, 300);")
Response.Write("            }, 2500);")
Response.Write("        }")
Response.Write("        ")
Response.Write("        function filterTable() {")
Response.Write("            var input = document.getElementById('filterInput');")
Response.Write("            var filter = input.value.toUpperCase();")
Response.Write("            var table = document.getElementById('printerTable');")
Response.Write("            var tr = table.getElementsByTagName('tr');")
Response.Write("            ")
Response.Write("            for (var i = 1; i < tr.length; i++) {")
Response.Write("                var td = tr[i].getElementsByTagName('td');")
Response.Write("                var found = false;")
Response.Write("                for (var j = 0; j < td.length; j++) {")
Response.Write("                    if (td[j]) {")
Response.Write("                        var txtValue = td[j].textContent || td[j].innerText;")
Response.Write("                        if (txtValue.toUpperCase().indexOf(filter) > -1) {")
Response.Write("                            found = true;")
Response.Write("                            break;")
Response.Write("                        }")
Response.Write("                    }")
Response.Write("                }")
Response.Write("                tr[i].style.display = found ? '' : 'none';")
Response.Write("            }")
Response.Write("        }")
Response.Write("    </script>")
Response.Write("</body>")
Response.Write("</html>")
%>
