<script th:fragment="stats-scripts" th:inline="javascript">
  (function () {
    const startTime = /*[[${theme.config.aside.basic.site_start_time}]]*/ "";
    if (startTime) {
      const start = new Date(startTime).getTime();
      const now = Date.now();
      const days = Math.floor((now - start) / (1000 * 60 * 60 * 24));

      const years = Math.floor(days / 365);
      const remainingDays = days % 365;
      const months = Math.floor(remainingDays / 30);

      let text = "";
      if (years > 0) text += years + "年";
      if (months > 0) text += months + "个月";
      if (years === 0 && months === 0) text = days + "天";

      const el = document.getElementById("site-runtime");
      if (el) el.textContent = text;
    }

    document.querySelectorAll(".time-ago").forEach((el) => {
      const timeStr = el.getAttribute("data-time");
      if (timeStr) {
        const time = new Date(timeStr).getTime();
        const now = Date.now();
        const diff = now - time;
        const minutes = Math.floor(diff / (1000 * 60));
        const hours = Math.floor(diff / (1000 * 60 * 60));
        const days = Math.floor(diff / (1000 * 60 * 60 * 24));

        let text = "";
        if (days > 0) text = days + "天前";
        else if (hours > 0) text = hours + "小时前";
        else if (minutes > 0) text = minutes + "分钟前";
        else text = "刚刚";

        el.textContent = text;
        el.title = "更新于 " + new Date(timeStr).toLocaleString("zh-CN");
      }
    });
  })();
</script>
