<!DOCTYPE html>
<html lang="en">
<head>
  <!--#include file="./includes/header.asp"-->
  <!--#include file="./includes/sql.asp"-->
  <!-- DataTables CSS -->
  <link rel="stylesheet" href="assets/plugins/datatables/dataTables.bootstrap4.min.css">
</head>

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

<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="container-fluid">

      <div class="row mt-3">
        <div class="col-xl-auto">
          <div class="card">
            <div class="card-body">
              <%
                Dim showFilter
                showFilter = Request.QueryString("filter")
                If showFilter = "" Then showFilter = "all"
              %>
              <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:15px;">
                <h5 class="card-title" style="margin:0;">
                  <i class="zmdi zmdi-usb"></i> USB Devices
                  <% If showFilter = "available" Then %>
                    <span class="badge badge-success">Available Only</span>
                  <% ElseIf showFilter = "checkedout" Then %>
                    <span class="badge badge-warning">Checked Out Only</span>
                  <% Else %>
                    <span class="badge badge-info">All Devices</span>
                  <% End If %>
                </h5>
                <div>
                  <% If showFilter <> "all" Then %>
                    <a href="./displayusb.asp" class="btn btn-info btn-sm">
                      <i class="zmdi zmdi-view-list-alt"></i> Show All
                    </a>
                  <% End If %>
                  <% If showFilter <> "available" Then %>
                    <a href="./displayusb.asp?filter=available" class="btn btn-success btn-sm">
                      <i class="zmdi zmdi-check-circle"></i> Available Only
                    </a>
                  <% End If %>
                  <% If showFilter <> "checkedout" Then %>
                    <a href="./displayusb.asp?filter=checkedout" class="btn btn-warning btn-sm">
                      <i class="zmdi zmdi-time"></i> Checked Out
                    </a>
                  <% End If %>
                  <a href="./checkoutusb.asp" class="btn btn-primary btn-sm">
                    <i class="zmdi zmdi-arrow-right"></i> Checkout
                  </a>
                  <a href="./checkinusb.asp" class="btn btn-secondary btn-sm">
                    <i class="zmdi zmdi-arrow-left"></i> Check-in
                  </a>
                  <a href="./addusb.asp" class="btn btn-success btn-sm">
                    <i class="zmdi zmdi-plus-circle"></i> Add USB
                  </a>
                </div>
              </div>

			  <div class="table-responsive">
              <table id="usbTable" class="table table-hover table-striped">
                <thead>
                  <tr>
                    <th scope="col">Serial Number</th>
                    <th scope="col">Name</th>
                    <th scope="col">Business Unit</th>
                    <th scope="col">Status</th>
                    <th scope="col">Current Holder</th>
                    <th scope="col">Checkout Time</th>
                    <th scope="col">Actions</th>
                  </tr>
                </thead>
                <tbody>

<%
	Dim strSQL, rs
	' Query USB devices with current checkout status
	strSQL = "SELECT m.machineid, m.serialnumber, m.alias, bu.businessunit, " & _
	         "uc.checkoutid, uc.sso AS current_holder, uc.checkout_time, uc.checkout_reason, " & _
	         "CASE WHEN uc.checkoutid IS NOT NULL THEN 'Checked Out' ELSE 'Available' END AS status " & _
	         "FROM machines m " & _
	         "LEFT JOIN businessunits bu ON m.businessunitid = bu.businessunitid " & _
	         "LEFT JOIN usbcheckouts uc ON m.machineid = uc.machineid AND uc.checkin_time IS NULL " & _
	         "WHERE m.machinetypeid = 44 AND m.isactive = 1 "

	' Apply filter
	If showFilter = "available" Then
		strSQL = strSQL & "AND uc.checkoutid IS NULL "
	ElseIf showFilter = "checkedout" Then
		strSQL = strSQL & "AND uc.checkoutid IS NOT NULL "
	End If

	strSQL = strSQL & "ORDER BY m.serialnumber ASC"

	Set rs = objConn.Execute(strSQL)

	Dim rowCount
	rowCount = 0

	While Not rs.EOF
		rowCount = rowCount + 1
		Dim machineId, serialNum, usbAlias, businessUnit, statusText, currentHolder, checkoutTime, checkoutReason
		Dim statusClass, checkoutId

		machineId = rs("machineid")
		serialNum = rs("serialnumber") & ""
		usbAlias = rs("alias") & ""
		businessUnit = rs("businessunit") & ""
		statusText = rs("status") & ""
		currentHolder = rs("current_holder") & ""
		checkoutId = rs("checkoutid")

		' Handle checkout time (MM/DD/YYYY h:mm AM/PM)
		If Not IsNull(rs("checkout_time")) Then
			checkoutTime = Month(rs("checkout_time")) & "/" & Day(rs("checkout_time")) & "/" & Year(rs("checkout_time")) & " " & FormatDateTime(rs("checkout_time"), 3)
		Else
			checkoutTime = "-"
		End If

		checkoutReason = rs("checkout_reason") & ""

		' Status styling
		If statusText = "Available" Then
			statusClass = "success"
		Else
			statusClass = "warning"
		End If
%>
                  <tr>
                    <td><code><%=Server.HTMLEncode(serialNum)%></code></td>
                    <td><%=Server.HTMLEncode(usbAlias)%></td>
                    <td><%=Server.HTMLEncode(businessUnit)%></td>
                    <td><span class="badge badge-<%=statusClass%>"><%=Server.HTMLEncode(statusText)%></span></td>
                    <td>
                      <% If currentHolder <> "" Then %>
                        <strong><%=Server.HTMLEncode(currentHolder)%></strong>
                        <% If checkoutReason <> "" Then %>
                          <br><small class="text-muted" title="<%=Server.HTMLEncode(checkoutReason)%>"><%=Server.HTMLEncode(Left(checkoutReason, 30))%><% If Len(checkoutReason) > 30 Then Response.Write("...") End If %></small>
                        <% End If %>
                      <% Else %>
                        <span class="text-muted">-</span>
                      <% End If %>
                    </td>
                    <td><%=Server.HTMLEncode(checkoutTime)%></td>
                    <td>
                      <% If IsNull(checkoutId) Then %>
                        <a href="./checkoutusb.asp?serial=<%=Server.URLEncode(serialNum)%>" class="btn btn-sm btn-primary" title="Checkout this USB">
                          <i class="zmdi zmdi-arrow-right"></i>
                        </a>
                      <% Else %>
                        <a href="./checkinusb.asp?serial=<%=Server.URLEncode(serialNum)%>" class="btn btn-sm btn-success" title="Check-in this USB">
                          <i class="zmdi zmdi-arrow-left"></i>
                        </a>
                      <% End If %>
                      <a href="./usbhistory.asp?machineid=<%=machineId%>" class="btn btn-sm btn-secondary" title="View history">
                        <i class="zmdi zmdi-time"></i>
                      </a>
                    </td>
                  </tr>
<%
		rs.MoveNext
	Wend

	rs.Close
	Set rs = Nothing

	If rowCount = 0 Then
%>
                  <tr>
                    <td colspan="7" class="text-center text-muted">
                      <i class="zmdi zmdi-info zmdi-hc-2x"></i><br>
                      No USB devices found.
                      <% If showFilter <> "all" Then %>
                        <br><a href="./displayusb.asp">Show all devices</a>
                      <% Else %>
                        <br><a href="./addusb.asp">Add a USB device</a>
                      <% End If %>
                    </td>
                  </tr>
<%
	End If
%>

                </tbody>
              </table>
              </div>

              <div class="mt-3 text-muted">
                <small>
                  <i class="zmdi zmdi-info"></i>
                  Total: <%=rowCount%> USB device(s)
                  <% If showFilter <> "all" Then %> (filtered)<% End If %>
                </small>
              </div>

            </div>
          </div>
        </div>
      </div>

    </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-->
  <footer class="footer">
    <div class="container">
      <div class="text-center">
      </div>
    </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>

<!-- DataTables js -->
<script src="assets/plugins/datatables/jquery.dataTables.min.js"></script>
<script src="assets/plugins/datatables/dataTables.bootstrap4.min.js"></script>

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

<script>
$(document).ready(function() {
    $('#usbTable').DataTable({
        "order": [[0, "asc"]],
        "pageLength": 25,
        "language": {
            "emptyTable": "No USB devices found"
        }
    });
});
</script>

</body>
</html>
