// pages/config/config.js
const app=getApp()
const WXAPI = require('../../utils/request')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio: '',
    list:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {    
    var userinfo = wx.getStorageSync('userInfo')
    var tenantId = userinfo.tenantId.split(',')
    var arr = []
    this.setData({
      radio:wx.getStorageSync('tenantId')
    })
    WXAPI.getConfig('').then(res => {
      if (res.success) {
        if(userinfo.type===0){
          arr=res.data.list
        }else{
          tenantId.map(lt => {
            var obj = res.data.list.filter(item => {return item.value==lt})[0]
            if(obj){
              arr.push(obj)
            }
          })
        }
        this.setData({
          list:arr
        })
        // localStorage.setItem('selectoption',JSON.stringify(arr))
      } else {
        this.$message.error("获取失败")
      }
    })
  },
  onChange(event) {
    var that=this
    wx.showModal({
      content: '是否切换数据源',
      success (res) {
        if (res.confirm) {
          that.setData({
            radio: event.detail,
          });
          wx.setStorageSync('tenantId', event.detail)
          WXAPI.setTenantId(event.detail)
          wx.reLaunch({
            url:"/pages/index/index"
          })
        } else if (res.cancel) {

        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})