// 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 () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  onPullDownRefresh: function () {
    this.refreshweb2();
    wx.stopPullDownRefresh();
  },
  refreshweb2: function () {
    wx.login({
      //获取code
      success: res=> {
        this.setData({
          code:res.code,
        })
        console.log(this.data.code)
        //获取OpenID
        wx.request({
          url: 'https://www.mpecloud.com:8443/na/iocm/devNotify/v1.1.0/getOpenId/' + this.data.code,
          header: { 'content-type': 'application/json' },
          success: res => {
            console.log(res.data)
            if (res.data.message == 'success') {
              wx.request({
                url: 'https://www.mpecloud.com:8443/na/iocm/devNotify/v1.1.0/detailInfo/' + res.data.openid,
                header: { 'content-type': 'application/json' },
                success: res => {
                  //账号信息
                  this.setData({
                    userName: res.data.user['userName'],
                    userPhone: res.data.user['userPhone'],
                    unitName: res.data.user['unitName'],
                    province: res.data.user['province'] + res.data.user['city'],
                    addrDetail: res.data.user['area'] + res.data.user['addrDetail']
                  })

                  //设置数据加载
                  var rrr = new Array();
                  rrr = res.data.user['deviceList'];//设备信息
                  for (var i = 0; i < rrr.length; i++) {
                    //设置设备网络状态
                    if (rrr[i]['status'] ==0)
                    {
                      rrr[i]['status'] = '基本信息注册中'
                    }
                    else if (rrr[i]['status'] == 1)
                    {
                      rrr[i]['status'] = '产品信息注册中'
                    }
                    else if (rrr[i]['status'] == 2) {
                      rrr[i]['status'] = '注册成功'
                    }
                    else if (rrr[i]['status'] == 3) {
                      rrr[i]['status'] = '在线'
                    }
                    else if (rrr[i]['status'] == 4) {
                      rrr[i]['status'] = '离线'
                    }
                    else if (rrr[i]['status'] == 6) {
                      rrr[i]['status'] = '过期'
                    }

                    //设置设备状态
                    if (rrr[i]['mute'] == 0) {
                      rrr[i]['mute'] = '正常'
                      rrr[i]['attr3'] ='white'
                      rrr[i]['attr4'] ='400'
                      rrr[i]['attr5'] ='1em'
                    }
                    else if (rrr[i]['mute'] == 1) {
                      rrr[i]['mute'] = '故障'
                      rrr[i]['attr3']='red'
                      rrr[i]['attr4'] ='800'
                      rrr[i]['attr5'] ='1.5em'
                    }
                    else if (rrr[i]['mute'] == 2) {
                      rrr[i]['mute'] = '状态未设'
                      rrr[i]['attr3'] = 'white'
                      rrr[i]['attr4'] = '400'
                      rrr[i]['attr5'] = '1em'
                    }

                    //设置设备特殊字段
                    if (rrr[i]['productId']==4) {
                      rrr[i]['attr2'] = '燃气浓度'
                    }
                    else {
                      rrr[i]['attr2'] = '电池电量'
                    }

                    //设置按钮
                    if (rrr[i]['productId'] == 3) {
                      rrr[i]['area']='true'
                    }
                  }

                  this.setData({
                    items: rrr,

                  })
                }, fail: function () {
                  wx.showToast({
                    title: app.globalData.userid + '接口null',
                    icon: 'none',
                    duration: 2000
                  })
                }
              })

            }
            else {
              wx.showToast({
                title: '设备未绑定！',
                icon: 'none',
                duration: 2000
              })
            }
          }, fail: function () {
            wx.showToast({
              title: 'openid获取错误！',
              icon: 'none',
              duration: 2000
            })
          }
        })
      }

    })

  },
  btnclick_xiaoyin: function (e) {
    var that = this;
    wx.request({
      url: 'https://www.mpecloud.com:8443/na/iocm/devNotify/v1.1.0/command/' + e.target.id +'?command=CJ',
      header: { 'content-type': 'application/json' },
      success: res => {
        console.log(res.data.code)
        if (res.data.code == 'Ok') {
        wx.showToast({
          title: '持续静音已成功!',
          icon: 'none',
          duration: 2000
        })
        }
        else
        {
          wx.showToast({
            title: '操作失败!',
            icon: 'none',
            duration: 2000
          })
        }
      }, fail: function () {
        wx.showToast({
          title: '操作失败!',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  btnclick_chefang: function (e) {
    var that = this;
    wx.request({
      url: 'https://www.mpecloud.com:8443/na/iocm/devNotify/v1.1.0/command/' + e.target.id + '?command=CF',
      header: { 'content-type': 'application/json' },
      success: res => {
        if (res.data.code =='Ok')
        {
        wx.showToast({
          title: '撤防已成功!',
          icon: 'none',
          duration: 2000
        })
        }
        else {
          wx.showToast({
            title: '操作失败!',
            icon: 'none',
            duration: 2000
          })
        }
      }, fail: function () {
        wx.showToast({
          title: '操作失败!',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  btnclick_cancelchefang: function (e) {
    var that = this;
    wx.request({
      url: 'https://www.mpecloud.com:8443/na/iocm/devNotify/v1.1.0/command/' + e.target.id + '?command=BF',
      header: { 'content-type': 'application/json' },
      success: res => {
        if (res.data.code == 'Ok') {
        wx.showToast({
          title: '取消撤防已成功!',
          icon: 'none',
          duration: 2000
        })
        }
        else {
          wx.showToast({
            title: '操作失败!',
            icon: 'none',
            duration: 2000
          })
        }
      }, fail: function () {
        wx.showToast({
          title: '操作失败!',
          icon: 'none',
          duration: 2000
        })
      }
    })
  }
})