// pages/wakeList/wakeList.js
const { deviceWakeList, deviceWakeadd, deviceWakedetail, listByDevice, pageByDevice, radioList }=require('./../../utils/api.js');
let PreselectionIndex = null; // 记录选中的频道
let _this = null;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBarHeight: app.globalData.navBarHeight,
    MenuButtonTop: app.globalData.MenuButtonTop,
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '设置唤醒音', //导航栏 中间的标题
    },
    list: [],
    actionIndex: null,
    clientType: "",
    deviceMac: "",
    detailList: [],
    isShowWakeDetail: false,
    actionWakeIndex: null,
    deviceWake: [],
  },
  actionWakeDetail(e){
    this.setData({
      actionWakeIndex: e.currentTarget.dataset.index,
    })
  },
  setWake() {
    const index = this.data.actionWakeIndex;
    if(_this.data.deviceWake.length > 0 && (_this.data.deviceWake[0].audioId === this.data.detailList[index].audioId)) {
      wx.showToast({
        title: '请不要重复设置哦！',
        icon: "none",
        duration: 1500
      })
      return;
    }
    if(index === null) {
      wx.showToast({
        title: '请选择唤醒音',
        icon: "none",
        duration: 1500
      })
      return
    };
    let repData = {}
    if(this.data.detailList[index].channelId) {
      repData={
        clientType: this.data.clientType,
        deviceMac: this.data.deviceMac,
        type: 1,
        audioId: this.data.detailList[index].audioId,
        audioType: this.data.detailList[index].audioType,
        channelId: this.data.detailList[index].channelId,
      }
    } else {
      repData = {
        wakeAudioId: this.data.detailList[index].wakeAudioId,
        clientType: this.data.clientType,
        deviceMac: this.data.deviceMac,
        type: 0,
      }
    }
    deviceWakeadd(repData).then(()=> {
      _this.setData({
        actionWakeIndex: null,
        isShowWakeDetail: false,
        actionWakeDetail: null,
        actionWakeIndex: PreselectionIndex
      });
      // 刷新页面
      _this.init();
      getCurrentPages().map((v)=> {
        if(v.route === "pages/deviceWake/deviceWake") {
          v.setdeviceWakedetail();
        };
      })
    })
  },
  clone() {
    this.setData({
      isShowWakeDetail: false,
      actionWakeIndex: null
    })
  },
  actionWake(e) {
    
    const index = e.currentTarget.dataset.index;
    if(this.data.list[index].channelId) {
      // 选择1-12频道
      pageByDevice({
        channelId: this.data.list[index].channelId,
        pageNum: 1,
        pageSize: 300
      }).then((res)=> {
        if(_this.data.deviceWake.length > 0) {
          res.records.map((v, index)=> {
            if(v.audioId === _this.data.deviceWake[0].audioId) {
              _this.setData({
                actionWakeIndex: index
              })
            }
          });
        };
        _this.setData({
          isShowWakeDetail: true,
          detailList: res.records
        })
      })
    } else {
      // 官方频道
      radioList({
        wakeId: this.data.list[index].wakeId,
        pageNum: 1,
        pageSize: 300
      }).then((res)=> {
        if(_this.data.deviceWake.length > 0) {
          res.records.map((v, index)=> {
            if(v.audioId === _this.data.deviceWake[0].audioId) {
              _this.setData({
                actionWakeIndex: index
              })
            }
          });
        };
        _this.setData({
          isShowWakeDetail: true,
          detailList: res.records
        })
      })
    };
    PreselectionIndex = e.currentTarget.dataset.index;
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    _this = this;

    this.data.clientType = options.clientType;
    this.data.deviceMac = options.deviceMac;
    
    this.init();
  },

  init() {
    deviceWakeList({
      clientType: this.data.clientType,
      deviceMac: this.data.deviceMac,
    }).then((res)=> {

      listByDevice({clientType: this.data.clientType}).then((item)=> {

        const listData = [...res, ...item];
        _this.setData({
          list: listData
        })
        deviceWakedetail({
          clientType: this.data.clientType,
          deviceMac: this.data.deviceMac,
        }).then((data)=> {
          if(data && data.length === 0) {
            return;
          };
          _this.setData({
            deviceWake: data,
          });
          listData.map((v, index)=> {
            if(v.wakeId === data[0].wakeId || v.channelId === data[0].channelId) {
              this.setData({
                actionIndex: index
              })
            }
          })
        })
      })
    })

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})