import routeUtil from '../../utils/route_util'
import route_constant from '../../utils/route_constant.js'
Page({

  data: {
    scopeBluetooth: false,
    userFuzzyLocation: true,
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '配网模式选择', //导航栏 中间的标题
    },
    isNotruter: false,
    model: "",
    connectDevice: {},
    typeList: [{
        "icon": "../../images/device/mode_2.png",
        "text": "WiFi模式",
        "type": 2,
      },
      {
        "icon": "../../images/device/mode_1.png",
        "text": "蓝牙模式",
        "type": 1,
      },
      {
        "icon": "../../images/device/mode_4.png",
        "text": "4G模式",
        "type": 4,
      },
    ],
  },

  getModeName(curItem) {
    // 设备类型：1-蓝牙，2-Wifi，3-传统蓝牙，4-4G
    var str = "";
    if (curItem.type == 4) {
      str = "移动数据模式";
    } else if (curItem.type == 2) {
      str = "WiFi模式";
    } else if (curItem.type == 1) {
      str = "蓝牙模式";
    }
    return str;
  },

  nextTap(e) {
    if (this.nullTips()) {
      return
    }
    let device = this.data.typeList[0];
    let type = device.type ?? 0;
    console.log("选择模式", type)
    ///蓝牙模式
    if (type == 1) {
      // let device = e.currentTarget.dataset.data
      let json = JSON.stringify(this.data.connectDevice)
      routeUtil.jumpParam(route_constant.connectBle, json)
      // wx.navigateTo({
      //   url: './../deviceDetail/detail',
      // });
    }
    ///Wifi模式
    else if (type == 2) {
      wx.navigateTo({
        url: './../deviceConnect0/deviceConnect0',
      });
    } else if (type == 3) {
      // btHelper.stopSearch()
    }
    ///4G模式
    else {

    }
  },

  async selectDeviceModelTap(e) {
    let device = e.currentTarget.dataset.device;
    // console.log(e)
    let type = device.type;
    if (type == 1) {
      // this.getConnectedDevices()
    } else if (type == 2) {

    } else if (type == 3) {
      // btHelper.stopSearch()
    } else {

    }
    var list = []
    list.push(device)
    this.data.typeList.forEach(element => {
      if (element.type != type) {
        list.push(element)
      }
    });
    this.setData({
      typeList: list,
      model: this.getModeName(device)
    })

  },
  nullTips() {
    if (this.data.typeList.length === 0) {
      wx.showToast({
        title: '暂不支持该设备配网',
        icon: 'none',
      })
      return true
    }
    return false
  },
  onLoad: function (options) {
    let device = JSON.parse(options.param) || {}
    console.log("配网配对的设备：", device)
    if (Object.keys(device).length === 0) {
      wx.showToast({
        title: '设备数据为空，返回刷新列表试试看',
      })
      return;
    }
    let _this = this
    let isWifi = device.clientType != "MW-S2";
    let isS2 = device.clientType === "MW-S2";

    let _typeList = device.typeList
      .filter(e => (isWifi && e.type == 2) || isS2)
      .map(e => ({
        icon: "../../images/device/mode_" + e.type + ".png",
        text: _this.getModeName(e),
        type: e.type
      }));

    var list = []

    this.data.typeList.forEach(element => {
      if (_typeList.find(_type => _type.type === element.type)) {
        list.push(element)
      }
    });
    this.setData({
      typeList: list,
      connectDevice: device
    })
    this.nullTips()

  },

})