<!--#include file="./includes/sql.asp"-->
<!--#include file="./includes/db_helpers.asp"-->
<%
	' Get and validate linkid
	Dim linkid
	linkid = Request.Querystring("linkid")

	' Basic validation - must be numeric and positive
	If Not IsNumeric(linkid) Or CLng(linkid) < 1 Then
		Response.Redirect("displayknowledgebase.asp")
		Response.End
	End If

	' Get the article details using parameterized query
	Dim strSQL, rs
	strSQL = "SELECT kb.*, app.appname " &_
			"FROM knowledgebase kb " &_
			"INNER JOIN applications app ON kb.appid = app.appid " &_
			"WHERE kb.linkid = ? AND kb.isactive = 1"

	Set rs = ExecuteParameterizedQuery(objConn, strSQL, Array(CLng(linkid)))

	If rs.EOF Then
		rs.Close
		Set rs = Nothing
		objConn.Close
		Response.Redirect("displayknowledgebase.asp")
		Response.End
	End If
%>
<!DOCTYPE html>
<html lang="en">
<head>
  <!--#include file="./includes/header.asp"-->
</head>

<%
	Dim theme
	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-lg-8 offset-lg-2">
          <div class="card">
            <div class="card-body">
              <div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:20px;">
                <h5 class="card-title" style="margin:0;">
                  <i class="zmdi zmdi-edit"></i> Edit Knowledge Base Article
                </h5>
                <a href="./displayknowledgearticle.asp?linkid=<%=linkid%>" class="btn btn-sm btn-secondary">
                  <i class="zmdi zmdi-arrow-left"></i> Cancel
                </a>
              </div>

              <form method="post" action="./updatelink_direct.asp">
                <input type="hidden" name="linkid" value="<%=linkid%>">

                <div class="form-group">
                  <label for="shortdescription">Description <span class="text-danger">*</span></label>
                  <input type="text" class="form-control" id="shortdescription" name="shortdescription"
                         value="<%=Server.HTMLEncode(rs("shortdescription") & "")%>"
                         required maxlength="500" placeholder="Brief description of the article">
                </div>

                <div class="form-group">
                  <label for="linkurl">URL <span class="text-danger">*</span></label>
                  <input type="url" class="form-control" id="linkurl" name="linkurl"
                         value="<%=Server.HTMLEncode(rs("linkurl") & "")%>"
                         required maxlength="2000" placeholder="https://...">
                </div>

                <div class="form-group">
                  <label for="keywords">Keywords</label>
                  <input type="text" class="form-control" id="keywords" name="keywords"
                         value="<%=Server.HTMLEncode(rs("keywords") & "")%>"
                         maxlength="500" placeholder="Space-separated keywords">
                  <small class="form-text text-muted">Keywords help with search - separate with spaces</small>
                </div>

                <div class="form-group">
                  <label for="appid">Topic <span class="text-danger">*</span></label>
                  <div class="input-group">
                    <select class="form-control" id="appid" name="appid" required>
                      <option value='<%=rs("appid")%>'><%=Server.HTMLEncode(rs("appname"))%></option>
<%
	' Get all support teams for dropdown
	Dim rsApps, sqlApps
	sqlApps = "SELECT appid, appname FROM applications WHERE isactive = 1 ORDER BY appname ASC"
	Set rsApps = objconn.Execute(sqlApps)
	While Not rsApps.EOF
		If CLng(rsApps("appid")) <> CLng(rs("appid")) Then
			Response.Write("<option value='" & rsApps("appid") & "'>" & Server.HTMLEncode(rsApps("appname")) & "</option>")
		End If
		rsApps.MoveNext
	Wend
	rsApps.Close
	Set rsApps = Nothing
%>
                      <option value="new">+ Add New Topic</option>
                    </select>
                    <div class="input-group-append">
                      <button type="button" class="btn btn-info" id="addTopicBtn">
                        <i class="zmdi zmdi-plus"></i> New
                      </button>
                    </div>
                  </div>
                  <small class="form-text text-muted">Select the application/topic this article relates to</small>
                </div>

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

                  <div class="form-group">
                    <label for="newappname">Topic Name</label>
                    <input type="text" class="form-control" id="newappname" name="newappname"
                           maxlength="50" placeholder="e.g., Network Tools, Email System">
                  </div>

                  <div class="form-group">
                    <label for="newappdescription">Description</label>
                    <input type="text" class="form-control" id="newappdescription" name="newappdescription"
                           maxlength="255" placeholder="Short description of the topic">
                  </div>

                  <div class="form-group">
                    <label for="newsupportteamid">Support Team <span class="text-danger">*</span></label>
                    <div class="input-group">
                      <select class="form-control" id="newsupportteamid" name="newsupportteamid">
                        <option value="">-- Select Support Team --</option>
<%
    Dim strSupportSQL2, rsSupportTeams2
    strSupportSQL2 = "SELECT supporteamid, teamname FROM supportteams WHERE isactive=1 ORDER BY teamname ASC"
    Set rsSupportTeams2 = objConn.Execute(strSupportSQL2)
    While Not rsSupportTeams2.EOF
        Response.Write("<option value='" & rsSupportTeams2("supporteamid") & "'>")
        Response.Write(Server.HTMLEncode(rsSupportTeams2("teamname")))
        Response.Write("</option>" & vbCrLf)
        rsSupportTeams2.MoveNext
    Wend
    rsSupportTeams2.Close
    Set rsSupportTeams2 = Nothing
%>
                        <option value="new">+ Add New Support Team</option>
                      </select>
                      <div class="input-group-append">
                        <button type="button" class="btn btn-info btn-sm" id="addSupportTeamBtnKB">
                          <i class="zmdi zmdi-plus"></i> New
                        </button>
                      </div>
                    </div>
                  </div>

                  <!-- Nested section for adding new support team -->
                  <div id="newSupportTeamSectionKB" style="display:none; padding:10px; background:rgba(255,255,255,0.05); border:1px solid rgba(255,255,255,0.15); border-radius:3px; margin-bottom:10px;">
                    <h6 class="mb-2" style="font-size:0.9rem;"><i class="zmdi zmdi-users"></i> New Support Team</h6>
                    <div class="form-group mb-2">
                      <label for="newsupportteamname" style="font-size:0.85rem;">Team Name</label>
                      <input type="text" class="form-control form-control-sm" id="newsupportteamname" name="newsupportteamname"
                             maxlength="50" placeholder="e.g., IT Support, Engineering">
                    </div>
                    <div class="form-group mb-2">
                      <label for="newsupportteamurl" style="font-size:0.85rem;">Team URL</label>
                      <input type="text" class="form-control form-control-sm" id="newsupportteamurl" name="newsupportteamurl"
                             maxlength="512" placeholder="Service Now URL or team page">
                    </div>
                    <div class="form-group mb-2">
                      <label for="newappownerid" style="font-size:0.85rem;">App Owner <span class="text-danger">*</span></label>
                      <div class="input-group input-group-sm">
                        <select class="form-control form-control-sm" id="newappownerid" name="newappownerid">
                          <option value="">-- Select App Owner --</option>
<%
    Dim strAppOwnerSQL2, rsAppOwners2
    strAppOwnerSQL2 = "SELECT appownerid, appowner FROM appowners WHERE isactive=1 ORDER BY appowner ASC"
    Set rsAppOwners2 = objConn.Execute(strAppOwnerSQL2)
    While Not rsAppOwners2.EOF
        Response.Write("<option value='" & rsAppOwners2("appownerid") & "'>")
        Response.Write(Server.HTMLEncode(rsAppOwners2("appowner")))
        Response.Write("</option>" & vbCrLf)
        rsAppOwners2.MoveNext
    Wend
    rsAppOwners2.Close
    Set rsAppOwners2 = Nothing
%>
                          <option value="new">+ Add New App Owner</option>
                        </select>
                        <div class="input-group-append">
                          <button type="button" class="btn btn-info btn-sm" id="addAppOwnerBtnKB" style="font-size:0.75rem;">
                            <i class="zmdi zmdi-plus"></i> New
                          </button>
                        </div>
                      </div>
                    </div>

                    <!-- Doubly nested section for adding new app owner -->
                    <div id="newAppOwnerSectionKB" style="display:none; padding:8px; background:rgba(255,255,255,0.08); border:1px solid rgba(255,255,255,0.2); border-radius:2px; margin-bottom:8px;">
                      <h6 class="mb-1" style="font-size:0.8rem;"><i class="zmdi zmdi-account"></i> New App Owner</h6>
                      <div class="form-group mb-1">
                        <label for="newappownername" style="font-size:0.75rem;">Name</label>
                        <input type="text" class="form-control form-control-sm" id="newappownername" name="newappownername"
                               maxlength="50" placeholder="e.g., John Smith" style="font-size:0.85rem;">
                      </div>
                      <div class="form-group mb-1">
                        <label for="newappownersso" style="font-size:0.75rem;">SSO</label>
                        <input type="text" class="form-control form-control-sm" id="newappownersso" name="newappownersso"
                               maxlength="50" placeholder="e.g., jsmith" style="font-size:0.85rem;">
                      </div>
                      <button type="button" class="btn btn-sm btn-secondary" id="cancelNewAppOwnerKB" style="font-size:0.75rem;">
                        <i class="zmdi zmdi-close"></i> Cancel
                      </button>
                    </div>

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

                  <div class="form-group">
                    <label for="newapplicationnotes">Application Notes</label>
                    <textarea class="form-control" id="newapplicationnotes" name="newapplicationnotes"
                              rows="3" maxlength="512"
                              placeholder="Detailed notes about the application..."></textarea>
                  </div>

                  <div class="form-group">
                    <label for="newinstallpath">Installation Path/URL</label>
                    <input type="text" class="form-control" id="newinstallpath" name="newinstallpath"
                           maxlength="255"
                           placeholder="\\server\share\installer.exe or http://...">
                  </div>

                  <div class="form-group">
                    <label for="newdocumentationpath">Documentation Path/URL</label>
                    <input type="text" class="form-control" id="newdocumentationpath" name="newdocumentationpath"
                           maxlength="512"
                           placeholder="\\server\docs or http://docs.example.com">
                  </div>

                  <div class="form-group">
                    <div class="custom-control custom-checkbox">
                      <input type="checkbox" class="custom-control-input"
                             id="newisactive" name="newisactive" value="1" checked>
                      <label class="custom-control-label" for="newisactive">
                        <strong>Active</strong>
                        <small class="d-block text-muted">Topic is currently in use</small>
                      </label>
                    </div>
                  </div>

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

                <hr>

                <div class="form-group text-right">
                  <button type="submit" class="btn btn-primary btn-lg">
                    <i class="zmdi zmdi-check"></i> Update Article
                  </button>
                  <a href="./displayknowledgearticle.asp?linkid=<%=linkid%>" class="btn btn-secondary btn-lg">
                    <i class="zmdi zmdi-close"></i> Cancel
                  </a>
                </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">
      <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>

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

  <script>
  $(document).ready(function() {
      // Show/hide new topic section
      $('#addTopicBtn, #appid').on('change click', function() {
          if ($('#appid').val() === 'new' || $(this).attr('id') === 'addTopicBtn') {
              $('#appid').val('new');
              $('#newTopicSection').slideDown();
              $('#newappname').prop('required', true);
              $('#newsupportteamid').prop('required', true);
          }
      });

      $('#cancelNewTopic').on('click', function() {
          $('#newTopicSection').slideUp();
          $('#appid').val('<%=rs("appid")%>');  // Reset to original value
          $('#newappname').val('').prop('required', false);
          $('#newappdescription').val('');
          $('#newsupportteamid').val('').prop('required', false);
          $('#newapplicationnotes').val('');
          $('#newinstallpath').val('');
          $('#newdocumentationpath').val('');
          $('#newisactive').prop('checked', true);
          // Also hide and clear support team section
          $('#newSupportTeamSectionKB').hide();
          $('#newsupportteamname').val('');
          $('#newsupportteamurl').val('');
      });

      // Show/hide new support team section (nested within topic)
      $('#addSupportTeamBtnKB, #newsupportteamid').on('change click', function() {
          if ($('#newsupportteamid').val() === 'new' || $(this).attr('id') === 'addSupportTeamBtnKB') {
              $('#newsupportteamid').val('new');
              $('#newSupportTeamSectionKB').slideDown();
              $('#newsupportteamname').prop('required', true);
              $('#newappownerid').prop('required', true);
          }
      });

      $('#cancelNewSupportTeamKB').on('click', function() {
          $('#newSupportTeamSectionKB').slideUp();
          $('#newsupportteamid').val('');
          $('#newsupportteamname').val('').prop('required', false);
          $('#newsupportteamurl').val('');
          $('#newappownerid').val('').prop('required', false);
          // Also hide and clear app owner section
          $('#newAppOwnerSectionKB').hide();
          $('#newappownername').val('');
          $('#newappownersso').val('');
      });

      // Show/hide new app owner section (doubly nested within support team)
      $('#addAppOwnerBtnKB, #newappownerid').on('change click', function() {
          if ($('#newappownerid').val() === 'new' || $(this).attr('id') === 'addAppOwnerBtnKB') {
              $('#newappownerid').val('new');
              $('#newAppOwnerSectionKB').slideDown();
              $('#newappownername').prop('required', true);
              $('#newappownersso').prop('required', true);
          }
      });

      $('#cancelNewAppOwnerKB').on('click', function() {
          $('#newAppOwnerSectionKB').slideUp();
          $('#newappownerid').val('');
          $('#newappownername').val('').prop('required', false);
          $('#newappownersso').val('').prop('required', false);
      });

      // Form validation
      $('form').on('submit', function(e) {
          // If adding new topic, make sure name is filled
          if ($('#appid').val() === 'new') {
              if ($('#newappname').val().trim() === '') {
                  e.preventDefault();
                  alert('Please enter a topic name or select an existing one');
                  $('#newappname').focus();
                  return false;
              }

              // Check support team selection
              if ($('#newsupportteamid').val() === 'new') {
                  if ($('#newsupportteamname').val().trim() === '') {
                      e.preventDefault();
                      alert('Please enter a support team name or select an existing one');
                      $('#newsupportteamname').focus();
                      return false;
                  }

                  // Check if adding new app owner within new support team
                  if ($('#newappownerid').val() === 'new') {
                      if ($('#newappownername').val().trim() === '') {
                          e.preventDefault();
                          alert('Please enter an app owner name or select an existing one');
                          $('#newappownername').focus();
                          return false;
                      }
                      if ($('#newappownersso').val().trim() === '') {
                          e.preventDefault();
                          alert('Please enter an SSO for the new app owner');
                          $('#newappownersso').focus();
                          return false;
                      }
                  } else if ($('#newappownerid').val() === '') {
                      e.preventDefault();
                      alert('Please select an app owner for the new support team');
                      $('#newappownerid').focus();
                      return false;
                  }
              } else if ($('#newsupportteamid').val() === '') {
                  e.preventDefault();
                  alert('Please select a support team for the new topic');
                  $('#newsupportteamid').focus();
                  return false;
              }
          }
      });
  });
  </script>

</body>
</html>
<%
	rs.Close
	Set rs = Nothing
	objConn.Close
%>
