<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/cssIndex.png"></image>
      </view>
      <view v-if="!hasLeftWin" class="uni-text-box">
        <text class="hello-text">uni-app x目前已支持的CSS属性，展示样式仅供参考，文档详见：</text>
        <u-link :href="'https://uniapp.dcloud.io/uni-app-x/css/'" :text="'https://uniapp.dcloud.io/uni-app-x/css/'"
          :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="uni-navigate-item pl"
              hover-class="is--active" @click="goPage(`/${childMenuItem.path}`)">
              <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>
            </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>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script lang="uts">
  import { generateMenu } from './generateMenu.uts'
  import { MenuItem } from './generateMenu.uts'
  const menu = generateMenu('pages/CSS')
  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)
      },
      isDarkMode() : boolean {
        return state.isDarkMode
      }
    },
    methods: {
      goPage(url : string) {
        if (this.hasLeftWin) {
          uni.reLaunch({ url })
        } else {
          uni.navigateTo({ url })
        }
      }
    },
    // #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
  }
</style>
