<!DOCTYPE html>
<html lang="en">
<head>
  <!--#include file="./includes/header.asp"-->
  <!--#include file="./includes/sql.asp"-->
  <!--#include file="./includes/validation.asp"-->
</head>

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

	Dim idfid, isNewRecord
	idfid = Request.QueryString("id")
	If idfid = "" Or idfid = "0" Then
		isNewRecord = True
		idfid = 0
	Else
		isNewRecord = False
	End If

	' If editing, fetch existing data
	Dim rs, idfname, description, maptop, mapleft, isactive
	If Not isNewRecord Then
		Dim strSQL
		strSQL = "SELECT mac.machineid, mac.alias AS idfname, mac.machinenotes AS description, " & _
		         "mac.maptop, mac.mapleft, mac.isactive " & _
		         "FROM machines mac " & _
		         "WHERE mac.machineid = " & idfid & " AND mac.machinetypeid = 17"
		Set rs = objConn.Execute(strSQL)

		If rs.EOF Then
			Response.Write("IDF not found")
			Response.End
		End If

		If Not IsNull(rs("idfname")) Then idfname = rs("idfname") Else idfname = ""
		If Not IsNull(rs("description")) Then description = rs("description") Else description = ""
		If Not IsNull(rs("maptop")) Then maptop = rs("maptop") Else maptop = ""
		If Not IsNull(rs("mapleft")) Then mapleft = rs("mapleft") Else mapleft = ""
		If Not IsNull(rs("isactive")) Then isactive = rs("isactive") Else isactive = 1

		rs.Close
		Set rs = Nothing
	Else
		' New record defaults
		idfname = ""
		description = ""
		maptop = ""
		mapleft = ""
		isactive = 1  ' Active by default for new records
	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">

      <!-- Breadcrumb -->
      <div class="row mt-3">
        <div class="col-lg-12">
          <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
              <li class="breadcrumb-item"><a href="networkdevices.asp">Network Devices</a></li>
              <li class="breadcrumb-item"><a href="networkdevices.asp?filter=IDF">IDFs</a></li>
              <li class="breadcrumb-item active"><%If isNewRecord Then Response.Write("Add IDF") Else Response.Write("Edit IDF")%></li>
            </ol>
          </nav>
        </div>
      </div>

      <div class="row">
        <div class="col-lg-12">
           <div class="card">
            <div class="card-body">
              <h5 class="card-title">
                <i class="zmdi zmdi-city-alt"></i>
                <%If isNewRecord Then Response.Write("Add IDF") Else Response.Write("Edit IDF: " & Server.HTMLEncode(idfname))%>
              </h5>
              <hr>

              <form method="post" action="savenetworkdevice.asp">
                <input type="hidden" name="type" value="idf">
                <input type="hidden" name="id" value="<%=idfid%>">

                <div class="form-group row">
                  <label class="col-sm-3 col-form-label">IDF Name <span class="text-danger">*</span></label>
                  <div class="col-sm-9">
                    <input type="text" name="idfname" class="form-control"
                           value="<%=Server.HTMLEncode(idfname)%>"
                           required maxlength="100"
                           placeholder="e.g., Main-IDF, Floor-2-IDF">
                    <small class="form-text text-muted">
                      Short name to identify this IDF location
                    </small>
                  </div>
                </div>

                <div class="form-group row">
                  <label class="col-sm-3 col-form-label">Description</label>
                  <div class="col-sm-9">
                    <textarea name="description" class="form-control" rows="3"
                              maxlength="255" placeholder="Detailed notes about this IDF..."><%=Server.HTMLEncode(description)%></textarea>
                    <small class="form-text text-muted">
                      Optional: Location details, access information, or other notes
                    </small>
                  </div>
                </div>

                <div class="form-group row">
                  <label class="col-sm-3 col-form-label"></label>
                  <div class="col-sm-9">
                    <div class="custom-control custom-checkbox">
                      <input type="checkbox" class="custom-control-input" id="isactive" name="isactive" value="1"
                             <%If isactive = True Or isactive = 1 Then Response.Write("checked")%>>
                      <label class="custom-control-label" for="isactive">Active</label>
                    </div>
                    <small class="form-text text-muted">
                      Inactive devices are hidden from most lists and the network map
                    </small>
                  </div>
                </div>

                <!-- Hidden coordinate fields - populated by map selector -->
                <input type="hidden" id="maptop" name="maptop" value="<%=maptop%>">
                <input type="hidden" id="mapleft" name="mapleft" value="<%=mapleft%>">

                <div class="form-group row">
                  <label class="col-sm-3 col-form-label">Map Position (Optional)</label>
                  <div class="col-sm-9">
                    <button type="button" class="btn btn-secondary" id="selectLocationBtn">
                      <i class="zmdi zmdi-pin"></i> Select Location on Map
                    </button>
                    <div id="coordinateDisplay" style="margin-top:10px; color:#aaa; font-size:13px;">
                      <%If maptop <> "" And mapleft <> "" Then
                          Response.Write("Current position: X=" & mapleft & ", Y=" & maptop)
                      Else
                          Response.Write("No position set - click button to select")
                      End If%>
                    </div>
                    <small class="form-text text-muted">
                      Click to select this IDF's position on the network map
                    </small>
                  </div>
                </div>

                <hr>

                <div class="form-group row">
                  <div class="col-sm-9 offset-sm-3">
                    <button type="submit" class="btn btn-success">
                      <i class="zmdi zmdi-save"></i>
                      <%If isNewRecord Then Response.Write("Add IDF") Else Response.Write("Save Changes")%>
                    </button>
                    <a href="networkdevices.asp?filter=IDF" class="btn btn-secondary">
                      <i class="zmdi zmdi-close"></i> Cancel
                    </a>
                    <%If Not isNewRecord Then%>
                    <button type="button" class="btn btn-danger float-right" onclick="confirmDelete()">
                      <i class="zmdi zmdi-delete"></i> Delete
                    </button>
                    <%End If%>
                  </div>
                </div>

              </form>

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

    </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">
    </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>

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

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

<script>
function confirmDelete() {
  if (confirm('Are you sure you want to delete this IDF? This action cannot be undone.')) {
    // Submit delete request
    var form = document.createElement('form');
    form.method = 'POST';
    form.action = 'savenetworkdevice.asp';

    var typeInput = document.createElement('input');
    typeInput.type = 'hidden';
    typeInput.name = 'type';
    typeInput.value = 'idf';
    form.appendChild(typeInput);

    var idInput = document.createElement('input');
    idInput.type = 'hidden';
    idInput.name = 'id';
    idInput.value = '<%=idfid%>';
    form.appendChild(idInput);

    var deleteInput = document.createElement('input');
    deleteInput.type = 'hidden';
    deleteInput.name = 'delete';
    deleteInput.value = '1';
    form.appendChild(deleteInput);

    document.body.appendChild(form);
    form.submit();
  }
}
</script>

<!--#include file="./includes/map_picker.asp"-->

</body>
</html>
<%
	objConn.Close
%>
