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

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

	' Get the notification details using parameterized query
	Dim strSQL, rs
	strSQL = "SELECT * FROM notifications WHERE notificationid = ?"
	Set rs = ExecuteParameterizedQuery(objConn, strSQL, Array(CLng(notificationid)))

	If rs.EOF Then
		rs.Close
		Set rs = Nothing
		objConn.Close
		Response.Redirect("displaynotifications.asp")
		Response.End
	End If

	' Convert datetime to datetime-local format (YYYY-MM-DDTHH:MM)
	Dim startFormatted, endFormatted
	If IsNull(rs("starttime")) Or rs("starttime") = "" Then
		startFormatted = ""
	Else
		' Handle both MySQL format and VBScript Date format
		If VarType(rs("starttime")) = 7 Then
			' VarType 7 is Date - format it properly
			startFormatted = Year(rs("starttime")) & "-" & _
			                 Right("0" & Month(rs("starttime")), 2) & "-" & _
			                 Right("0" & Day(rs("starttime")), 2) & "T" & _
			                 Right("0" & Hour(rs("starttime")), 2) & ":" & _
			                 Right("0" & Minute(rs("starttime")), 2)
		Else
			' String format - try to convert
			startFormatted = Left(Replace(rs("starttime"), " ", "T"), 16)
		End If
	End If

	If IsNull(rs("endtime")) Or rs("endtime") = "" Then
		endFormatted = ""
	Else
		' Handle both MySQL format and VBScript Date format
		If VarType(rs("endtime")) = 7 Then
			' VarType 7 is Date - format it properly
			endFormatted = Year(rs("endtime")) & "-" & _
			               Right("0" & Month(rs("endtime")), 2) & "-" & _
			               Right("0" & Day(rs("endtime")), 2) & "T" & _
			               Right("0" & Hour(rs("endtime")), 2) & ":" & _
			               Right("0" & Minute(rs("endtime")), 2)
		Else
			' String format - try to convert
			endFormatted = Left(Replace(rs("endtime"), " ", "T"), 16)
		End If
	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 Notification
                </h5>
                <a href="./displaynotifications.asp" class="btn btn-sm btn-secondary">
                  <i class="zmdi zmdi-arrow-left"></i> Cancel
                </a>
              </div>

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

                <div class="form-group">
                  <label for="notification">Message <span class="text-danger">*</span></label>
                  <textarea class="form-control" id="notification" name="notification" rows="3"
                         required maxlength="500"><%=Server.HTMLEncode(rs("notification") & "")%></textarea>
                  <small class="form-text text-muted">This message will appear on the dashboard</small>
                </div>

                <div class="form-group">
                  <label for="notificationtypeid">Type <span class="text-danger">*</span></label>
                  <select class="form-control" id="notificationtypeid" name="notificationtypeid" required>
<%
                    Dim rsTypes, currentTypeId
                    currentTypeId = rs("notificationtypeid")
                    Set rsTypes = objConn.Execute("SELECT notificationtypeid, typename, typedescription FROM notificationtypes WHERE isactive = 1 ORDER BY notificationtypeid")
                    While Not rsTypes.EOF
                      Dim isSelectedType
                      isSelectedType = ""
                      If CLng(rsTypes("notificationtypeid")) = CLng(currentTypeId) Then
                        isSelectedType = " selected"
                      End If
%>
                    <option value="<%=rsTypes("notificationtypeid")%>"<%=isSelectedType%>><%=rsTypes("typename")%><%If Not IsNull(rsTypes("typedescription")) Then%> - <%=rsTypes("typedescription")%><%End If%></option>
<%
                      rsTypes.MoveNext
                    Wend
                    rsTypes.Close
                    Set rsTypes = Nothing
%>
                  </select>
                  <small class="form-text text-muted">Classification type for this notification</small>
                </div>

                <div class="form-group">
                  <label for="businessunitid">Business Unit <span class="text-muted">(Optional - blank applies to all)</span></label>
                  <select class="form-control" id="businessunitid" name="businessunitid">
                    <option value="">-- All Business Units --</option>
<%
                    Dim rsBusinessUnits, currentBusinessUnitId
                    currentBusinessUnitId = rs("businessunitid") & ""
                    Set rsBusinessUnits = objConn.Execute("SELECT businessunitid, businessunit FROM businessunits WHERE isactive = 1 ORDER BY businessunit")
                    Dim isSelectedBU
                    While Not rsBusinessUnits.EOF
                      isSelectedBU = ""
                      If currentBusinessUnitId <> "" And IsNumeric(currentBusinessUnitId) Then
                        If CLng(rsBusinessUnits("businessunitid")) = CLng(currentBusinessUnitId) Then
                          isSelectedBU = " selected"
                        End If
                      End If
%>
                    <option value="<%=rsBusinessUnits("businessunitid")%>"<%=isSelectedBU%>><%=rsBusinessUnits("businessunit")%></option>
<%
                      rsBusinessUnits.MoveNext
                    Wend
                    rsBusinessUnits.Close
                    Set rsBusinessUnits = Nothing
%>
                  </select>
                  <small class="form-text text-muted">Select a specific business unit or leave blank to apply to all</small>
                </div>

                <div class="form-group">
                  <label for="appid">Related Application <span class="text-muted">(Optional)</span></label>
                  <select class="form-control" id="appid" name="appid">
                    <option value="">-- No Application --</option>
<%
                    Dim rsApps, currentAppId
                    currentAppId = rs("appid") & ""
                    Set rsApps = objConn.Execute("SELECT appid, appname FROM applications WHERE isactive = 1 ORDER BY appname")
                    While Not rsApps.EOF
                      If currentAppId <> "" And CStr(rsApps("appid")) = currentAppId Then
                        Response.Write("<option value=""" & rsApps("appid") & """ selected>" & Server.HTMLEncode(rsApps("appname") & "") & "</option>")
                      Else
                        Response.Write("<option value=""" & rsApps("appid") & """>" & Server.HTMLEncode(rsApps("appname") & "") & "</option>")
                      End If
                      rsApps.MoveNext
                    Wend
                    rsApps.Close
                    Set rsApps = Nothing
%>
                  </select>
                  <small class="form-text text-muted">Link this notification to a specific application (e.g., for software updates)</small>
                </div>

                <div class="form-group">
                  <label for="ticketnumber">Ticket Number</label>
                  <input type="text" class="form-control" id="ticketnumber" name="ticketnumber"
                         value="<%=Server.HTMLEncode(rs("ticketnumber") & "")%>"
                         maxlength="50" placeholder="GEINC123456 or GECHG123456">
                  <small class="form-text text-muted">Optional ServiceNow ticket number</small>
                </div>

                <div class="form-row">
                  <div class="form-group col-md-6">
                    <label for="starttime">Start Time <span class="text-danger">*</span></label>
                    <div class="input-group">
                      <input type="datetime-local" class="form-control" id="starttime" name="starttime"
                           value="<%=startFormatted%>" required>
                      <div class="input-group-append">
                        <button type="button" class="btn btn-outline-primary" onclick="setNow('starttime')" title="Set to current date/time">
                          <i class="zmdi zmdi-time"></i> Now
                        </button>
                      </div>
                    </div>
                    <small class="form-text text-muted">When notification becomes visible</small>
                  </div>

                  <div class="form-group col-md-6">
                    <label for="endtime">End Time <span class="text-muted">(Optional - indefinite if blank)</span></label>
                    <div class="input-group">
                      <input type="datetime-local" class="form-control" id="endtime" name="endtime"
                           value="<%=endFormatted%>">
                      <div class="input-group-append">
                        <button type="button" class="btn btn-outline-primary" onclick="setNow('endtime')" title="Set to current date/time">
                          <i class="zmdi zmdi-time"></i> Now
                        </button>
                        <button type="button" class="btn btn-outline-secondary" onclick="clearEndtime()" title="Clear to make indefinite">
                          <i class="zmdi zmdi-close"></i>
                        </button>
                      </div>
                    </div>
                    <small class="form-text text-muted">Leave blank for indefinite (will display until you set an end date)</small>
                  </div>
                </div>

                <div class="form-group">
                  <input type="hidden" name="isactive_submitted" value="1">
                  <div class="custom-control custom-checkbox">
                    <input type="checkbox" class="custom-control-input" id="isactive" name="isactive" value="1" <%If CBool(rs("isactive")) = True Then Response.Write("checked") End If%>>
                    <label class="custom-control-label" for="isactive">Active</label>
                  </div>
                  <small class="form-text text-muted">Uncheck to hide notification without deleting</small>
                </div>

                <div class="form-group">
                  <input type="hidden" name="isshopfloor_submitted" value="1">
                  <div class="custom-control custom-checkbox">
                    <input type="checkbox" class="custom-control-input" id="isshopfloor" name="isshopfloor" value="1" <%If CBool(rs("isshopfloor")) = True Then Response.Write("checked") End If%>>
                    <label class="custom-control-label" for="isshopfloor">Show on Shopfloor Dashboard</label>
                  </div>
                  <small class="form-text text-muted">Check this to display on the shopfloor TV dashboard (72-hour window)</small>
                </div>

                <hr>

                <div class="form-group text-right">
                  <button type="submit" class="btn btn-primary btn-lg">
                    <i class="zmdi zmdi-check"></i> Update Notification
                  </button>
                  <a href="./displaynotifications.asp" 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>
    // Format: YYYY-MM-DDTHH:MM
    function formatDateTime(date) {
      var year = date.getFullYear();
      var month = ('0' + (date.getMonth() + 1)).slice(-2);
      var day = ('0' + date.getDate()).slice(-2);
      var hours = ('0' + date.getHours()).slice(-2);
      var minutes = ('0' + date.getMinutes()).slice(-2);
      return year + '-' + month + '-' + day + 'T' + hours + ':' + minutes;
    }

    // Set field to current date/time
    function setNow(fieldId) {
      var now = new Date();
      document.getElementById(fieldId).value = formatDateTime(now);
    }

    // Clear end time to make notification indefinite
    function clearEndtime() {
      document.getElementById('endtime').value = '';
    }
  </script>

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