const Http = require("../../utils/HttpBasics");
var config = require("../../config/config.js");
let app = getApp();

Component({
  data: {
    selected: null,
    "color": "#abb1be",
    "selectedColor": "#6c5535",
    list: [
      {
        "pagePath": "/index/index",
        "text": "首页",
        "iconPath": "../../assets/images/home.png",
        "selectedIconPath": "../../assets/images/home_a.png"
      },
      {
        "pagePath": "/index/searchbar",
        "text": "门店",
        "iconPath": "../../assets/images/portal.png",
        "selectedIconPath": "../../assets/images/portal_a.png"
      },
      {
        "pagePath": "/pages/order/index/index",
        "text": "订单",
        "iconPath": "../../assets/images/dindan.png",
        "selectedIconPath": "../../assets/images/dindan_a.png"
      },
      {
        "pagePath": "/index/user",
        "text": "我的",
        "iconPath": "../../assets/images/user.png",
        "selectedIconPath": "../../assets/images/user_a.png"
      }
    ],
    
  },
  properties: {

  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      console.log(app.globalData.selected, 'selected');
      this.setData({
        selected: app.globalData.selected,
        // templateId:app.globalData.templateId
      })
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  methods: {
    navigateTo(e) {
      
      // this.setData({
      //   list: this.data.home_a_img
      // })
      // console.log(this.data.home_a_img)
      

      const data = e.currentTarget.dataset;
      if(app.globalData.selected==data.index){
        return
      }
      // tt.hideLoading();
      tt.showLoading({
        title: "加载中"
      });
      app.globalData.selected = data.index
   
      console.log(app.globalData.selected);
      const url = data.path;
      if (data.index == 0 || data.index == 3) {
        this.setWxMessage()
      }
      if(data.index == 2){
        app.globalData.ifshowtab=true
      }else{
        app.globalData.ifshowtab=false
      }
      // tt.reLaunch({
      //   url
      // });
      tt.reLaunch({
        url: url,
        success: (res) => {
          tt.hideLoading();
        },
        fail: (res) => {
          tt.hideLoading();
        },
      });
    },

    // getTemplateId() {
    //   // console.log("隐藏")
    //   //  tt.hideTabBar()
    //   Http.get({
    //     url: config.api.templateId,
    //     data: {
    //       pageNum: 1,
    //       pageSize: 100
    //     }
    //   }).then(res => {
    //     const {
    //       code,
    //       data
    //     } = res
    //     if (code == 200) {
    //       this.setData({
    //         templateId: data.list
    //       })
    //       this.setWxMessage()
    //     } else {
    //       return
    //     }
    //   })
    // },
    setWxMessage() {
      if(app.globalData.templateId.length<0)return
      console.log(app.globalData.templateId);
      let tmplIds = []
      app.globalData.templateId.map(res => {
        tmplIds.push(res.templateId)
      })
      tt.requestSubscribeMessage({
        tmplIds: tmplIds,
        success(res) {
          console.log("我重复订阅")
          let _statue = null
          tmplIds.map((item, index) => { //判读用户是否点击了确定
            if (res[item] == "accept") {
              _statue = 'ok'
            }
          })
          if (_statue == 'ok') {
            Http.post({
              url: config.api.wxMsg
            }).then(res => {

              tt.setStorageSync({
                key: 'ifShowTab',
                data: true,
              })
              console.log(res)
            })
            return
          }

        },
        fail(error) {
          //订阅失败
          console.log("订阅失败, 错误详情: ", error);
          // tt.showToast({
          //   title: "订阅失败",
          //   icon: "fail",
          // });
        },
        complete() {

        }
      })
    },

  }
})