<%@ Language=VBScript %>
<%
Option Explicit
Dim theme, strSQL, rs, objConn
%>
<!DOCTYPE html>
<html lang="en">
<head>
  <!--#include file="./includes/header.asp"-->
  <!--#include file="./includes/sql.asp"-->
</head>

<%
	theme = Request.Cookies("theme")
	IF theme = "" THEN
		theme="bg-theme1"
	END IF

	' Get filter parameter
	Dim filterBU
	filterBU = Request.QueryString("bu")
%>
<body class="bg-theme <%Response.Write(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="row">
        <div class="col-xl-auto">
          <div class="card">
            <div class="card-body">
              <div style="margin-bottom:15px;">
                <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:15px;">
                  <h5 class="card-title" style="margin:0;"><a href="./machinemap.asp" target="_blank"><i class='zmdi zmdi-map' title='Show Machine Map'></i></a>&nbsp;&nbsp;&nbsp;&nbsp;Machines</h5>
                  <div>
                    <a href="./addmachine.asp" class="btn btn-primary">
                      <i class="zmdi zmdi-plus-circle"></i> Add Machine
                    </a>
                  </div>
                </div>
                <div style="display:flex; gap:10px; flex-wrap:wrap; align-items:center;">
                  <select id="buFilter" class="btn btn-secondary btn-sm" onchange="updateFilter('bu', this.value)">
                    <option value="">All Business Units</option>
<%
	' Get list of business units for filter dropdown
	Dim strBUSQL, rsBU
	strBUSQL = "SELECT DISTINCT businessunitid, businessunit FROM businessunits ORDER BY businessunit"
	Set rsBU = objConn.Execute(strBUSQL)
	While Not rsBU.EOF
		Response.Write("<option value=""" & rsBU("businessunitid") & """")
		If filterBU = CStr(rsBU("businessunitid")) Then Response.Write(" selected")
		Response.Write(">" & Server.HTMLEncode(rsBU("businessunit")) & "</option>")
		rsBU.MoveNext
	Wend
	rsBU.Close
	Set rsBU = Nothing
%>
                  </select>
                  <% If filterBU <> "" And filterBU <> "all" Then %>
                  <a href="displaymachines.asp" class="btn btn-outline-secondary btn-sm">
                    <i class="zmdi zmdi-close"></i> Clear
                  </a>
                  <% End If %>
                </div>
              </div>
			  <div class="table-responsive">
              <table class="table table-hover">
                <thead>
                  <tr>
                    <th scope="col"><i class="zmdi zmdi-pin"></i></th>
                    <th scope="col">Machine</th>
                    <th scope="col">Function</th>
                    <th scope="col">Make</th>
                    <th scope="col">Model</th>
                    <th scope="col">BU</th>
                  </tr>
                </thead>
                <tbody>

<%
		' Build WHERE clause with optional BU filter
		' NOTE: Exclude LocationOnly (1), network devices (16-20), and PC types (33+)
		Dim whereClause
		whereClause = "models.machinetypeid = machinetypes.machinetypeid AND " &_
		              "machines.modelnumberid = models.modelnumberid AND " &_
		              "models.vendorid = vendors.vendorid AND " &_
		              "machines.businessunitid = businessunits.businessunitID AND " &_
		              "machines.isactive = 1 AND islocationonly=0 AND " &_
		              "models.machinetypeid NOT IN (1, 16, 17, 18, 19, 20) AND models.machinetypeid < 33"

		' Add BU filter if specified
		If filterBU <> "" And IsNumeric(filterBU) Then
			whereClause = whereClause & " AND machines.businessunitid = " & CLng(filterBU)
		End If

		strSQL = "SELECT * FROM machines,machinetypes,models,vendors,businessunits WHERE " &_
		         whereClause & " ORDER BY machinenumber ASC"

	set rs = objconn.Execute(strSQL)

	while not rs.eof
			Response.write("<tr>")
%>
                    <td>
                        <span class="location-link" data-machineid="<%=rs("machineid")%>" style="cursor:pointer;">
                            <i class="zmdi zmdi-pin"></i>
                        </span>
                    </td>
                    <td><a href="./displaymachine.asp?machineid=<%=rs("machineid")%>" title="View Machine Details"><%
					Dim displayName
					displayName = rs("machinenumber") & ""
					If displayName = "" Then displayName = rs("hostname") & ""
					If displayName = "" Then displayName = "ID:" & rs("machineid")
					Response.Write(Server.HTMLEncode(displayName))
					%></a></td>
                    <td><%=Server.HTMLEncode(rs("machinetype") & "")%></td>
                    <td><%=Server.HTMLEncode(rs("vendor") & "")%></td>
                    <td><%=Server.HTMLEncode(rs("modelnumber") & "")%></td>
                    <td><%=Server.HTMLEncode(rs("businessunit") & "")%></td>
 
                  </tr>

<%
	rs.movenext
	wend
	objConn.Close 
%>
                </tbody>
              </table>
            </div>
            </div>
          </div>
        </div>
      </div><!--End Row-->

    <!-- 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-->
	<footer class="footer">
      </div>
    </footer>
	<!--End footer-->
	
   
  </div><!--End wrapper-->


  <!-- 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>

  <!-- Filter update script -->
  <script>
  function updateFilter(param, value) {
      var url = new URL(window.location.href);
      var params = new URLSearchParams(url.search);

      if (value === '') {
          params.delete(param);
      } else {
          params.set(param, value);
      }

      window.location.href = 'displaymachines.asp' + (params.toString() ? '?' + params.toString() : '');
  }
  </script>

  <!-- Location map popup modal -->
  <style>
  /* Theme-specific styling for better visibility on all themes */
  body.bg-theme1 .location-link,
  body.bg-theme2 .location-link,
  body.bg-theme3 .location-link,
  body.bg-theme4 .location-link,
  body.bg-theme5 .location-link,
  body.bg-theme6 .location-link,
  body.bg-theme7 .location-link,
  body.bg-theme8 .location-link,
  body.bg-theme9 .location-link,
  body.bg-theme10 .location-link,
  body.bg-theme11 .location-link,
  body.bg-theme12 .location-link,
  body.bg-theme13 .location-link,
  body.bg-theme14 .location-link,
  body.bg-theme15 .location-link,
  body.bg-theme16 .location-link {
      color: #fff !important;
  }

  /* Theme-specific popup header colors */
  body.bg-theme1 .location-popup-header { background: linear-gradient(45deg, #3a3a3a, #4a4a4a); }
  body.bg-theme2 .location-popup-header { background: linear-gradient(45deg, #3a3a3a, #4a4a4a); }
  body.bg-theme3 .location-popup-header { background: linear-gradient(45deg, #3a3a3a, #4a4a4a); }
  body.bg-theme4 .location-popup-header { background: linear-gradient(45deg, #3a3a3a, #4a4a4a); }
  body.bg-theme5 .location-popup-header { background: linear-gradient(45deg, #3a3a3a, #4a4a4a); }
  body.bg-theme6 .location-popup-header { background: linear-gradient(45deg, #3a3a3a, #4a4a4a); }
  body.bg-theme7 .location-popup-header { background: linear-gradient(45deg, #0c675e, #069e90); }
  body.bg-theme8 .location-popup-header { background: linear-gradient(45deg, #a52a04, #4f5f58); }
  body.bg-theme9 .location-popup-header { background: linear-gradient(45deg, #29323c, #485563); }
  body.bg-theme10 .location-popup-header { background: linear-gradient(45deg, #795548, #945c48); }
  body.bg-theme11 .location-popup-header { background: linear-gradient(45deg, #1565C0, #1E88E5); }
  body.bg-theme12 .location-popup-header { background: linear-gradient(45deg, #65379b, #886aea); }
  body.bg-theme13 .location-popup-header { background: linear-gradient(45deg, #ff5447, #f1076f); }
  body.bg-theme14 .location-popup-header { background: linear-gradient(45deg, #08a50e, #69bb03); }
  body.bg-theme15 .location-popup-header { background: linear-gradient(45deg, #6a11cb, #2575fc); }
  body.bg-theme16 .location-popup-header { background: linear-gradient(45deg, #6a11cb, #cccccc); }

  .location-popup-overlay {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: rgba(0, 0, 0, 0.5);
      z-index: 9998;
      display: none;
  }
  .location-popup {
      position: fixed;
      background: #1f1f1f;
      border: 2px solid #667eea;
      border-radius: 8px;
      box-shadow: 0 10px 40px rgba(0, 0, 0, 0.8);
      z-index: 9999;
      display: none;
      max-width: 90vw;
      max-height: 90vh;
  }
  .location-popup-header {
      background: linear-gradient(45deg, #667eea 0%, #764ba2 100%);
      color: white;
      padding: 12px 15px;
      border-radius: 6px 6px 0 0;
      display: flex;
      justify-content: space-between;
      align-items: center;
  }
  .location-popup-close {
      background: none;
      border: none;
      color: white;
      font-size: 24px;
      cursor: pointer;
      padding: 0;
      width: 30px;
      height: 30px;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 4px;
  }
  .location-popup-close:hover {
      background: rgba(255, 255, 255, 0.2);
  }
  .location-popup-body {
      padding: 0;
      background: #2a2a2a;
  }
  .location-popup iframe {
      display: block;
      border: none;
      border-radius: 0 0 6px 6px;
  }
  .location-link:hover {
      text-decoration: underline;
  }
  </style>

  <script>
  $(document).ready(function() {
      var $overlay = $('<div class="location-popup-overlay"></div>').appendTo('body');
      var $popup = $('<div class="location-popup"></div>').appendTo('body');

      $popup.html(
          '<div class="location-popup-header">' +
              '<h6 style="margin:0; font-size:16px;"><i class="zmdi zmdi-pin"></i> <span class="location-title">Loading...</span></h6>' +
              '<button class="location-popup-close" title="Close (Esc)">&times;</button>' +
          '</div>' +
          '<div class="location-popup-body">' +
              '<iframe src="" width="440" height="340"></iframe>' +
          '</div>'
      );

      var $iframe = $popup.find('iframe');
      var $title = $popup.find('.location-title');
      var currentMachineId = null;

      function showLocationPopup(machineId, locationName, mouseEvent) {
          if (currentMachineId === machineId && $popup.is(':visible')) {
              return;
          }

          currentMachineId = machineId;
          $title.text('Machine ' + locationName);
          $iframe.attr('src', './displaylocation.asp?machineid=' + machineId);

          var popupWidth = 440;
          var popupHeight = 400;
          var mouseX = mouseEvent.clientX;
          var mouseY = mouseEvent.clientY;
          var windowWidth = window.innerWidth;
          var windowHeight = window.innerHeight;

          var left, top;

          left = mouseX + 10;
          if (left + popupWidth > windowWidth - 10) {
              left = mouseX - popupWidth - 10;
          }
          if (left < 10) {
              left = 10;
          }

          var spaceBelow = windowHeight - mouseY;
          var spaceAbove = mouseY;

          if (spaceBelow >= popupHeight + 20) {
              top = mouseY + 10;
          } else if (spaceAbove >= popupHeight + 20) {
              top = mouseY - popupHeight - 10;
          } else {
              top = Math.max(10, (windowHeight - popupHeight) / 2);
          }

          if (top < 10) {
              top = 10;
          }
          if (top + popupHeight > windowHeight - 10) {
              top = windowHeight - popupHeight - 10;
          }

          $popup.css({
              left: left + 'px',
              top: top + 'px',
              display: 'block'
          });

          $overlay.fadeIn(200);
          $popup.fadeIn(200);
      }

      function hideLocationPopup() {
          $overlay.fadeOut(200);
          $popup.fadeOut(200);
          setTimeout(function() {
              $iframe.attr('src', '');
              currentMachineId = null;
          }, 200);
      }

      var hoverTimer = null;

      $('.location-link').on('mouseenter', function(e) {
          var $link = $(this);
          var machineId = $link.data('machineid');
          var locationName = $link.text().trim();
          var mouseEvent = e;

          if (hoverTimer) {
              clearTimeout(hoverTimer);
          }

          hoverTimer = setTimeout(function() {
              showLocationPopup(machineId, locationName, mouseEvent);
          }, 300);
      });

      $('.location-link').on('mouseleave', function() {
          if (hoverTimer) {
              clearTimeout(hoverTimer);
              hoverTimer = null;
          }
      });

      $popup.on('mouseenter', function() {});
      $popup.on('mouseleave', function() {
          hideLocationPopup();
      });

      $overlay.on('click', hideLocationPopup);
      $popup.find('.location-popup-close').on('click', hideLocationPopup);

      $(document).on('keydown', function(e) {
          if (e.key === 'Escape' && $popup.is(':visible')) {
              hideLocationPopup();
          }
      });
  });
  </script>

</body>
</html>
