const extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
let ifStoreApp = extConfig.attr.ifStoreApp;
const Http = require("../utils/HttpBasics");
var config = require("../config/config.js");
let app = getApp();
Component({
  data: {
    home_a_img: [],
    selected: 0,
    "color": "#abb1be",
    "selectedColor": "#FD782D",
    "list": [{
      "pagePath": "/index/index",
      "iconPath": "../assets/images/home.png",
      "selectedIconPath": "../assets/images/home-a.png",
      "text": "首页"
    },
    {
      "pagePath": "/index/searchbar",
      "iconPath": "../assets/images/mendian.png",
      "selectedIconPath": "../assets/images/mendian-a.png",
      "text": "门店"
    },
    {
      "pagePath": "/index/passCar",
      "iconPath": "../assets/images/park.png",
      "selectedIconPath": "../assets/images/park-a.png",
      "text": "停车"
    },
    {
      "pagePath": "/index/user",
      "iconPath": "../assets/images/user.png",
      "selectedIconPath": "../assets/images/user-a.png",
      "text": "我的"
    }
    ]
  },
  created() {
    if (app.globalData.token) {
      this.getType()
    }
  },
  // lifetimes: {
  //   attached: function () {
  //     // 在组件实例进入页面节点树时执行

  //   },
  // },
  attached() {
    if (ifStoreApp == 1) {
      this.setData({
        list: [{
          "pagePath": "/index/index",
          "iconPath": "../assets/images/home.png",
          "selectedIconPath": "../assets/images/home-a.png",
          "text": "首页"
        },
        {
          "pagePath": "/index/user",
          "iconPath": "../assets/images/user.png",
          "selectedIconPath": "../assets/images/user-a.png",
          "text": "我的"
        }
        ]
      })
    } else if (ifStoreApp == 2) {
      this.setData({
        list: [{
          "pagePath": "/index/index",
          "iconPath": "../assets/images/home.png",
          "selectedIconPath": "../assets/images/home-a.png",
          "text": "首页"
        },
        {
          "pagePath": "/index/searchbar",
          "iconPath": "../assets/images/mendian.png",
          "selectedIconPath": "../assets/images/mendian-a.png",
          "text": "门店"
        },
        {
          "pagePath": "/index/user",
          "iconPath": "../assets/images/user.png",
          "selectedIconPath": "../assets/images/user-a.png",
          "text": "我的"
        }
        ]
      })
    }
  },
  methods: {
    switchTab(e) {

      // this.setData({
      //   list: this.data.home_a_img
      // })
      // console.log(this.data.home_a_img)

      const data = e.currentTarget.dataset;
      this.setData({
        selected: data.index
      })
      console.log(data.index)
      const url = data.path;
      if (data.index == 0 || data.index == 3) {
        this.getTemplateId()
      }

      wx.switchTab({
        url
      });
    },
    /**
     * 获取全局样式
     */
    getType() {
      Http.get({
        url: config.api.setType,
        data: {
          mouldType: app.globalData.mouldType
        }
      }).then(res => {
        const {
          code,
          data
        } = res
        if (code == 200) {
          app.globalData.styleLsit = data
          this.setTabStyle()
        }
        // console.log(this.data.list[0].selectedIconPath, '99999999999999999999999999999999999999999')
      })
    },
    // 初始换底部导航栏
    setTabStyle: function () {
      const that = this
      console.log(app.globalData.mouldType, "mouldType")
      if (app.globalData.mouldType == 0) {
        that.setData({
          selectedColor: `${app.globalData.styleLsit.t_in_std.styleClass}`,
          list: [{
            "pagePath": "/index/index",
            "iconPath": `${app.globalData.styleLsit.t_in.icon}`,
            "selectedIconPath": `${app.globalData.styleLsit.t_in_std.icon}`,
            "text": "首页"
          },
          {
            "pagePath": "/index/searchbar",
            "iconPath": `${app.globalData.styleLsit.t_md.icon}`,
            "selectedIconPath": `${app.globalData.styleLsit.t_md_std.icon}`,
            "text": "门店"
          },
          {
            "pagePath": "/index/passCar",
            "iconPath": `${app.globalData.styleLsit.t_tc.icon}`,
            "selectedIconPath": `${app.globalData.styleLsit.t_tc_std.icon}`,
            "text": "停车"
          },
          {
            "pagePath": "/index/user",
            "iconPath": `${app.globalData.styleLsit.t_uc.icon}`,
            "selectedIconPath": `${app.globalData.styleLsit.t_uc_std.icon}`,
            "text": "我的"
          }
          ],
        })
      } else if (app.globalData.mouldType == 1) {
        that.setData({ //getTabBar().
          selectedColor: `${app.globalData.styleLsit.t_in_std.styleClass}`,
          list: [{
            "pagePath": "/pages/theme/index1/index",
            "iconPath": `${app.globalData.styleLsit.t_in.icon}`,
            "selectedIconPath": `${app.globalData.styleLsit.t_in_std.icon}`,
            "text": "首页"
          },
          {
            "pagePath": "/index/searchbar",
            "iconPath": `${app.globalData.styleLsit.t_md.icon}`,
            "selectedIconPath": `${app.globalData.styleLsit.t_md_std.icon}`,
            "text": "门店"
          },
          {
            "pagePath": "/index/passCar",
            "iconPath": `${app.globalData.styleLsit.t_tc.icon}`,
            "selectedIconPath": `${app.globalData.styleLsit.t_tc_std.icon}`,
            "text": "停车"
          },
          {
            "pagePath": "/index/user",
            "iconPath": `${app.globalData.styleLsit.t_uc.icon}`,
            "selectedIconPath": `${app.globalData.styleLsit.t_uc_std.icon}`,
            "text": "我的"
          }
          ],
        })

      }

    },

    getTemplateId() {
      // console.log("隐藏")
      //  wx.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 || ''
          })
          this.setWxMessage()
        } else {
          return
        }
      })
    },
    setWxMessage() {
      let tmplIds = []
      this.data.templateId.map(res => {
        tmplIds.push(res.templateId)
      })
      wx.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 => {

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

        },
        complete() {

        }
      })
    },

  }

})