// packageE/top_navigation/top_navigation.js
Component({
  properties: {
    datas: {
      type: null,
    },
    btnFlag: {
      type: null,
    },
    page_id: {
      type: null,
    },
  },
  // 私有数据，可用于模板渲染
  data: {
    emptyImage: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/image.png",
    clientWidth: "375",

    selectedMenuIndex: -1,
    routeData: "",
  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      let that = this;
      var pages = getCurrentPages(); //获取加载的页面
      var currentPage = pages[pages.length - 1]; //获取当前页面的对象
      this.setData({
        routeData: currentPage.route,
      });
      if (this.data.datas) {
        if (that.data.datas.title_list.value) {
          let menus = that.data.datas.title_list.value;
          for (let i = 0; i < menus.length; i++) {
            let url = menus[i].h5_link;
            if (url.indexOf(that.data.routeData) >= 0) {
              if (that.data.routeData == "packageG/newDiy/newDiy") {
                //diy页面加判断个id，来判断是否当前diy页面
                if (
                  currentPage.options &&
                  url.indexOf(Number(currentPage.options.page_id)) >= 0
                ) {
                  that.setData({
                    selectedMenuIndex: i,
                  });
                  break;
                }
              } else {
                that.setData({
                  selectedMenuIndex: i,
                });
                break;
              }
            }
          }
        }
      }
    },
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {},
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    this.setData({
      clientWidth: wx.getSystemInfoSync().windowWidth,
    });
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },
  methods: {
    tosearch() {
      wx.navigateTo({
        url: "/packageB/member/category/search_v2/search_v2",
      });
    },
    selectedMenu(e) {
      let item = e.currentTarget.dataset.item;
      let index = e.currentTarget.dataset.index;
      if (item.is_minApp == 2) {
        if (item.gh_id) {
          if (item.minApp_link) {
            try {
              wx.navigateToMiniProgram({
                appId: item.gh_id,
                path: item.minApp_link,
                extraData: {},
                envVersion: "release",
                success(res) {
                  // 打开成功
                },
                fail(res) {
                  wx.showToast({
                    title: "小程序跳转失败",
                    icon: "none",
                    duration: 2000,
                  });
                },
              });
            } catch (e) {
              wx.showToast({
                title: "小程序跳转失败",
                icon: "none",
                duration: 2000,
              });
            }
          } else {
            wx.showToast({
              title: "请设置跳转小程序后的跳转链接",
              icon: "none",
              duration: 2000,
            });
          }
        } else {
          wx.showToast({
            title: "请设置小程序所需跳转appId",
            icon: "none",
            duration: 2000,
          });
        }
      } else {
        if (item.h5_link === this.data.routeData) {
          return;
        }
        let url = item.h5_link;
        this.setData({
          selectedMenuIndex: index,
        });
        if (url) {
          try {
            wx.redirectTo({
              url: url,
            });
          } catch (error) {
            try {
              wx.reLaunch({
                url: url,
              });
            } catch (e) {
              wx.showToast({
                title: "链接设置错误:" + url,
                icon: "none",
                duration: 2000,
              });
            }
          }
          
        } else {
          wx.showToast({
            title: "请设置跳转链接",
            icon: "none",
            duration: 2000,
          });
        }
      }
    },
    gotoUrl(){
      let item = this.data.datas.notice_link && this.data.datas.notice_link.value || {};
      if (item.is_h5 == 2) {
        if (item.gh_id) {
          if (item.minApp_link) {
            try {
              wx.navigateToMiniProgram({
                appId: item.gh_id,
                path: item.minApp_link,
                extraData: {},
                envVersion: "release",
                success(res) {
                  // 打开成功
                },
                fail(res) {
                  wx.showToast({
                    title: "小程序跳转失败",
                    icon: "none",
                    duration: 2000,
                  });
                },
              });
            } catch (e) {
              wx.showToast({
                title: "小程序跳转失败",
                icon: "none",
                duration: 2000,
              });
            }
          } else {
            wx.showToast({
              title: "请设置跳转小程序后的跳转链接",
              icon: "none",
              duration: 2000,
            });
          }
        } else {
          wx.showToast({
            title: "请设置小程序所需跳转appId",
            icon: "none",
            duration: 2000,
          });
        }
      } else {
        let url = item.link;
        try {
          wx.navigateTo({
            url: url,
          });
        } catch (e) {
          wx.showToast({
            title: "链接设置错误",
            icon: "none",
            duration: 2000,
          });
        }
      }
    }
  },
});
