<div class="toolbar" th:fragment="toolbar" th:if="${theme.config.post.enable_toolbar != true}">
  <div></div>

  <div class="order-toggle">
    <div class="dropdown dropdown-end">
      <div tabindex="0" role="button" class="dropdown-trigger">
        <th:block
          th:with="iconVal = ${category?.metadata?.annotations?.get('categoryIicon')}, colorVal = ${category?.metadata?.annotations?.get('color')}"
        >
          <img th:if="${not #strings.isEmpty(iconVal)}" th:src="${iconVal}" alt="" />
          <span th:if="${#strings.isEmpty(iconVal)}" class="icon-[ph--folder-bold]"></span>
          <span
            th:text="${category != null ? category.spec.displayName : '全部分类'}"
            th:style="${#strings.isEmpty(colorVal) ? null : 'color: ' + colorVal}"
            >全部分类</span
          >
        </th:block>
        <span class="icon-[ph--caret-down-bold]" style="font-size: 0.75em"></span>
      </div>
      <ul tabindex="0" class="dropdown-content menu bg-base-100 rounded-box z-9999 w-48 p-2 shadow-lg">
        <li>
          <a href="/" th:classappend="${category == null} ? 'active' : ''">
            <span class="icon-[ph--squares-four-bold]"></span>
            全部分类
          </a>
        </li>
        <th:block th:each="cat : ${categoryFinder.listAsTree()}">
          <th:block th:replace="~{modules/order-toggle :: catItem(cat=${cat}, level=0)}" />
        </th:block>
      </ul>
    </div>
  </div>
</div>

<!-- 递归分类项片段 -->
<th:block th:fragment="catItem(cat, level)">
  <li class="cat-level" th:classappend="'level-' + ${level}">
    <a
      th:href="${cat.status.permalink}"
      th:classappend="${category != null and category.metadata.name == cat.metadata.name} ? 'active' : ''"
    >
      <!-- 层级缩进线 -->
      <span th:if="${level > 0}" class="cat-indent">
        <span th:each="i : ${#numbers.sequence(1, level)}" class="cat-indent-line"></span>
        <span class="cat-indent-corner">└</span>
      </span>
      <th:block
        th:with="iconVal = ${cat.metadata?.annotations?.get('categoryIicon')}, colorVal = ${cat.metadata?.annotations?.get('color')}"
      >
        <img th:if="${not #strings.isEmpty(iconVal)}" th:src="${iconVal}" alt="" />
        <span
          th:if="${#strings.isEmpty(iconVal)}"
          th:classappend="${level == 0} ? 'icon-[ph--folder]' : 'icon-[ph--folder-simple]'"
        ></span>
        <span
          th:text="${cat.spec.displayName}"
          th:style="${#strings.isEmpty(colorVal) ? null : 'color: ' + colorVal}"
        ></span>
      </th:block>
    </a>
  </li>
  <!-- 递归渲染子分类 -->
  <th:block th:if="${not #lists.isEmpty(cat.children)}" th:each="child : ${cat.children}">
    <th:block th:replace="~{modules/order-toggle :: catItem(cat=${child}, level=${level + 1})}" />
  </th:block>
</th:block>
