<!-- top start -->
<style>
  /* dropdown 크기 및 스크롤 개선 */
  #noticeList {
    max-height: 300px;
    overflow-y: auto;
  }
</style>
<nav class="navbar navbar-expand-lg fixed-top"
  style="background-color:#E7E7E7; border-top:1px solid darkgray; border-bottom:1px solid darkgray;">
  <div class="container-fluid">
    <nav class="nav"><a class="navbar-brand" href="/admin">DBMS 관리</a></nav>
    <ul class="nav justify-content-center">
      <li class="nav-item"><?= view_cell("\App\Cells\SearchCell::client", []) ?></li>
      <li class="nav-item">
        <?= form_open("/admin/search", ['method' => 'GET', 'class' => 'd-flex gap-2']) ?>
        <input type="text" name="keyword" placeholder="고객명/IP/서버명/기타 검색" id="search_keyword" class="form-control" />
        <button type="submit" class="btn btn-default border border-dark"><?= ICONS['SEARCH'] ?></button>
        <?= form_close(); ?>
      </li>
      <li class="nav-item" style="background-color:white; margin-left:20px; font-size:12px;">
        <!-- 🔔 공지 영역 -->
        <ul class="navbar-nav ms-auto">
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="noticeDropdown" role="button" data-bs-toggle="dropdown"
              aria-expanded="false">
              🔔 공지사항 <span id="notice-count" class="badge bg-danger">0</span>
            </a>
            <ol class="dropdown-menu dropdown-menu-end" aria-labelledby="noticeDropdown" id="noticeList">
              <li class="dropdown-item text-muted">불러오는 중...</li>
            </ol>
          </li>
        </ul>
        <!-- 🔔 공지 영역 -->
      </li>
      <li class="nav-item" style="background-color:white; margin-left:20px; font-size:12px;">
        <!-- 🔔 업무요청 영역 -->
        <ul class="navbar-nav ms-auto">
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="requesttaskDropdown" role="button"
              data-bs-toggle="dropdown" aria-expanded="false">
              🔔 업무요청 <span id="requesttask-count" class="badge bg-danger">0</span>
            </a>
            <ol class="dropdown-menu dropdown-menu-end" aria-labelledby="requesttaskDropdown" id="requesttaskList">
              <li class="dropdown-item text-muted">불러오는 중...</li>
            </ol>
          </li>
        </ul>
        <!-- 🔔 업무요청 영역 -->
      </li>
      <li class="nav-item" style="background-color:white; margin-left:20px; font-size:12px;">
        📋:클립보드COPY , 📌:실서비스서버 , ✔️:대체서버->실서비스서버 교체 , 🔄:대체서버등록 , <?= ICONS['MONTH'] ?>:월비용 ,
        <?= ICONS['ONETIME'] ?>:일회성<BR>
        <?= ICONS['SERVER_ITEM_SWITCH'] ?>:스위치 , <?= ICONS['SERVER_ITEM_IP'] ?>:IP , <?= ICONS['SERVER_ITEM_OS'] ?>:OS ,
        <?= ICONS['SERVER_ITEM_SOFTWARE'] ?>:소프트웨어 , <?= ICONS['SERVER_ITEM_DEFENCE'] ?>:CS ,
        <?= ICONS['SERVER_ITEM_CPU'] ?>:CPU , <?= ICONS['SERVER_ITEM_RAM'] ?>:RAM ,
        <?= ICONS['SERVER_ITEM_DISK'] ?>:DISK
      </li>
    </ul>
    <ul class="nav justify-content-end">
      <li class="nav-item">
        <?php if ($viewDatas['authContext']->isLoggedIn()): ?>
          <div class="btn-group">
            <button type="button" class="btn btn-outline-primary dropdown-toggle" data-bs-toggle="dropdown"
              aria-expanded="false">
              <b><?= ICONS['LOGIN'] . $viewDatas['authContext']->getName() ?></b>
            </button>
            <ul class="dropdown-menu dropdown-menu-end">
              <li><?= form_label(
                ICONS['SETUP'] . "정보수정",
                "modify",
                [
                  "class" => "dropdown-item form-label-sm",
                  "data-src" => "/admin/user/modify/" . $viewDatas['authContext']->getUID(),
                  "data-bs-toggle" => "modal",
                  "data-bs-target" => "#modal_action_form"
                ]
              ) ?></li>
              <li>
                <hr class="dropdown-divider">
              </li>
              <li><a class="dropdown-item" href="<?= URLS['LOGOUT'] ?>"><?= ICONS['LOGOUT'] ?>Logout</a></li>
            </ul>
          </div>
        <?php else: ?>
          <a class="nav-link dropdown-toggle" href="<?= URLS['LOGIN'] ?>" role="button"><?= ICONS['LOGIN'] ?>Login</a>
        <?php endif ?>
      </li>
    </ul>
  </div>
</nav>
<script>
  //전체검색용
  document.addEventListener('DOMContentLoaded', () => {
    const params = new URLSearchParams(location.search);
    const keyword = params.get('keyword');
    if (keyword)
      document.querySelector('input[name="keyword"]').value = keyword;
  });
  //공지사항용
  document.addEventListener("DOMContentLoaded", function () {
    async function loadNotices() {
      try {
        const res = await fetch("/admin/board/latest/<?= BOARD['CATEGORY']['NOTICE'] ?>");
        const notices = await res.json();
        const notice_list = document.getElementById("noticeList");
        const notice_count = document.getElementById("notice-count");
        notice_list.innerHTML = "";
        if (notices.length === 0) {
          notice_list.innerHTML = '<li class="dropdown-item text-muted">공지 없음</li>';
          return;
        }
        notice_count.innerHTML = notices.length;
        notices.forEach(n => {
          const item = document.createElement("li");
          item.classList.add("dropdown-item");
          item.innerHTML = `<small class="text-muted">[${n.user}/${n.created_at}]</small> ${n.title}<br>`;
          notice_list.appendChild(item);
        });
      } catch (err) {
        console.error("공지 불러오기 실패:", err);
      }
    }
    async function loadrequesttasks() {
      try {
        const res = await fetch("/admin/board/latest/<?= BOARD['CATEGORY']['REQUESTTASK'] ?>");
        const requesttasks = await res.json();
        const requesttask_list = document.getElementById("requesttaskList");
        const requesttask_count = document.getElementById("requesttask-count");
        requesttask_list.innerHTML = "";
        if (requesttasks.length === 0) {
          requesttask_list.innerHTML = '<li class="dropdown-item text-muted">업무요청 없음</li>';
          return;
        }
        requesttask_count.innerHTML = requesttasks.length;
        requesttasks.forEach(n => {
          const item = document.createElement("li");
          item.classList.add("dropdown-item");
          item.innerHTML = `<small class="text-muted">[${n.user}/${n.created_at}]</small> ${n.title}<br>`;
          requesttask_list.appendChild(item);
        });
      } catch (err) {
        console.error("공지 불러오기 실패:", err);
      }
    }
    loadNotices(); // 최초 로드
    loadrequesttasks(); // 최초 로드
    setInterval(loadNotices, 60000); // 60초마다 갱신
    setInterval(loadrequesttasks, 60000); // 60초마다 갱신
  });
</script>
<!-- top end -->