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

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: [],
  },

  onLoad(options) {
    var that = this;
    that.data.clientType = options.clientType;
    that.data.deviceMac = options.deviceMac;
    that.init();
  },

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

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

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

  actionWakeDetail(e) {
    var that = this;
    that.setData({
      actionWakeIndex: e.currentTarget.dataset.index,
    })
  },

  setWake() {
    var that = this;
    var index = that.data.actionWakeIndex;
    if (index === null) {
      wx.showToast({
        title: '请选择唤醒音',
        icon: "none",
        duration: 1500
      })
      return
    };

    if (that.data.deviceWake.length > 0 && (that.data.deviceWake[0].audioId === that.data.detailList[index].audioId)) {
      wx.showToast({
        title: '请不要重复设置哦！',
        icon: "none",
        duration: 1500
      });
      return;
    }

    let repData = {}
    if (that.data.detailList[index].channelId) {
      repData = {
        clientType: that.data.clientType,
        deviceMac: that.data.deviceMac,
        type: 1,
        audioId: that.data.detailList[index].audioId,
        audioType: that.data.detailList[index].audioType,
        channelId: that.data.detailList[index].channelId,
      }
    } else {
      repData = {
        wakeAudioId: that.data.detailList[index].wakeAudioId,
        clientType: that.data.clientType,
        deviceMac: that.data.deviceMac,
        type: 0,
      }
    }

    deviceWakeadd(repData).then(() => {
      that.setData({
        actionWakeIndex: null,
        isShowWakeDetail: false,
        actionWakeDetail: null,
        actionWakeIndex: PreselectionIndex
      });
      // 刷新页面
      that.init();
      getCurrentPages().map((v) => {
        if (v.route === "pages/deviceWake/deviceWake") {
          v.setdeviceWakedetail();
        };
      })
    });
  },

  clone() {
    var that = this;
    that.setData({
      isShowWakeDetail: false,
      actionWakeIndex: null
    })
  },

  actionWake(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    if (that.data.list[index].channelId) {
      // 选择1-12频道
      pageByDevice({
        channelId: that.data.list[index].channelId,
        pageNum: 1,
        pageSize: 300
      }).then((res) => {
        if (that.data.deviceWake.length > 0) {
          res.records.map((v, index) => {
            if (v.audioId === that.data.deviceWake[0].audioId) {
              that.setData({
                actionWakeIndex: index
              })
            }
          });
        };
        that.setData({
          isShowWakeDetail: true,
          detailList: res.records
        })
      })
    } else {
      // 官方频道
      radioList({
        wakeId: that.data.list[index].wakeId,
        pageNum: 1,
        pageSize: 300
      }).then((res) => {
        if (that.data.deviceWake.length > 0) {
          res.records.map((v, index) => {
            if (v.audioId === that.data.deviceWake[0].audioId) {
              that.setData({
                actionWakeIndex: index
              })
            }
          });
        };
        that.setData({
          isShowWakeDetail: true,
          detailList: res.records
        })
      })
    };
    PreselectionIndex = e.currentTarget.dataset.index;
  },

})