<%
'=============================================================================
' FILE: displaymachine.asp
' PURPOSE: Display detailed machine information with edit capability
' SECURITY: Parameterized queries, HTML encoding, input validation
' UPDATED: 2025-11-07 - Fixed for Phase 2 schema (machines + communications)
'=============================================================================
%><!DOCTYPE html>
<html lang="en">
<head>
  <!--#include file="./includes/header.asp"-->
  <!--#include file="./includes/sql.asp"-->
  <!--#include file="./includes/validation.asp"-->
  <!--#include file="./includes/db_helpers.asp"-->
  <link rel="stylesheet" href="./leaflet/leaflet.css">
  <script src="./leaflet/leaflet.js"></script>
</head>

<%
	theme = Request.Cookies("theme")
	If theme = "" Then
		theme = "bg-theme1"
	End If

	'=============================================================================
	' SECURITY: Validate machineid parameter
	'=============================================================================
	Dim machineid
	machineid = GetSafeInteger("QS", "machineid", 0, 1, 999999)

	IF machineid = 0 THEN
		objConn.Close
		Response.Redirect("default.asp")
		Response.End
	END IF

	'=============================================================================
	' PHASE 2 SCHEMA: Query machines table with LEFT JOINs for optional data
	' - No more pc/pc_network_interfaces/pc_dualpath_assignments tables
	' - Use communications for network interfaces
	' - Use machinerelationships for dualpath relationships
	' - Use compliance for compliance data
	'=============================================================================
	strSQL = "SELECT machines.*, models.modelnumber, vendors.vendor, " & _
			"businessunits.businessunit, machinetypes.machinetype " & _
			"FROM machines " & _
			"INNER JOIN models ON machines.modelnumberid = models.modelnumberid " & _
			"INNER JOIN vendors ON models.vendorid = vendors.vendorid " & _
			"INNER JOIN businessunits ON machines.businessunitid = businessunits.businessunitid " & _
			"LEFT JOIN machinetypes ON models.machinetypeid = machinetypes.machinetypeid " & _
			"WHERE machines.machineid = ?"

	Set rs = ExecuteParameterizedQuery(objConn, strSQL, Array(machineid))

	' Check if machine exists
	If rs.EOF Then
		rs.Close
		Set rs = Nothing
		objConn.Close
		Response.Redirect("default.asp")
		Response.End
	End If

	'=============================================================================
	' Get primary network interface from communications table
	'=============================================================================
	Dim rsPrimaryNetwork, primaryIP, primaryMAC, primaryHostname, primaryInterface
	primaryIP = ""
	primaryMAC = ""
	primaryHostname = ""
	primaryInterface = ""

	' Get hostname from machines table (for PCs)
	If Not IsNull(rs("hostname")) And rs("hostname") & "" <> "" Then
		primaryHostname = rs("hostname") & ""
	End If

	' Query for primary network interface
	strSQL = "SELECT c.address, c.macaddress, c.interfacename " & _
			 "FROM communications c " & _
			 "INNER JOIN comstypes ct ON c.comstypeid = ct.comstypeid " & _
			 "WHERE c.machineid = ? AND ct.typename = 'Network_Interface' " & _
			 "AND c.isactive = 1 AND c.isprimary = 1 " & _
			 "LIMIT 1"
	Set rsPrimaryNetwork = ExecuteParameterizedQuery(objConn, strSQL, Array(machineid))

	If Not rsPrimaryNetwork.EOF Then
		primaryIP = rsPrimaryNetwork("address") & ""
		If Not IsNull(rsPrimaryNetwork("macaddress")) Then
			primaryMAC = rsPrimaryNetwork("macaddress") & ""
		End If
		If Not IsNull(rsPrimaryNetwork("interfacename")) Then
			primaryInterface = rsPrimaryNetwork("interfacename") & ""
		End If
	End If
	rsPrimaryNetwork.Close
	Set rsPrimaryNetwork = Nothing

	' If no primary, get the first network interface
	If primaryIP = "" Then
		strSQL = "SELECT c.address, c.macaddress, c.interfacename " & _
				 "FROM communications c " & _
				 "INNER JOIN comstypes ct ON c.comstypeid = ct.comstypeid " & _
				 "WHERE c.machineid = ? AND ct.typename = 'Network_Interface' " & _
				 "AND c.isactive = 1 " & _
				 "ORDER BY c.comid ASC LIMIT 1"
		Set rsPrimaryNetwork = ExecuteParameterizedQuery(objConn, strSQL, Array(machineid))

		If Not rsPrimaryNetwork.EOF Then
			primaryIP = rsPrimaryNetwork("address") & ""
			If Not IsNull(rsPrimaryNetwork("macaddress")) Then
				primaryMAC = rsPrimaryNetwork("macaddress") & ""
			End If
			If Not IsNull(rsPrimaryNetwork("interfacename")) Then
				primaryInterface = rsPrimaryNetwork("interfacename") & ""
			End If
		End If
		rsPrimaryNetwork.Close
		Set rsPrimaryNetwork = Nothing
	End If

	'=============================================================================
	' Query dualpath relationships from machinerelationships table
	'=============================================================================
	Dim rsDualpath, isDualpath, relatedMachineNumber, relatedMachineId
	isDualpath = False
	relatedMachineNumber = ""
	relatedMachineId = 0

	strSQL = "SELECT mr.related_machineid, m2.machinenumber " & _
			 "FROM machinerelationships mr " & _
			 "INNER JOIN relationshiptypes rt ON mr.relationshiptypeid = rt.relationshiptypeid " & _
			 "INNER JOIN machines m2 ON mr.related_machineid = m2.machineid " & _
			 "WHERE mr.machineid = ? AND rt.relationshiptype = 'Dualpath' AND mr.isactive = 1 " & _
			 "LIMIT 1"
	Set rsDualpath = ExecuteParameterizedQuery(objConn, strSQL, Array(machineid))

	If Not rsDualpath.EOF Then
		isDualpath = True
		relatedMachineId = rsDualpath("related_machineid")
		relatedMachineNumber = rsDualpath("machinenumber") & ""
	End If
	rsDualpath.Close
	Set rsDualpath = Nothing
%>

<body class="bg-theme <%=Server.HTMLEncode(theme)%>">

<!-- start loader -->
   <div id="pageloader-overlay" class="visible incoming"><div class="loader-wrapper-outer"><div class="loader-wrapper-inner" ><div class="loader"></div></div></div></div>
   <!-- end loader -->
<!-- Start wrapper-->
 <div id="wrapper">
  <!--#include file="./includes/leftsidebar.asp"-->
<!--Start topbar header-->
  <!--#include file="./includes/topbarheader.asp"-->
<!--End topbar header-->
<div class="clearfix"></div>

  <div class="content-wrapper">
    <div class="container-fluid">

      <div class="row mt-3">
        <div class="col-lg-4">
           <div class="card profile-card-2">
            <div class="card-img-block">
                <img class="img-fluid" src="./images/machines/<%If Not IsNull(rs("image")) Then Response.Write(Server.HTMLEncode(rs("image") & "")) Else Response.Write("default.png") End If%>" alt="Card image cap">
            </div>
            <div class="card-body pt-5">
                <img src="./images/machines/<%If Not IsNull(rs("image")) Then Response.Write(Server.HTMLEncode(rs("image") & "")) Else Response.Write("default.png") End If%>" alt="profile-image" class="profile">
                <h5 class="card-title"><%=Server.HTMLEncode(rs("machinenumber") & "")%></h5>
          	<h5 class="card-title"><%=Server.HTMLEncode(rs("vendor") & "")%></h5>
                <h5 class="card-text"><%=Server.HTMLEncode(rs("machinetype") & "")%></h5>
                <p class="card-text"><%=Server.HTMLEncode(rs("machinedescription") & "")%></p>
            </div>

        </div>
        </div>

        <div class="col-lg-8">
           <div class="card">
            <div class="card-body">
            <ul class="nav nav-tabs nav-tabs-primary top-icon nav-justified">
                <li class="nav-item">
                    <a href="javascript:void();" data-target="#profile" data-toggle="pill" class="nav-link active"><i class="icon-wrench"></i> <span class="hidden-xs">Settings</span></a>
                </li>
                <li class="nav-item">
                    <a href="javascript:void();" data-target="#network" data-toggle="pill" class="nav-link"><i class="zmdi zmdi-network"></i> <span class="hidden-xs">Network</span></a>
                </li>
                <li class="nav-item">
                    <a href="javascript:void();" data-target="#relationships" data-toggle="pill" class="nav-link"><i class="zmdi zmdi-link"></i> <span class="hidden-xs">Relationships</span></a>
                </li>
                <li class="nav-item">
                    <a href="javascript:void();" data-target="#compliance" data-toggle="pill" class="nav-link"><i class="zmdi zmdi-lock"></i> <span class="hidden-xs">Compliance</span></a>
                </li>
                <li class="nav-item">
                    <a href="javascript:void();" data-target="#applications" data-toggle="pill" class="nav-link"><i class="zmdi zmdi-apps"></i> <span class="hidden-xs">Applications</span></a>
                </li>
            </ul>
            <div class="tab-content p-3">
                <div class="tab-pane active" id="profile">
                    <h5 class="mb-3">Configuration</h5>
                    <div class="row">
                        <div class="col-md-3">
                            <p class="mb-2"><strong>Machine #:</strong></p>
                            <p class="mb-2"><strong>Alias:</strong></p>
                            <p class="mb-2"><strong>Hostname:</strong></p>
                            <p class="mb-2"><strong>Location:</strong></p>
                            <p class="mb-2"><strong>Vendor:</strong></p>
                            <p class="mb-2"><strong>Model:</strong></p>
                            <p class="mb-2"><strong>Type:</strong></p>
                            <p class="mb-2"><strong>BU:</strong></p>
                            <p class="mb-2"><strong>Controller:</strong></p>
                            <p class="mb-2"><strong>Serial #:</strong></p>
                            <p class="mb-2"><strong>IP Address:</strong></p>
                            <p class="mb-2"><strong>VLAN:</strong></p>
                            <p class="mb-2"><strong>Criticality:</strong></p>
                            <p class="mb-2"><strong>Printer:</strong></p>
<% If isDualpath Then %>
                            <p class="mb-2"><strong>Dualpath:</strong></p>
<% End If %>
                            <p>

                            </p>
                        </div>
                        <div class="col-md-5">
<%
Dim machineNumVal, aliasVal, hostnameVal, vendorValM, modelValM, machineTypeVal, buVal

' Get values and default to N/A if empty
machineNumVal = rs("machinenumber") & ""
If machineNumVal = "" Then machineNumVal = "N/A"

aliasVal = rs("alias") & ""
If aliasVal = "" Then aliasVal = "N/A"

hostnameVal = primaryHostname
If hostnameVal = "" Then hostnameVal = "N/A"

vendorValM = rs("vendor") & ""
If vendorValM = "" Then vendorValM = "N/A"

modelValM = rs("modelnumber") & ""
If modelValM = "" Then modelValM = "N/A"

machineTypeVal = rs("machinetype") & ""
If machineTypeVal = "" Then machineTypeVal = "N/A"

buVal = rs("businessunit") & ""
If buVal = "" Then buVal = "N/A"
%>
                            <p class="mb-2">
<%
If machineNumVal <> "N/A" Then
%>
                                <span class="location-link" data-machineid="<%=Server.HTMLEncode(machineid)%>" style="cursor:pointer; color:#007bff;">
                                    <i class="zmdi zmdi-pin" style="margin-right:4px;"></i><%=Server.HTMLEncode(machineNumVal)%>
                                </span>
<%
Else
    Response.Write("N/A")
End If
%>
                            </p>
                            <p class="mb-2"><%=Server.HTMLEncode(aliasVal)%></p>
                            <p class="mb-2">
<%
If hostnameVal <> "N/A" And primaryIP <> "" Then
%>
                                <a href='com.realvnc.vncviewer.connect://<%=Server.HTMLEncode(primaryIP)%>:5900' title='VNC To Desktop'><%=Server.HTMLEncode(hostnameVal)%></a>
<%
Else
    Response.Write(Server.HTMLEncode(hostnameVal))
End If
%>
                            </p>
                            <p class="mb-2">
<%
' Map location display
Dim mapLeft, mapTop
mapLeft = rs("mapleft") & ""
mapTop = rs("maptop") & ""
If mapLeft <> "" And mapTop <> "" Then
    Response.Write("<i class='zmdi zmdi-pin'></i> Shop Floor (" & Server.HTMLEncode(mapLeft) & ", " & Server.HTMLEncode(mapTop) & ")")
Else
    Response.Write("N/A")
End If
%>
                            </p>
                            <p class="mb-2"><%=Server.HTMLEncode(vendorValM)%></p>
                            <p class="mb-2"><%=Server.HTMLEncode(modelValM)%></p>
                            <p class="mb-2"><%=Server.HTMLEncode(machineTypeVal)%></p>
                            <p class="mb-2"><%=Server.HTMLEncode(buVal)%></p>
<%
' Controller information
' Controller info commented out - not in simplified query
' If Not IsNull(rs("controller_vendor")) And rs("controller_vendor") & "" <> "" Then
'     Dim controllerDisplay
'     controllerDisplay = rs("controller_vendor") & ""
'     If Not IsNull(rs("controller_model")) And rs("controller_model") & "" <> "" Then
'         controllerDisplay = controllerDisplay & " " & rs("controller_model") & ""
'     End If
'     Response.Write("<p class='mb-2'><i class='zmdi zmdi-memory'></i> " & Server.HTMLEncode(controllerDisplay) & "</p>")
' Else
    Response.Write("<p class='mb-2'>N/A</p>")
' End If

' Serial number
If Not IsNull(rs("serialnumber")) And rs("serialnumber") & "" <> "" Then
    Response.Write("<p class='mb-2'><code>" & Server.HTMLEncode(rs("serialnumber") & "") & "</code></p>")
Else
    Response.Write("<p class='mb-2'>N/A</p>")
End If

' IP Address
If primaryIP <> "" Then
    Response.Write("<p class='mb-2'><code>" & Server.HTMLEncode(primaryIP) & "</code></p>")
Else
    Response.Write("<p class='mb-2'>N/A</p>")
End If

' VLAN
If Not IsNull(rs("vlan")) And rs("vlan") & "" <> "" Then
    Response.Write("<p class='mb-2'>VLAN " & Server.HTMLEncode(rs("vlan") & "") & "</p>")
Else
    Response.Write("<p class='mb-2'>N/A</p>")
End If

' Asset Criticality
If Not IsNull(rs("asset_criticality")) And rs("asset_criticality") & "" <> "" Then
    Dim criticalityBadge, criticalityVal
    criticalityVal = rs("asset_criticality") & ""
    Select Case UCase(criticalityVal)
        Case "HIGH"
            criticalityBadge = "<i class='zmdi zmdi-alert-triangle'></i> High"
        Case "MEDIUM"
            criticalityBadge = "Medium"
        Case "LOW"
            criticalityBadge = "Low"
        Case Else
            criticalityBadge = Server.HTMLEncode(criticalityVal)
    End Select
    Response.Write("<p class='mb-2'>" & criticalityBadge & "</p>")
Else
    Response.Write("<p class='mb-2'>N/A</p>")
End If

' Printer data - check if exists (LEFT JOIN may return NULL)
If Not IsNull(rs("printerid")) And rs("printerid") <> "" Then
    Dim printerNameVal
    printerNameVal = rs("printerwindowsname") & ""
    If printerNameVal = "" Then printerNameVal = "Printer #" & rs("printerid")

    Response.Write("<p class='mb-2'><a href='./displayprinter.asp?printerid=" & Server.HTMLEncode(rs("printerid") & "") & "'>" & Server.HTMLEncode(printerNameVal) & "</a></p>")
Else
    Response.Write("<p class='mb-2'>N/A</p>")
End If

' Dualpath information
If isDualpath Then
    Response.Write("<p class='mb-2'><a href='./displaymachine.asp?machineid=" & Server.HTMLEncode(relatedMachineId) & "'><i class='zmdi zmdi-swap'></i> " & Server.HTMLEncode(relatedMachineNumber) & "</a></p>")
End If
%>
                        </div>
                        <div class="col-md-4">
                            <h6 class="text-uppercase mb-2">Notes</h6>
                            <div class="alert alert-secondary" role="alert">
<%
Dim machineNotes
machineNotes = rs("machinenotes") & ""
If machineNotes <> "" Then
    Response.Write("<small>" & Server.HTMLEncode(machineNotes) & "</small>")
Else
    Response.Write("<small class='text-muted'>No notes</small>")
End If
%>
                            </div>
                        </div>
                    </div>
                    <!--/row-->
                </div>

                <div class="tab-pane" id="network">
                    <h5 class="mb-3">Network Interfaces</h5>
                    <div class="table-responsive">
                        <table class="table table-hover table-striped">
                            <thead>
                                <tr>
                                    <th>Interface</th>
                                    <th>IP Address</th>
                                    <th>MAC Address</th>
                                    <th>Type</th>
                                    <th>Primary</th>
                                </tr>
                            </thead>
                            <tbody>
<%
	'=============================================================================
	' Query all network interfaces from communications table
	'=============================================================================
	strSQL2 = "SELECT c.address, c.macaddress, c.interfacename, c.isprimary, c.isdhcp " & _
			  "FROM communications c " & _
			  "INNER JOIN comstypes ct ON c.comstypeid = ct.comstypeid " & _
			  "WHERE c.machineid = ? AND ct.typename = 'Network_Interface' AND c.isactive = 1 " & _
			  "ORDER BY c.isprimary DESC, c.comid ASC"
	Set rs2 = ExecuteParameterizedQuery(objConn, strSQL2, Array(machineid))

	If rs2.EOF Then
		Response.Write("<tr><td colspan='5' class='text-center text-muted'><em>No network interfaces found</em></td></tr>")
	Else
		Do While Not rs2.EOF
			Dim interfaceNameVal, ipAddressVal, macAddressVal, isPrimaryVal, isDHCPVal

			interfaceNameVal = rs2("interfacename") & ""
			If interfaceNameVal = "" Then interfaceNameVal = "Unknown"

			ipAddressVal = rs2("address") & ""
			If ipAddressVal = "" Then ipAddressVal = "N/A"

			macAddressVal = rs2("macaddress") & ""
			If macAddressVal = "" Then macAddressVal = "N/A"

			isPrimaryVal = rs2("isprimary")
			isDHCPVal = rs2("isdhcp")

			Response.Write("<tr>")
			Response.Write("<td>" & Server.HTMLEncode(interfaceNameVal) & "</td>")
			Response.Write("<td><code>" & Server.HTMLEncode(ipAddressVal) & "</code></td>")
			Response.Write("<td><code>" & Server.HTMLEncode(macAddressVal) & "</code></td>")

			If isDHCPVal = 1 Or isDHCPVal = True Then
				Response.Write("<td><span class='badge badge-info'>DHCP</span></td>")
			Else
				Response.Write("<td><span class='badge badge-secondary'>Static</span></td>")
			End If

			If isPrimaryVal = 1 Or isPrimaryVal = True Then
				Response.Write("<td><i class='zmdi zmdi-check-circle text-success'></i></td>")
			Else
				Response.Write("<td></td>")
			End If

			Response.Write("</tr>")
			rs2.MoveNext
		Loop
	End If
	rs2.Close
	Set rs2 = Nothing
%>
                            </tbody>
                        </table>
                    </div>

                    <h5 class="mb-3 mt-4">Other Communications</h5>
                    <div class="table-responsive">
                        <table class="table table-hover table-striped">
                            <thead>
                                <tr>
                                    <th>Type</th>
                                    <th>Address/Port</th>
                                    <th>Details</th>
                                    <th>Description</th>
                                </tr>
                            </thead>
                            <tbody>
<%
	'=============================================================================
	' Query other communication types (Serial, IP, USB, etc.)
	'=============================================================================
	strSQL2 = "SELECT c.address, c.port, c.portname, c.description, c.baud, c.databits, c.stopbits, c.parity, ct.typename " & _
			  "FROM communications c " & _
			  "INNER JOIN comstypes ct ON c.comstypeid = ct.comstypeid " & _
			  "WHERE c.machineid = ? AND ct.typename != 'Network_Interface' AND c.isactive = 1 " & _
			  "ORDER BY ct.typename, c.comid ASC"
	Set rs2 = ExecuteParameterizedQuery(objConn, strSQL2, Array(machineid))

	If rs2.EOF Then
		Response.Write("<tr><td colspan='4' class='text-center text-muted'><em>No other communications found</em></td></tr>")
	Else
		Do While Not rs2.EOF
			Dim typeNameVal, addressVal, detailsVal, descriptionVal

			typeNameVal = rs2("typename") & ""
			addressVal = rs2("address") & ""

			' Build details based on type
			detailsVal = ""
			If typeNameVal = "Serial" Then
				If Not IsNull(rs2("baud")) Then detailsVal = rs2("baud") & " baud"
				If Not IsNull(rs2("databits")) And rs2("databits") & "" <> "" Then
					If detailsVal <> "" Then detailsVal = detailsVal & ", "
					detailsVal = detailsVal & rs2("databits") & "N" & rs2("stopbits") & ""
				End If
			ElseIf typeNameVal = "IP" Then
				If Not IsNull(rs2("port")) Then detailsVal = "Port " & rs2("port")
			End If

			descriptionVal = rs2("description") & ""
			If descriptionVal = "" Then descriptionVal = "-"

			Response.Write("<tr>")
			Response.Write("<td><span class='badge badge-primary'>" & Server.HTMLEncode(typeNameVal) & "</span></td>")
			Response.Write("<td><code>" & Server.HTMLEncode(addressVal) & "</code></td>")
			Response.Write("<td>" & Server.HTMLEncode(detailsVal) & "</td>")
			Response.Write("<td>" & Server.HTMLEncode(descriptionVal) & "</td>")
			Response.Write("</tr>")

			rs2.MoveNext
		Loop
	End If
	rs2.Close
	Set rs2 = Nothing
%>
                            </tbody>
                        </table>
                    </div>
                </div>

                <div class="tab-pane" id="relationships">
                    <h5 class="mb-3">Machine Relationships</h5>
                    <div class="table-responsive">
                        <table class="table table-hover table-striped">
                            <thead>
                                <tr>
                                    <th>Relationship Type</th>
                                    <th>Related Machine</th>
                                    <th>Notes</th>
                                </tr>
                            </thead>
                            <tbody>
<%
	'=============================================================================
	' Query machine relationships from machinerelationships table
	'=============================================================================
	strSQL2 = "SELECT mr.related_machineid, mr.relationship_notes, rt.relationshiptype, m2.machinenumber " & _
			  "FROM machinerelationships mr " & _
			  "INNER JOIN relationshiptypes rt ON mr.relationshiptypeid = rt.relationshiptypeid " & _
			  "INNER JOIN machines m2 ON mr.related_machineid = m2.machineid " & _
			  "WHERE mr.machineid = ? AND mr.isactive = 1 " & _
			  "ORDER BY rt.relationshiptype, m2.machinenumber"
	Set rs2 = ExecuteParameterizedQuery(objConn, strSQL2, Array(machineid))

	If rs2.EOF Then
		Response.Write("<tr><td colspan='3' class='text-center text-muted'><em>No relationships found</em></td></tr>")
	Else
		Do While Not rs2.EOF
			Dim relationshipTypeVal, relatedMachineVal, notesVal

			relationshipTypeVal = rs2("relationshiptype") & ""
			relatedMachineVal = rs2("machinenumber") & ""
			notesVal = rs2("relationship_notes") & ""
			If notesVal = "" Then notesVal = "-"

			Response.Write("<tr>")
			Response.Write("<td><span class='badge badge-info'>" & Server.HTMLEncode(relationshipTypeVal) & "</span></td>")
			Response.Write("<td><a href='./displaymachine.asp?machineid=" & Server.HTMLEncode(rs2("related_machineid")) & "'>" & Server.HTMLEncode(relatedMachineVal) & "</a></td>")
			Response.Write("<td>" & Server.HTMLEncode(notesVal) & "</td>")
			Response.Write("</tr>")

			rs2.MoveNext
		Loop
	End If
	rs2.Close
	Set rs2 = Nothing
%>
                            </tbody>
                        </table>
                    </div>
                </div>

                <div class="tab-pane" id="compliance">
                    <h5 class="mb-3">Compliance & Security</h5>
                    <div class="row">
                        <div class="col-md-6">
                            <h6 class="text-uppercase mb-3"><i class="zmdi zmdi-shield-check"></i> Management & Access</h6>
                            <p class="mb-2"><strong>Third Party Managed:</strong></p>
                            <p class="mb-3">
<%
If Not IsNull(rs("is_third_party_managed")) And rs("is_third_party_managed") & "" <> "" Then
    Dim thirdPartyVal
    thirdPartyVal = rs("is_third_party_managed") & ""
    If UCase(thirdPartyVal) = "YES" Or thirdPartyVal = "Yes" Then
        Response.Write("Yes")
    ElseIf UCase(thirdPartyVal) = "NO" Or thirdPartyVal = "No" Then
        Response.Write("No")
    Else
        Response.Write(Server.HTMLEncode(thirdPartyVal))
    End If
Else
    Response.Write("<span class='text-muted'>Not Specified</span>")
End If
%>
                            </p>

                            <p class="mb-2"><strong>Managed By:</strong></p>
                            <p class="mb-3">
<%
If Not IsNull(rs("third_party_manager")) And rs("third_party_manager") & "" <> "" Then
    Response.Write("<i class='zmdi zmdi-accounts'></i> " & Server.HTMLEncode(rs("third_party_manager") & "") & "")
Else
    Response.Write("<span class='text-muted'>Not Specified</span>")
End If
%>
                            </p>

                            <p class="mb-2"><strong>Last Scan:</strong></p>
                            <p class="mb-3">
<%
If Not IsNull(rs("scan_date")) And rs("scan_date") & "" <> "" Then
    Response.Write(Server.HTMLEncode(rs("scan_date") & ""))
Else
    Response.Write("<span class='text-muted'>Never Scanned</span>")
End If
%>
                            </p>

                            <p class="mb-2"><strong>Scan Result:</strong></p>
                            <p class="mb-3">
<%
If Not IsNull(rs("scan")) And rs("scan") & "" <> "" Then
    Response.Write(Server.HTMLEncode(rs("scan") & ""))
Else
    Response.Write("<span class='text-muted'>N/A</span>")
End If
%>
                            </p>
                        </div>

                        <div class="col-md-6">
                            <h6 class="text-uppercase mb-3"><i class="zmdi zmdi-factory"></i> OT Asset Information</h6>

                            <p class="mb-2"><strong>OT Asset System:</strong></p>
                            <p class="mb-3">
<%
If Not IsNull(rs("ot_asset_system")) And rs("ot_asset_system") & "" <> "" Then
    Response.Write(Server.HTMLEncode(rs("ot_asset_system") & ""))
Else
    Response.Write("<span class='text-muted'>Not Specified</span>")
End If
%>
                            </p>

                            <p class="mb-2"><strong>OT Device Type:</strong></p>
                            <p class="mb-3">
<%
If Not IsNull(rs("ot_asset_device_type")) And rs("ot_asset_device_type") & "" <> "" Then
    Response.Write(Server.HTMLEncode(rs("ot_asset_device_type") & ""))
Else
    Response.Write("<span class='text-muted'>Not Specified</span>")
End If
%>
                            </p>

                            <p class="mb-2"><strong>MFT:</strong></p>
                            <p class="mb-3">
<%
If Not IsNull(rs("mft")) And rs("mft") & "" <> "" Then
    Response.Write(Server.HTMLEncode(rs("mft") & ""))
Else
    Response.Write("<span class='text-muted'>Not Specified</span>")
End If
%>
                            </p>

                            <p class="mb-2"><strong>Deployment Notes:</strong></p>
                            <p class="mb-3">
<%
' TEXT fields in MySQL require special handling in classic ASP
Dim deploymentNotesValue
On Error Resume Next
deploymentNotesValue = ""
If Not IsNull(rs("deployment_notes")) Then
    deploymentNotesValue = rs("deployment_notes").Value
End If
On Error Goto 0

If deploymentNotesValue <> "" And Not IsNull(deploymentNotesValue) Then
    Response.Write("<small>" & Server.HTMLEncode(deploymentNotesValue) & "</small>")
Else
    Response.Write("<span class='text-muted'>No deployment notes</span>")
End If
%>
                            </p>
                        </div>
                    </div>
                </div>

                <div class="tab-pane" id="applications">
                  <div class="table-responsive">
                    <table class="table table-hover table-striped">
                        <thead>
                            <tr>
                                <th>Application Name</th>
                                <th>Version</th>
                            </tr>
                        </thead>
                        <tbody>
<%
	'=============================================================================
	' SECURITY: Use parameterized query for installed applications
	'=============================================================================
	strSQL2 = "SELECT app.appname, ia.version " & _
			  "FROM installedapps ia " & _
			  "INNER JOIN applications app ON ia.appid = app.appid " & _
			  "WHERE ia.machineid = ? AND ia.isactive = 1 " & _
			  "ORDER BY app.appname ASC"
	Set rs2 = ExecuteParameterizedQuery(objConn, strSQL2, Array(machineid))

	If rs2.EOF Then
		Response.Write("<tr><td colspan='2' class='text-center text-muted'><em>No applications installed</em></td></tr>")
	Else
		Do While Not rs2.EOF
			Dim appNameVal, versionVal
			appNameVal = rs2("appname") & ""
			versionVal = rs2("version") & ""
			If versionVal = "" Then versionVal = "N/A"

			Response.Write("<tr>")
			Response.Write("<td>" & Server.HTMLEncode(appNameVal) & "</td>")
			Response.Write("<td>" & Server.HTMLEncode(versionVal) & "</td>")
			Response.Write("</tr>")
			rs2.MoveNext
		Loop
	End If
	rs2.Close
	Set rs2 = Nothing
%>
                        </tbody>
                    </table>
                  </div>
                </div>
            </div>
            </div>
        </div>
        </div>

      </div><!--End Row-->

	<!--start overlay-->
		  <div class="overlay toggle-menu"></div>
		<!--end overlay-->

    </div>
    <!-- End container-fluid-->

    </div><!--End content-wrapper-->
   <!--Start Back To Top Button-->
    <a href="javaScript:void();" class="back-to-top"><i class="fa fa-angle-double-up"></i> </a>
    <!--End Back To Top Button-->

	<!--Start footer-->
	<!--#include file="./includes/footer.asp"-->
	<!--End footer-->

	<!--start color switcher-->
   <!--#include file="./includes/colorswitcher.asp"-->
  <!--end color switcher-->

  </div><!--End wrapper-->

<%
' Clean up
rs.Close
Set rs = Nothing
objConn.Close
Set objConn = Nothing
%>

<!-- Bootstrap core JavaScript-->
  <script src="./assets/js/jquery.min.js"></script>
  <script src="./assets/js/popper.min.js"></script>
  <script src="./assets/js/bootstrap.min.js"></script>

  <!-- simplebar js -->
  <script src="./assets/plugins/simplebar/js/simplebar.js"></script>
  <!-- sidebar-menu js -->
  <script src="./assets/js/sidebar-menu.js"></script>

  <!-- Custom scripts -->
  <script src="./assets/js/app-script.js"></script>

  <script>
  // Location link click handler
  $(document).ready(function() {
      $('.location-link').click(function() {
          var machineid = $(this).data('machineid');
          window.location.href = './shopfloor.asp?highlight=' + machineid;
      });
  });
  </script>

</body>
</html>
