// pages/index/index.js
const menuItemDict = require('../../utils/menuList.js')
const getUserGroupByRole = require('../../utils/getUserGroupByRole.js')
const toggleCustomTabBar = require('../../custom-tab-bar/toggleCustomTabBar')

import scanQRCode from '../../utils/scanQRCode'

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    debugText: "调试",
    displayUsername: "",
    userGroup: "unknown",
    filterMenuItems: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    toggleCustomTabBar.toggle(this)
    console.log("index/index onLoad", options)
    // console.log("wx.getLaunchOptionsSync()", wx.getLaunchOptionsSync())

    let userInfo = wx.getStorageSync("userInfo")
    if (!userInfo) {
      console.log("用户未登录")
      wx.redirectTo({
        url: '/pages/index/login'
      })
      return
    } else {
      // 用户已登录
      let userGroup = getUserGroupByRole(userInfo.roleId)
      this.setData({
        debugText: JSON.stringify(options, null, 4),
        userInfo: userInfo,
        userGroup: userGroup,
        displayUsername: userInfo ? (`『${userInfo.realname}』`) : "请登录",
        filterMenuItems: this.getFilterMenuItems(menuItemDict, userGroup)
      })
      console.log("menuItemDict", menuItemDict)
      console.log("filterMenuItems", this.data.filterMenuItems)

      //判断用户是否是通过扫小程序码进来的
      if (options && options.scene) {
        // 扫门禁的小程序码
        let scene = options.scene
        if (scene.startsWith("guard")) {
          options.scene = null // 清掉参数 避免重复触发
          wx.navigateTo({
            url: "/pages/scan/entrance?scene=" + scene
          })
          return
        }
      }

      // // 开发模式下自动跳转到指定页面，节省开发时间
      // console.log("app.globalData", app.globalData)
      // if (app.globalData.debugMode) {
      //   wx.switchTab({
      //     // url: '/pages/residents/report'
      //     // url: '/pages/person/person'
      //     url: '/pages/shop/shop'
      //   })
      //   return
      // }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    console.log("index/index onReady")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    toggleCustomTabBar.toggle(this)
    console.log("index/index onShow")
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    console.log("index/index onHide")
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    console.log("index/index onUnload")
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.log("index/index onPullDownRefresh")
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log("index/index onReachBottom")
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    console.log("index/index onShareAppMessage")
  },


  getFilterMenuItems(menuItemDict, userGroup) {
    let filterMenuItems = Object.values(menuItemDict)
      .filter((item) => item.for.indexOf(userGroup) != -1)

    // 动态控制显示隐藏
    let indexItem = wx.getStorageSync('indexItem') || [
      "/pages/index/login",
      "/pages/person/person"
    ]
    filterMenuItems = filterMenuItems
      .filter((item) => {
        let a = indexItem.includes(item.url)
        // console.log("filterMenuItems -> filter", item.url, indexItem)
        return a
      })
    return filterMenuItems
  },


  switchTo(event) {
    let dataset = event.target.dataset
    console.log("dataset", dataset)
    switch (dataset.switchfunc) {
      case 'switchTab':
        wx.switchTab({
          url: dataset.pageurl
        })
        break;
      case 'navigateTo':
        wx.navigateTo({
          url: dataset.pageurl
        })
        break;
      case 'redirectTo':
        wx.redirectTo({
          url: dataset.pageurl
        })
        break;
      case 'function':
        if (dataset.pageurl == 'scanQRCode') {
          scanQRCode(wx)
        }
        break;
      default:
        console.log("切换页面失败", dataset)
        break;
    }
  },

  goCode() {
    wx.switchTab({
      url: '/pages/residents/code'
    })
  },

  toggleDot(event, ownerInstance, iconName = 'code', status = undefined) {
    console.log("menuItemDict[iconName]", menuItemDict[iconName])
    console.log("this.data.userGroup", this.data.userGroup)
    if (typeof (status) === 'undefined') {
      menuItemDict[iconName].addDot = !menuItemDict[iconName].addDot;
    } else {
      menuItemDict[iconName].addDot = !!status;
    }
    this.setData({
      filterMenuItems: this.getFilterMenuItems(menuItemDict, this.data.userGroup)
    })
  },

  magicButton() {
    wx.navigateTo({
      // 扫码进社区
      // url: "/pages/scan/entrance?a=1"
      // 订单确认页
      // url: "/pages/shop/orderDetail?orderId=1748800678865801225"
      // 我的订单
      url: '/pages/shop/myOrder'
    })
  }
})
