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

  <style>
  .notification-row {
    padding: 15px;
    margin-bottom: 10px;
    border: 1px solid #ddd;
    border-radius: 5px;
    background: #fff;
  }

  .notification-row:hover {
    background: #f8f9fa;
  }

  .notification-text {
    font-weight: bold;
    margin-bottom: 5px;
  }

  .notification-meta {
    font-size: 0.9em;
    color: #666;
  }

  .type-selector {
    width: 200px;
  }

  .badge-tbd { background-color: #6c757d; }
  .badge-awareness { background-color: #28a745; }
  .badge-change { background-color: #ffc107; color: #212529; }
  .badge-incident { background-color: #dc3545; }

  .dark-mode .notification-row {
    background: #2a2a2a;
    border-color: #444;
    color: #ddd;
  }

  .dark-mode .notification-row:hover {
    background: #333;
  }

  .dark-mode .notification-meta {
    color: #aaa;
  }
  </style>
</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-12">
          <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> Bulk Update Notification Types
                </h5>
                <div>
                  <a href="./calendar.asp" class="btn btn-secondary">
                    <i class="zmdi zmdi-calendar"></i> Back to Calendar
                  </a>
                  <a href="./displaynotifications.asp" class="btn btn-secondary">
                    <i class="zmdi zmdi-view-list"></i> List View
                  </a>
                </div>
              </div>

<%
' Check for success/error messages
Dim updated, errors
updated = Request.QueryString("updated")
errors = Request.QueryString("errors")

If updated <> "" Then
%>
              <div class="alert alert-success alert-dismissible fade show" role="alert">
                <i class="zmdi zmdi-check-circle"></i>
                <strong>Success!</strong> Updated <%=updated%> notification(s).
                <% If errors <> "" And errors <> "0" Then %>
                  <br><strong>Warning:</strong> <%=errors%> notification(s) failed to update.
                <% End If %>
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
<% End If %>

              <div class="alert alert-info">
                <i class="zmdi zmdi-info"></i>
                <strong>Instructions:</strong> Select the appropriate notification type for each notification below, then click "Save All Changes" at the bottom.
              </div>

              <div style="margin-bottom:20px;">
                <h6>Notification Type Legend:</h6>
                <span class="badge badge-tbd">TBD</span> - Type to be determined (gray on calendar)<br>
                <span class="badge badge-awareness">Awareness</span> - General awareness notification (green on calendar)<br>
                <span class="badge badge-change">Change</span> - Scheduled change or maintenance (yellow on calendar)<br>
                <span class="badge badge-incident">Incident</span> - Active incident or outage (red on calendar)
              </div>

              <form id="bulkUpdateForm" method="POST" action="bulk_update_notification_types_process.asp">
                <div style="margin-bottom:20px;">
                  <button type="button" class="btn btn-sm btn-secondary" onclick="setAllType(2)">Set All to Awareness</button>
                  <button type="button" class="btn btn-sm btn-warning" onclick="setAllType(3)">Set All to Change</button>
                  <button type="button" class="btn btn-sm btn-danger" onclick="setAllType(4)">Set All to Incident</button>
                </div>

<%
' Fetch all active notifications
Dim strSQL, rs
strSQL = "SELECT n.notificationid, n.notification, n.starttime, n.endtime, n.ticketnumber, " & _
         "n.notificationtypeid, nt.typename " & _
         "FROM notifications n " & _
         "LEFT JOIN notificationtypes nt ON n.notificationtypeid = nt.notificationtypeid " & _
         "WHERE n.isactive = 1 " & _
         "ORDER BY n.starttime DESC"
Set rs = objconn.Execute(strSQL)

Dim count
count = 0

If Not rs.EOF Then
    Do While Not rs.EOF
        count = count + 1

        Dim currentType, currentTypeName
        If IsNull(rs("notificationtypeid")) Then
            currentType = 1
            currentTypeName = "TBD"
        Else
            currentType = rs("notificationtypeid")
            currentTypeName = rs("typename")
        End If
%>
                <div class="notification-row">
                  <div class="notification-text">
                    <%=rs("notification")%>
                  </div>
                  <div class="notification-meta">
                    <strong>Date:</strong> <%=FormatDateTime(rs("starttime"), 2)%>
                    <% If Not IsNull(rs("endtime")) And rs("endtime") <> "" Then %>
                      to <%=FormatDateTime(rs("endtime"), 2)%>
                    <% Else %>
                      <span class="badge badge-secondary">ONGOING</span>
                    <% End If %>
                    <% If Not IsNull(rs("ticketnumber")) And rs("ticketnumber") <> "" Then %>
                      | <strong>Ticket:</strong> <%=rs("ticketnumber")%>
                    <% End If %>
                  </div>
                  <div style="margin-top:10px;">
                    <label style="display:inline-block; width:150px;">Current Type:</label>
                    <span class="badge badge-<%=LCase(currentTypeName)%>"><%=currentTypeName%></span>
                  </div>
                  <div style="margin-top:10px;">
                    <label style="display:inline-block; width:150px;">New Type:</label>
                    <select name="type_<%=rs("notificationid")%>" class="form-control type-selector" style="display:inline-block;">
                      <option value="1"<% If currentType = 1 Then Response.Write(" selected") End If %>>TBD</option>
                      <option value="2"<% If currentType = 2 Then Response.Write(" selected") End If %>>Awareness</option>
                      <option value="3"<% If currentType = 3 Then Response.Write(" selected") End If %>>Change</option>
                      <option value="4"<% If currentType = 4 Then Response.Write(" selected") End If %>>Incident</option>
                    </select>
                  </div>
                </div>
<%
        rs.MoveNext
    Loop
End If

rs.Close
Set rs = Nothing
objConn.Close
%>

                <% If count > 0 Then %>
                <div style="margin-top:30px; text-align:center;">
                  <button type="submit" class="btn btn-success btn-lg">
                    <i class="zmdi zmdi-check"></i> Save All Changes (<%=count%> notifications)
                  </button>
                </div>
                <% Else %>
                <div class="alert alert-warning">
                  No active notifications found.
                </div>
                <% End If %>

              </form>

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

    <!-- 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>
  function setAllType(typeId) {
      var selects = document.querySelectorAll('.type-selector');
      selects.forEach(function(select) {
          select.value = typeId;
      });
  }
  </script>

</body>
</html>
