<!-- 导航菜单组件 -->
<th:block
  th:fragment="nav-menu"
  th:if="${not #strings.isEmpty(theme.config.header.primary_menu)}"
  th:with="menu = ${menuFinder.getByName(theme.config.header.primary_menu)}, currentPath = ${#httpServletRequest.requestURI}"
>
  <menu th:if="${menu != null and menu.menuItems != null}">
    <li
      th:each="menuItem : ${menu.menuItems}"
      th:class="${menuItem.children != null and not #lists.isEmpty(menuItem.children)} ? 'has-submenu' : ''"
    >
      <th:block th:if="${menuItem.children != null and not #lists.isEmpty(menuItem.children)}">
        <div class="nav-item-wrapper">
          <a
            th:class="${currentPath != null and menuItem.status?.href != null and (currentPath == menuItem.status.href or #strings.endsWith(menuItem.status.href, currentPath)) ? 'sidebar-nav-item has-dropdown active' : 'sidebar-nav-item has-dropdown'}"
            th:href="${menuItem.status.href}"
            th:attr="target=${(menuItem.spec.target == '_blank' or menuItem.spec.target == 'BLANK' or menuItem.spec.target?.name == 'BLANK') ? '_blank' : ((menuItem.spec.target == '_parent' or menuItem.spec.target == 'PARENT' or menuItem.spec.target?.name == 'PARENT') ? '_parent' : ((menuItem.spec.target == '_top' or menuItem.spec.target == 'TOP' or menuItem.spec.target?.name == 'TOP') ? '_top' : ((menuItem.spec.target == '_self' or menuItem.spec.target == 'SELF' or menuItem.spec.target?.name == 'SELF') ? '_self' : null)))}"
          >
            <img
              th:if="${not #strings.isEmpty(menuItem.metadata?.annotations?.get('icon'))}"
              th:src="${menuItem.metadata.annotations.get('icon')}"
              alt=""
            />
            <span class="nav-text" th:text="${menuItem.status.displayName}"></span>
          </a>
          <button type="button" class="dropdown-toggle" aria-label="Toggle dropdown">
            <span class="icon-[ph--caret-down-bold] dropdown-arrow"></span>
          </button>
        </div>

        <div class="dropdown-menu">
          <a
            th:each="child : ${menuItem.children}"
            th:href="${child.status.href}"
            th:class="${currentPath != null and child.status?.href != null and (currentPath == child.status.href or #strings.endsWith(child.status.href, currentPath)) ? 'dropdown-item active' : 'dropdown-item'}"
            th:attr="target=${(child.spec.target == '_blank' or child.spec.target == 'BLANK' or child.spec.target?.name == 'BLANK') ? '_blank' : ((child.spec.target == '_parent' or child.spec.target == 'PARENT' or child.spec.target?.name == 'PARENT') ? '_parent' : ((child.spec.target == '_top' or child.spec.target == 'TOP' or child.spec.target?.name == 'TOP') ? '_top' : ((child.spec.target == '_self' or child.spec.target == 'SELF' or child.spec.target?.name == 'SELF') ? '_self' : null)))}"
          >
            <img
              th:if="${not #strings.isEmpty(child.metadata?.annotations?.get('icon'))}"
              th:src="${child.metadata.annotations.get('icon')}"
              alt=""
            />
            <span class="nav-text" th:text="${child.status.displayName}"></span>
          </a>
        </div>
      </th:block>
      <a
        th:unless="${menuItem.children != null and not #lists.isEmpty(menuItem.children)}"
        th:href="${menuItem.status.href}"
        th:class="${currentPath != null and menuItem.status?.href != null and (currentPath == menuItem.status.href or #strings.endsWith(menuItem.status.href, currentPath)) ? 'sidebar-nav-item active' : 'sidebar-nav-item'}"
        th:attr="target=${(menuItem.spec.target == '_blank' or menuItem.spec.target == 'BLANK' or menuItem.spec.target?.name == 'BLANK') ? '_blank' : ((menuItem.spec.target == '_parent' or menuItem.spec.target == 'PARENT' or menuItem.spec.target?.name == 'PARENT') ? '_parent' : ((menuItem.spec.target == '_top' or menuItem.spec.target == 'TOP' or menuItem.spec.target?.name == 'TOP') ? '_top' : ((menuItem.spec.target == '_self' or menuItem.spec.target == 'SELF' or menuItem.spec.target?.name == 'SELF') ? '_self' : null)))}"
      >
        <img
          th:if="${not #strings.isEmpty(menuItem.metadata?.annotations?.get('icon'))}"
          th:src="${menuItem.metadata.annotations.get('icon')}"
          alt=""
        />
        <span class="nav-text" th:text="${menuItem.status.displayName}"></span>
        <span th:if="${menuItem.spec.target == '_blank'}" class="icon-[ph--arrow-up-right] external-tip"></span>
      </a>
    </li>
  </menu>
</th:block>
