<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1;" enable-back-to-top="true">
  <!-- #endif -->
    <view class="uni-container" :class="{ 'dark-mode': isDarkMode }">
      <view v-if="!hasLeftWin" class="uni-header-logo">
        <image class="uni-header-image" src="/static/apiIndex.png"></image>
      </view>
      <view v-if="!hasLeftWin" class="uni-text-box">
        <text class="hello-text">以下将演示uni-app接口能力，详细文档见：</text>
        <u-link :href="'https://uniapp.dcloud.io/uni-app-x/api/'" :text="'https://uniapp.dcloud.io/uni-app-x/api/'"
          :inWhiteList="true"></u-link>
      </view>
      <uni-collapse>
        <uni-collapse-item ref="category" v-for="menuItem in menu" :key="menuItem.id" :title="menuItem.name"
          class="item">
          <template v-for="childMenuItem in menuItem.items" :key="childMenuItem.id">
            <view v-if="childMenuItem.items.length==0" class="pl"
              :class="{'uni-navigate-item':childMenuItem.path !== 'set-tab-bar' || windowWidth <= 768}"
              hover-class="is--active" @click="goPage(`/${childMenuItem.path}`)">
              <template v-if="childMenuItem.path !== 'set-tab-bar' || windowWidth <= 768">
                <text class="uni-navigate-text"
                  :class="{'left-win-active': leftWinActive === childMenuItem.path && hasLeftWin}">{{ childMenuItem.style["navigationBarTitleText"] }}</text>
                <image :src="arrowRightIcon" class="uni-icon-size"></image>
              </template>
            </view>
            <uni-collapse v-else style="width: 100%">
              <uni-collapse-item :title="childMenuItem.name" class="item"
                style="margin-bottom: 0;">
                <view class="uni-navigate-item pl" hover-class="is--active"
                  v-for="grandChildMenuItem in childMenuItem.items" :key="grandChildMenuItem.path"
                  @click="goPage(`/${grandChildMenuItem.path}`)">
                  <text class="uni-navigate-text" :class="{
                      'left-win-active':
                        leftWinActive === grandChildMenuItem.path && hasLeftWin,
                    }">{{ grandChildMenuItem.style["navigationBarTitleText"] }}</text>
                  <image :src="arrowRightIcon" class="uni-icon-size"></image>
                </view>
              </uni-collapse-item>
            </uni-collapse>
          </template>
        </uni-collapse-item>
      </uni-collapse>
      <!-- #ifdef MP || WEB -->
      <view v-if="!hasLeftWin" ref="pop" @click="hidePop()" class="popup" @touchmove.prevent="stopPropagation">
      <!-- #endif -->
      <!-- #ifndef MP || WEB -->
      <view v-if="!hasLeftWin" ref="pop" @click="hidePop()" class="popup">
      <!-- #endif -->
        <view style="width: 90%; background-color: white" @click.stop="stopClickPop">
          <api-set-tabbar></api-set-tabbar>
        </view>
      </view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script lang="uts">
  import { generateMenu } from './generateMenu.uts'
  import { MenuItem } from './generateMenu.uts'
  const menu = generateMenu('pages/API')
  import { state } from '@/store/index.uts'
  export default {
    data() {
      return {
        menu: menu as MenuItem[],
        arrowRightIcon: '/static/icons/arrow-right.png'
      }
    },
    computed: {
      hasLeftWin() : boolean {
        return !state.noMatchLeftWindow
      },
      leftWinActive() : string {
        return state.leftWinActive.slice(1)
      },
      windowWidth() : number {
        return uni.getWindowInfo().windowWidth
      },
      isDarkMode() : boolean {
        return state.isDarkMode
      }
    },
    methods: {
      goPage(url : string) {
        if (url == '/set-tab-bar') {
          this.showPop()
        }else {
          if (this.hasLeftWin) {
            uni.reLaunch({ url })
          } else {
            uni.navigateTo({ url })
          }
        }
      },
      showPop: function () {
        (this.$refs["pop"] as UniElement).style.setProperty("display", "flex")
      },
      hidePop: function () {
        (this.$refs["pop"] as UniElement).style.setProperty("display", "none")
      },
      stopClickPop: function (e : PointerEvent) {
        e.stopPropagation() //点击到pop的非灰色区域，拦截点击事件
      },
      stopPropagation() {

      }
    },
    // #ifdef WEB
    watch: {
      $route: {
        immediate: true,
        handler(newRoute) {
          if (newRoute.matched.length) {
            const activeCategoryIndex = this.menu.findIndex(menuItem => menuItem?.items.some(item => this.leftWinActive && this.leftWinActive === item?.path))
            if (activeCategoryIndex > -1) {
              this.$nextTick(() => {
                ((this.$refs.category as ComponentPublicInstance[])[activeCategoryIndex])?.$callMethod('openCollapse', true)
              })
            }
          }
        }
      }
    },
    // #endif
  }
</script>

<style>
  .item {
    margin-bottom: 12px;
  }
  .pl{
    padding-left: 18px
  }

  .popup {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    align-items: center;
    justify-content: center;
    display: none;
    background-color: rgba(16, 16, 16, 0.5);
  }
</style>
