Component({
    data: {
      selected: 0,
      color: "#412A12",
      selectedColor: "#B1967B",
      borderStyle: "black",
      backgroundColor: "#ffffff",
      isShowTabar: true,
      list: [
        {
          "pagePath": "/pages/index/index",
          "iconPath": "/imgs/icon_home_normal.png",
          "selectedIconPath": "/imgs/icon_home_active.png",
          "text": "首页"
        },
        {
          "pagePath": "/pages/exhibition/index",
          "iconPath": "/imgs/icon_exhibition_normal.png",
          "selectedIconPath": "/imgs/icon_exhibition_active.png",
          "text": "展览"
        },
        {
          "pagePath": "/pages/cover/index",
          "iconPath": "/imgs/icon_ar.png",
          "selectedIconPath": "/imgs/icon_ar.png",
          "text": "钻头列阵",
          "center": true,
        },
        {
          "pagePath": "/pages/collection/index",
          "iconPath": "/imgs/icon_culture_normal.png",
          "selectedIconPath": "/imgs/icon_culture_active.png",
          "text": "藏品"
        },
        {
          "pagePath": "/pages/user/index",
          "iconPath": "/imgs/icon_user_normal.png",
          "selectedIconPath": "/imgs/icon_user_active.png",
          "text": "我的"
        }
      ]
    },
    attached() {
      const pages = getCurrentPages() || []
      const current = pages[pages.length - 1]
      const route = current ? ('/' + current.route) : ''
      const idx = this.data.list.findIndex(i => i.pagePath === route)
      if (idx !== -1) {
        this.setData({ selected: idx })
      }
    },
    methods: {
      switchTab(e) {
        const data = e.currentTarget.dataset
        const url = data.path
        const item = this.data.list[data.index]
        if (item && item.navigate) {
          wx.navigateTo({ url })
        } else {
          const pages = getCurrentPages() || []
          const current = pages[pages.length - 1]
          const currentRoute = current ? ('/' + current.route) : ''
          // 如果已在当前页，避免重复切换导致页面重载与导航闪烁
          if (currentRoute === url) {
            this.setData({ selected: data.index })
            return
          }
          wx.switchTab({ url })
          // this.setData({ selected: data.index })
        }
      }
    }
  })