<!--#include file="./includes/sql.asp"-->
<%
'=============================================================================
' FILE: insert_all_printer_machines.asp
' PURPOSE: Insert ALL printer machines from printers table
' CREATED: 2025-10-20
'
' This will create machine records for all printers that don't already exist
'=============================================================================

Response.Write("<h1>Insert All Printer Machines</h1>")

Dim strSQL, executeInsert, totalQualifying, totalExisting, totalToInsert, rowCount

' Check if we should execute (look for ?execute=1 in URL)
executeInsert = (Request.QueryString("execute") = "1")

If Not executeInsert Then
    Response.Write("<div style='background:#fff3cd; padding:15px; margin:10px 0; border:1px solid #ffc107;'>")
    Response.Write("<h3>PREVIEW MODE</h3>")
    Response.Write("<p>This page is in PREVIEW mode. Review the printers below, then click the button to execute the INSERT.</p>")
    Response.Write("</div>")
End If

' Check 1: How many printers qualify
Response.Write("<h2>Step 1: Qualifying Printers</h2>")
strSQL = "SELECT COUNT(*) as cnt FROM printers p " &_
         "INNER JOIN machines m ON p.machineid = m.machineid " &_
         "WHERE p.isactive = 1 " &_
         "AND m.isactive = 1 " &_
         "AND m.mapleft IS NOT NULL " &_
         "AND m.maptop IS NOT NULL"
Set rs = objConn.Execute(strSQL)
totalQualifying = rs("cnt")
Response.Write("<p><strong>Total printers that qualify:</strong> " & totalQualifying & "</p>")
rs.Close

' Check 2: How many already exist
Response.Write("<h2>Step 2: Already Exist</h2>")
strSQL = "SELECT COUNT(*) as cnt FROM machines WHERE machinetypeid = 15"
Set rs = objConn.Execute(strSQL)
totalExisting = rs("cnt")
Response.Write("<p><strong>Printer machines already in database:</strong> " & totalExisting & "</p>")
rs.Close

' Check 3: How many will be inserted (printers that don't already have machine records)
Response.Write("<h2>Step 3: Will Be Inserted</h2>")
strSQL = "SELECT COUNT(*) as cnt FROM printers p " &_
         "INNER JOIN machines m ON p.machineid = m.machineid " &_
         "WHERE p.isactive = 1 " &_
         "AND m.isactive = 1 " &_
         "AND m.mapleft IS NOT NULL " &_
         "AND m.maptop IS NOT NULL " &_
         "AND NOT EXISTS (" &_
         "    SELECT 1 FROM machines m2 " &_
         "    WHERE m2.machinenumber = CONCAT(m.machinenumber, '-PRINTER')" &_
         ")"
Set rs = objConn.Execute(strSQL)
totalToInsert = rs("cnt")
Response.Write("<p><strong>Printers that will be inserted:</strong> " & totalToInsert & "</p>")
rs.Close

' Show preview of what will be inserted
Response.Write("<h2>Step 4: Preview (First 20)</h2>")
strSQL = "SELECT " &_
         "p.printerid, " &_
         "p.printercsfname, " &_
         "p.printerwindowsname, " &_
         "m.machinenumber, " &_
         "m.alias, " &_
         "m.mapleft, " &_
         "m.maptop, " &_
         "p.ipaddress, " &_
         "CONCAT(m.machinenumber, '-PRINTER') AS new_machinenumber, " &_
         "COALESCE(NULLIF(NULLIF(p.printercsfname, ''), 'NONE'), p.printerwindowsname, m.alias, m.machinenumber) AS new_alias " &_
         "FROM printers p " &_
         "INNER JOIN machines m ON p.machineid = m.machineid " &_
         "WHERE p.isactive = 1 " &_
         "AND m.isactive = 1 " &_
         "AND m.mapleft IS NOT NULL " &_
         "AND m.maptop IS NOT NULL " &_
         "AND NOT EXISTS (" &_
         "    SELECT 1 FROM machines m2 " &_
         "    WHERE m2.machinenumber = CONCAT(m.machinenumber, '-PRINTER')" &_
         ") " &_
         "ORDER BY m.machinenumber " &_
         "LIMIT 20"
Set rs = objConn.Execute(strSQL)

Response.Write("<table border='1' style='border-collapse:collapse; width:100%'>")
Response.Write("<tr style='background:#f0f0f0'>")
Response.Write("<th>Printer ID</th>")
Response.Write("<th>CSF Name</th>")
Response.Write("<th>Windows Name</th>")
Response.Write("<th>Current Machine</th>")
Response.Write("<th>NEW Machine Number</th>")
Response.Write("<th>NEW Alias</th>")
Response.Write("<th>IP Address</th>")
Response.Write("<th>Map Coords</th>")
Response.Write("</tr>")

rowCount = 0
While Not rs.EOF
    rowCount = rowCount + 1
    Response.Write("<tr>")
    Response.Write("<td>" & rs("printerid") & "</td>")
    Response.Write("<td>" & Server.HTMLEncode(rs("printercsfname") & "") & "</td>")
    Response.Write("<td>" & Server.HTMLEncode(rs("printerwindowsname") & "") & "</td>")
    Response.Write("<td>" & Server.HTMLEncode(rs("machinenumber") & "") & "</td>")
    Response.Write("<td style='background:#d4edda'><strong>" & Server.HTMLEncode(rs("new_machinenumber") & "") & "</strong></td>")
    Response.Write("<td style='background:#d4edda'><strong>" & Server.HTMLEncode(rs("new_alias") & "") & "</strong></td>")
    Response.Write("<td>" & Server.HTMLEncode(rs("ipaddress") & "") & "</td>")
    Response.Write("<td>" & rs("mapleft") & ", " & rs("maptop") & "</td>")
    Response.Write("</tr>")
    rs.MoveNext
Wend
Response.Write("</table>")
rs.Close

If rowCount = 0 Then
    Response.Write("<p style='color:green; font-weight:bold'>&#10003; All printers already have machine records! Nothing to insert.</p>")
Else
    If CLng(totalToInsert) > 20 Then
        Response.Write("<p><em>Showing first 20 of " & totalToInsert & " total printers...</em></p>")
    End If
End If

' Execute the INSERT if requested
If executeInsert = True And CLng(totalToInsert) > 0 Then
    Response.Write("<hr>")
    Response.Write("<h2 style='color:#d9534f'>EXECUTING INSERT...</h2>")

    On Error Resume Next

    strSQL = "INSERT INTO machines (" &_
             "machinenumber, alias, machinetypeid, mapleft, maptop, " &_
             "isactive, businessunitid, modelnumberid, ipaddress1" &_
             ") " &_
             "SELECT " &_
             "CONCAT(m.machinenumber, '-PRINTER') AS machinenumber, " &_
             "COALESCE(NULLIF(NULLIF(p.printercsfname, ''), 'NONE'), p.printerwindowsname, m.alias, m.machinenumber) AS alias, " &_
             "15 AS machinetypeid, " &_
             "m.mapleft, " &_
             "m.maptop, " &_
             "1 AS isactive, " &_
             "m.businessunitid, " &_
             "p.modelid AS modelnumberid, " &_
             "p.ipaddress AS ipaddress1 " &_
             "FROM printers p " &_
             "INNER JOIN machines m ON p.machineid = m.machineid " &_
             "WHERE p.isactive = 1 " &_
             "AND m.isactive = 1 " &_
             "AND m.mapleft IS NOT NULL " &_
             "AND m.maptop IS NOT NULL " &_
             "AND NOT EXISTS (" &_
             "    SELECT 1 FROM machines m2 " &_
             "    WHERE m2.machinenumber = CONCAT(m.machinenumber, '-PRINTER')" &_
             ")"

    objConn.Execute(strSQL)

    If Err.Number <> 0 Then
        Response.Write("<div style='background:#f8d7da; padding:15px; margin:10px 0; border:1px solid #f5c6cb;'>")
        Response.Write("<h3 style='color:#721c24'>ERROR!</h3>")
        Response.Write("<p>Error Number: " & Err.Number & "</p>")
        Response.Write("<p>Error Description: " & Server.HTMLEncode(Err.Description) & "</p>")
        Response.Write("</div>")
        Err.Clear
    Else
        Response.Write("<div style='background:#d4edda; padding:15px; margin:10px 0; border:1px solid #c3e6cb;'>")
        Response.Write("<h3 style='color:#155724'>SUCCESS!</h3>")
        Response.Write("<p>" & totalToInsert & " printer machine(s) were inserted successfully!</p>")
        Response.Write("</div>")

        ' Show sample of what was inserted
        Response.Write("<h3>Sample of Inserted Records:</h3>")
        strSQL = "SELECT m.machineid, m.machinenumber, m.alias, mt.machinetype, c.address as ipaddress, m.mapleft, m.maptop " &_
                 "FROM machines m " &_
                 "INNER JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid " &_
                 "LEFT JOIN communications c ON m.machineid = c.machineid AND c.comstypeid = 1 " &_
                 "WHERE m.machinetypeid = 15 " &_
                 "ORDER BY m.machineid DESC " &_
                 "LIMIT 10"
        Set rs = objConn.Execute(strSQL)

        Response.Write("<table border='1' style='border-collapse:collapse'>")
        Response.Write("<tr style='background:#f0f0f0'>")
        Response.Write("<th>Machine ID</th><th>Machine Number</th><th>Alias</th><th>Type</th><th>IP</th><th>Map</th>")
        Response.Write("</tr>")
        While Not rs.EOF
            Response.Write("<tr>")
            Response.Write("<td>" & rs("machineid") & "</td>")
            Response.Write("<td>" & Server.HTMLEncode(rs("machinenumber") & "") & "</td>")
            Response.Write("<td>" & Server.HTMLEncode(rs("alias") & "") & "</td>")
            Response.Write("<td>" & Server.HTMLEncode(rs("machinetype") & "") & "</td>")
            Response.Write("<td>" & Server.HTMLEncode(rs("ipaddress") & "") & "</td>")
            Response.Write("<td>" & rs("mapleft") & ", " & rs("maptop") & "</td>")
            Response.Write("</tr>")
            rs.MoveNext
        Wend
        Response.Write("</table>")
        rs.Close
    End If

    On Error Goto 0
End If

' Show execute button if there are printers to insert and not executing
If executeInsert <> True And CLng(totalToInsert) > 0 Then
    ' Show execute button
    Response.Write("<hr>")
    Response.Write("<div style='background:#d1ecf1; padding:15px; margin:10px 0; border:1px solid #bee5eb;'>")
    Response.Write("<h3>Ready to Execute?</h3>")
    Response.Write("<p>This will insert <strong>" & totalToInsert & " printer machine(s)</strong> into the database.</p>")
    Response.Write("<form method='get' action='insert_all_printer_machines.asp'>")
    Response.Write("<input type='hidden' name='execute' value='1'>")
    Response.Write("<button type='submit' style='background:#28a745; color:white; padding:10px 20px; font-size:16px; border:none; cursor:pointer; border-radius:5px;'>")
    Response.Write("&#10003; Execute INSERT for All " & totalToInsert & " Printers")
    Response.Write("</button>")
    Response.Write("</form>")
    Response.Write("</div>")
End If

objConn.Close
%>
