const app = getApp();
const request = require('../../utils/request.js');
const tools = require('../../utils/tools.js');
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
Page({

  data: {
    num: '',
    hardTypeArrAll: [],
    hardTypeArr: [],
    showPicker: false,
    index:''
  },
  onLoad: function (options) {
    this.setData({
      num: options.code
    })
  },
  async bd() {
    console.log(this.data.num)
    let options = {
      url: 'resource/miniapp/user/bindEquip',
      data: {
        devicenum: this.data.num,
        devicetype: this.data.hardTypeArrAll.filter((item,idx) => {
          return idx === this.data.index
        })[0].id,
        openId: wx.getStorageSync('openid'),
        type: app.globalData.type
        // 种类加上
      },
      method: 'post'
    }
    let res = await request.query(options);
    if (res.data.code === 200) {
      wx.showModal({
        title: '提示',
        content: '绑定成功',
        showCancel: false,
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/hardwareDetails/hardwareDetails',
            })
          }
        }
      })
    } else {
      Toast.fail(res.data.msg);
    }
  },
  typeClick() {
    this.setData({
      showPicker: true
    })
  },
  onConfirm(e) {
    console.log(e)
    this.setData({
      showPicker: false,
      type: e.detail.value,
      index: e.detail.index
    })
  },
  onCancel() {
    this.setData({
      showPicker: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  async getType() {
    let options = {
      url: 'resource/miniapp/user/getDeviceType',
      data: {},
      method: 'post'
    }
    let res = await request.query(options);
    if (res.data.code === 200) {
      let arr = [];
      res.data.data.map(item => {
        arr.push(item.name + '-' + item.model+'-'+item.manufactor)
      })
      this.setData({
        hardTypeArrAll: res.data.data,
        hardTypeArr: arr
      })
      console.log(res.data.data)
    } else {
      Toast.fail(res.data.msg);
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (!wx.getStorageSync('userId')) {
      wx.showModal({
        title: '提示',
        content: '请扫描二维码进入小程序',
        showCancel: false,
        success: (res) => {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        }
      })
      return false;
    }
    tools.isLogin().then((res) => {
      this.getType();
    }, (error) => {
      console.log(error);
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})