// pages/home/homefree.js
const app = getApp(); //获取全局对象
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: "../../images/yijianqiujiuguan.png",

    toptabel: 'Welcome Home!',
    userName: '',
    userPhone: '',
    province: '',
    addrDetail: '',
    unitName: '',
    items: [],

    userid: '',
    code: '1',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.refreshweb2()

  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.refreshweb2();
  },

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

  },

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

  },

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

  },

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

  },

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

  },
  onPullDownRefresh: function () {
    this.refreshweb2();
    wx.stopPullDownRefresh();
  },
  itemClick: function (e) {
    var that = this;
    //跳转到设备详情页面
    wx.redirectTo({
      url: '../ranqidetails/ranqidetails?imei=' + e.target.id,
    })
  },
  refreshweb2: function () {
    wx.request({
      url: 'https://api.mpecloud.cn/system/user/getInfo',
      header: {
        'content-type': 'application/json',
        'Authorization': 'Bearer ' + app.globalData.token + ''
      },
      success: res => {
        if (res.data.msg == '操作成功') {
          console.log(res.data.user)
          //账号信息
          this.setData({
            userName: res.data.user['nickName'],
            userPhone: res.data.user['phonenumber'],
            unitName: res.data.user['remark']
          })
        }

        //获取设备列表
        wx.request({
          url: 'https://api.mpecloud.cn/iotsystem/client/allData',
          header: {
            'content-type': 'application/json',
            'Authorization': 'Bearer ' + app.globalData.token + ''
          },
          success: res => {
            if (res.data.msg == '操作成功') {
              console.log(res.data.data)
              //设置设备数据
              var devices = new Array();
              devices = res.data.data.devices;
              var products = new Array();
              products = res.data.data.products;

              var rrr = new Array(); //创建数据
              rrr = devices;
              for (var i = 0; i < rrr.length; i++) {
                rrr[i]['attr3'] = 'white'
                rrr[i]['attr4'] = '400'
                rrr[i]['attr5'] = '1em'
                rrr[i]['nodeId'] = rrr[i]['imei'];
                //地址
                rrr[i]['installLocal'] = rrr[i]['city'] + rrr[i]['area'] + rrr[i]['addr'];
                //设备状态
                if (rrr[i]['deviceStatus'] == 0) {
                  rrr[i]['mute'] = '创建成功';
                } else if (rrr[i]['deviceStatus'] == 1) {
                  rrr[i]['mute'] = '注册失败';
                } else if (rrr[i]['deviceStatus'] == 2) {
                  rrr[i]['mute'] = '注册成功';
                } else if (rrr[i]['deviceStatus'] == 3) {
                  rrr[i]['mute'] = '已激活';
                }
                //网络状态
                if (rrr[i]['isOnline'] == 0) {
                  rrr[i]['status'] = '离线';
                } else if (rrr[i]['isOnline'] == 1) {
                  rrr[i]['status'] = '在线';
                }
                for (var j = 0; j < products.length; j++) {
                  if (rrr[i]['productId'] == products[j]['productId']) {
                    rrr[i]['manufacturerName'] = products[j]['productName'];
                  }
                }
              }
              this.setData({
                items: rrr,
              })
            }
          },
          fail: function () {
            wx.showToast({
              title: '设备数据获取错误！',
              icon: 'none',
              duration: 2000
            })
          }
        })

      },
      fail: function () {
        wx.showToast({
          title: '用户数据获取错误！',
          icon: 'none',
          duration: 2000
        })
      }
    })
  }
})