<!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 serverid, isNewRecord
	serverid = Request.QueryString("id")
	If serverid = "" Or serverid = "0" Then
		isNewRecord = True
		serverid = 0
	Else
		isNewRecord = False
	End If

	' If editing, fetch existing data
	Dim rs, servername, modelid, serialnumber, ipaddress, description, maptop, mapleft, isactive
	Dim vendorname, modelnumber
	If Not isNewRecord Then
		Dim strSQL
		strSQL = "SELECT mac.machineid, mac.alias AS servername, mac.modelnumberid AS modelid, " & _
		         "mac.serialnumber, mac.machinenotes AS description, mac.maptop, mac.mapleft, mac.isactive, " & _
		         "m.modelnumber, v.vendor, c.address AS ipaddress " & _
		         "FROM machines mac " & _
		         "LEFT JOIN models m ON mac.modelnumberid = m.modelnumberid " & _
		         "LEFT JOIN vendors v ON m.vendorid = v.vendorid " & _
		         "LEFT JOIN communications c ON mac.machineid = c.machineid AND c.isprimary = 1 AND c.comstypeid = 1 " & _
		         "WHERE mac.machineid = " & serverid & " AND mac.machinetypeid = 30"
		Set rs = objConn.Execute(strSQL)

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

		If Not IsNull(rs("servername")) Then servername = rs("servername") Else servername = ""
		If Not IsNull(rs("modelid")) Then modelid = rs("modelid") Else modelid = ""
		If Not IsNull(rs("serialnumber")) Then serialnumber = rs("serialnumber") Else serialnumber = ""
		If Not IsNull(rs("ipaddress")) Then ipaddress = rs("ipaddress") Else ipaddress = ""
		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
		If Not IsNull(rs("vendor")) Then vendorname = rs("vendor") Else vendorname = ""
		If Not IsNull(rs("modelnumber")) Then modelnumber = rs("modelnumber") Else modelnumber = ""

		rs.Close
		Set rs = Nothing
	Else
		' New record defaults
		servername = ""
		modelid = ""
		serialnumber = ""
		ipaddress = ""
		description = ""
		maptop = ""
		mapleft = ""
		isactive = 1  ' Active by default for new records
		vendorname = ""
		modelnumber = ""
	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="network_devices.asp">Network Devices</a></li>
              <li class="breadcrumb-item"><a href="network_devices.asp?filter=Server">Servers</a></li>
              <li class="breadcrumb-item active"><%If isNewRecord Then Response.Write("Add Server") Else Response.Write("Edit Server")%></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-storage"></i>
                <%If isNewRecord Then Response.Write("Add Server") Else Response.Write("Edit Server: " & Server.HTMLEncode(servername))%>
              </h5>
              <hr>

              <form method="post" action="save_network_device.asp">
                <input type="hidden" name="type" value="server">
                <input type="hidden" name="id" value="<%=serverid%>">

                <div class="form-group row">
                  <label class="col-sm-3 col-form-label">Server Name <span class="text-danger">*</span></label>
                  <div class="col-sm-9">
                    <input type="text" name="servername" class="form-control"
                           value="<%=Server.HTMLEncode(servername)%>"
                           required maxlength="100"
                           placeholder="e.g., DB-Server-01, App-Server-Primary">
                    <small class="form-text text-muted">
                      Short name to identify this server
                    </small>
                  </div>
                </div>

                <div class="form-group row">
                  <label class="col-sm-3 col-form-label">Model</label>
                  <div class="col-sm-9">
                    <div class="input-group">
                      <select name="modelid" id="modelid" class="form-control">
                        <option value="">-- Select Model --</option>
                        <%
                        Dim strSQL2, rsModels
                        strSQL2 = "SELECT m.modelnumberid, m.modelnumber, v.vendor " & _
                                  "FROM models m " & _
                                  "INNER JOIN vendors v ON m.vendorid = v.vendorid " & _
                                  "WHERE m.isactive = 1 " & _
                                  "ORDER BY v.vendor, m.modelnumber"
                        Set rsModels = objConn.Execute(strSQL2)
                        Do While Not rsModels.EOF
                            Dim selected
                            selected = ""
                            If Not IsNull(modelid) And modelid <> "" Then
                                If CStr(rsModels("modelnumberid")) = CStr(modelid) Then
                                    selected = "selected"
                                End If
                            End If
                        %>
                            <option value="<%=rsModels("modelnumberid")%>" <%=selected%>>
                                <%=Server.HTMLEncode(rsModels("vendor") & " - " & rsModels("modelnumber"))%>
                            </option>
                        <%
                            rsModels.MoveNext
                        Loop
                        rsModels.Close
                        Set rsModels = Nothing
                        %>
                        <option value="new">+ Add New Model</option>
                      </select>
                      <div class="input-group-append">
                        <button type="button" class="btn btn-info" id="addModelBtn">
                          <i class="zmdi zmdi-plus"></i> New
                        </button>
                      </div>
                    </div>
                    <small class="form-text text-muted">
                      Select a model or click "New" to add one
                    </small>
                  </div>
                </div>

                <!-- Hidden section for adding new model -->
                <div id="newModelSection" class="form-group row" style="display:none;">
                  <div class="col-sm-9 offset-sm-3">
                    <div style="padding:15px; background:rgba(255,255,255,0.03); border:1px solid rgba(255,255,255,0.1); border-radius:5px;">
                      <h6 class="mb-3"><i class="zmdi zmdi-plus-circle"></i> New Model</h6>

                      <div class="form-group">
                        <label for="newmodelnumber">Model Number <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" id="newmodelnumber" name="newmodelnumber"
                               maxlength="255" placeholder="e.g., PowerEdge R740">
                      </div>

                      <div class="form-group">
                        <label for="newvendorid">Vendor <span class="text-danger">*</span></label>
                        <div class="input-group">
                          <select class="form-control" id="newvendorid" name="newvendorid">
                            <option value="">-- Select Vendor --</option>
<%
    Dim rsVendors
    strSQL2 = "SELECT vendorid, vendor FROM vendors WHERE isactive = 1 ORDER BY vendor ASC"
    Set rsVendors = objConn.Execute(strSQL2)
    While Not rsVendors.EOF
        Response.Write("<option value='" & rsVendors("vendorid") & "'>" & Server.HTMLEncode(rsVendors("vendor")) & "</option>")
        rsVendors.MoveNext
    Wend
    rsVendors.Close
    Set rsVendors = Nothing
%>
                            <option value="new">+ Add New Vendor</option>
                          </select>
                          <div class="input-group-append">
                            <button type="button" class="btn btn-info" id="addVendorBtn">
                              <i class="zmdi zmdi-plus"></i> New
                            </button>
                          </div>
                        </div>
                      </div>

                      <!-- Hidden section for adding new vendor -->
                      <div id="newVendorSection" style="display:none; padding:15px; background:rgba(255,255,255,0.05); border:1px solid rgba(255,255,255,0.15); border-radius:5px; margin-bottom:15px;">
                        <h6 class="mb-3"><i class="zmdi zmdi-plus-circle"></i> New Vendor</h6>

                        <div class="form-group">
                          <label for="newvendorname">Vendor Name <span class="text-danger">*</span></label>
                          <input type="text" class="form-control" id="newvendorname" name="newvendorname"
                                 maxlength="50" placeholder="e.g., Dell, HP, Cisco">
                        </div>

                        <button type="button" class="btn btn-sm btn-secondary" id="cancelNewVendor">
                          <i class="zmdi zmdi-close"></i> Cancel
                        </button>
                      </div>

                      <div class="form-group">
                        <label for="newmodelnotes">Model Notes</label>
                        <textarea class="form-control" id="newmodelnotes" name="newmodelnotes"
                                  rows="2" maxlength="255"
                                  placeholder="Additional notes about this model..."></textarea>
                      </div>

                      <div class="form-group">
                        <label for="newmodeldocpath">Documentation Path</label>
                        <input type="text" class="form-control" id="newmodeldocpath" name="newmodeldocpath"
                               maxlength="255" placeholder="\\server\docs\model.pdf or http://...">
                      </div>

                      <button type="button" class="btn btn-sm btn-secondary" id="cancelNewModel">
                        <i class="zmdi zmdi-close"></i> Cancel
                      </button>
                    </div>
                  </div>
                </div>

                <div class="form-group row">
                  <label class="col-sm-3 col-form-label">Serial Number</label>
                  <div class="col-sm-9">
                    <input type="text" name="serialnumber" class="form-control"
                           value="<%=Server.HTMLEncode(serialnumber)%>"
                           maxlength="100" placeholder="e.g., SN123456789">
                  </div>
                </div>

                <div class="form-group row">
                  <label class="col-sm-3 col-form-label">IP Address</label>
                  <div class="col-sm-9">
                    <input type="text" name="ipaddress" class="form-control"
                           value="<%=Server.HTMLEncode(ipaddress)%>"
                           maxlength="45" pattern="^[0-9\.:]*$"
                           placeholder="e.g., 192.168.1.100 or 2001:db8::1">
                    <small class="form-text text-muted">
                      IPv4 or IPv6 address
                    </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 server..."><%=Server.HTMLEncode(description)%></textarea>
                    <small class="form-text text-muted">
                      Optional: Purpose, rack location, 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 server'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 Server") Else Response.Write("Save Changes")%>
                    </button>
                    <a href="network_devices.asp?filter=Server" 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 server? This action cannot be undone.')) {
    var form = document.createElement('form');
    form.method = 'POST';
    form.action = 'save_network_device.asp';

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

    var idInput = document.createElement('input');
    idInput.type = 'hidden';
    idInput.name = 'id';
    idInput.value = '<%=serverid%>';
    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();
  }
}

// Model/Vendor nested add functionality
$(document).ready(function() {
  // Show/hide new model section
  $('#addModelBtn, #modelid').on('change click', function() {
    if ($('#modelid').val() === 'new' || $(this).attr('id') === 'addModelBtn') {
      $('#modelid').val('new');
      $('#newModelSection').slideDown();
      $('#newmodelnumber').prop('required', true);
      $('#newvendorid').prop('required', true);
    }
  });

  $('#cancelNewModel').on('click', function() {
    $('#newModelSection').slideUp();
    $('#newVendorSection').slideUp();
    $('#modelid').val('');
    $('#newmodelnumber').val('').prop('required', false);
    $('#newvendorid').val('').prop('required', false);
    $('#newmodelnotes').val('');
    $('#newmodeldocpath').val('');
    $('#newvendorname').val('').prop('required', false);
  });

  // Show/hide new vendor section
  $('#addVendorBtn, #newvendorid').on('change click', function() {
    if ($('#newvendorid').val() === 'new' || $(this).attr('id') === 'addVendorBtn') {
      $('#newvendorid').val('new');
      $('#newVendorSection').slideDown();
      $('#newvendorname').prop('required', true);
    }
  });

  $('#cancelNewVendor').on('click', function() {
    $('#newVendorSection').slideUp();
    $('#newvendorid').val('');
    $('#newvendorname').val('').prop('required', false);
  });

  // Form validation
  $('form').on('submit', function(e) {
    // If adding new model, make sure fields are filled
    if ($('#modelid').val() === 'new') {
      if ($('#newmodelnumber').val().trim() === '') {
        e.preventDefault();
        alert('Please enter a model number or select an existing model');
        $('#newmodelnumber').focus();
        return false;
      }
      if ($('#newvendorid').val() === '' || $('#newvendorid').val() === 'new') {
        // If vendor is 'new', check vendor name
        if ($('#newvendorid').val() === 'new') {
          if ($('#newvendorname').val().trim() === '') {
            e.preventDefault();
            alert('Please enter a vendor name or select an existing vendor');
            $('#newvendorname').focus();
            return false;
          }
        } else {
          e.preventDefault();
          alert('Please select a vendor or add a new one');
          $('#newvendorid').focus();
          return false;
        }
      }
    }
  });
});
</script>

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

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