// pages/bocaiTV/index.js
const app = getApp();
import { customRequest } from '../../utils/request.js';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    second: 8,
    pause: false,
    SocketTask: null,
    TVType: -1,
    watchWs: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) { },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () { },
  sendWs () {
    let that = this;
    let SocketTask = wx.connectSocket({
      url: `wss://daotian.matripe.com.cn/tv/socket?tv=app_${app.globalData.loginUserInfo.id}`,
      header: {
        "content-type": "application/json",
      },
      success: function (res) { },
      fail: function (err) {
        console.log(err);
      },
    });

    // setTimeout(() => {
    SocketTask.onOpen((res) => {
      console.log(res);
      // that.startHeartBeat(that.data.socketTask[0])
      SocketTask.send({
        //向服务器发送消息
        data: JSON.stringify({ name: "TVPLAY", value: 1 }),
        success: function (res) {
          console.log(res, "发送成功了");
        },
        fail: function (err) {
          console.log(err, "发送失败了");
        },
      });
    });
    SocketTask.onMessage((res) => {
      //监听 WebSocket 接受到服务器的消息事件
      console.log(res);
      let data = JSON.parse(res.data);
      if (data.name == "TVPLAY") {
        if (data.value == 0) {
          that.setData({
            pause: false,
          });
        } else if (data.value == 1) {
          that.setData({
            pause: true,
          });
        }
        this.setData({
          TVType: data.value,
        });
      }
      console.log(data, "服务器的消息");
    });
    that.setData({
      SocketTask,
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    let that = this;
    that.sendWs();

    wx.onSocketClose(function () {
      console.log("WebSocket连接已关闭");
      if (that.data.watchWs) {
        that.sendWs();
      }
    });
    // }, 0);

  },
  control (e) {
    let that = this;
    let type = e.currentTarget.dataset.type;
    console.log(e);
    if (this.data.TVType == -1) {
      wx.showToast({
        title: "电视未打开,请打开后并登录后重试",
        icon: "none",
      });
      return;
    }
    customRequest("/yishoudan/agency/config/tv/ctrl?cmd=" + type, { header: 'headers', method: 'GET', data: {} }).then((res) => {
      if (res.data.status == 200) {
        if (type == 1) {
          that.setData({
            pause: false,
          });
        } else if (type == 2) {
          that.setData({
            pause: true,
          });
        } else {
          that.setData({
            pause: false,
          })
        }
      }
    })
    // wx.request({
    //   url: app.globalData.ip + "/yishoudan/agency/config/tv/ctrl?cmd=" + type, // 分类列表获取接口
    //   header: app.globalData.headers,
    //   method: "GET",
    //   success (res) {
    //     console.log(res);
    //     if (res.data.status == 200) {
    //       if (type == 1) {
    //         that.setData({
    //           pause: false,
    //         });
    //       } else if (type == 2) {
    //         that.setData({
    //           pause: true,
    //         });
    //       } else {
    //         that.setData({
    //           pause: false,
    //         })
    //       }
    //     }
    //   },
    // });
    wx.vibrateShort({
      type: "heavy",
      success: function (e) {
        console.log(e);
      },
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {
    console.log(this.data.SocketTask);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {
    console.log("onUnload");
    this.setData({
      watchWs: false,
    });
    this.data.SocketTask.close({
      success: function (res) {
        console.log(res, "离开页面关闭socket");
      },
      fail: function (err) {
        console.log(err, "关闭socket失败了");
      },
    });
  },

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

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

  /**
   * 用户点击右上角分享
   */
onShareAppMessage() {
  return app.sharePageImage()
},
  // radioChange(e) {
  // 	console.log(e);
  // },
  // changeTime(e) {
  // 	if (app.isNotEmptyCheck(e.target.dataset.type)) {
  // 		let type = e.target.dataset.type;
  // 		if (type == "add") {
  // 			this.data.second = this.data.second + 1;
  // 		} else {
  // 			this.data.second = this.data.second - 1;
  // 		}
  //     if(this.data.second <= 2){
  //       wx.showToast({
  // 				title: "间隔时间不能少于2秒",
  // 				icon: "none",
  // 				duration: 2000,
  // 			});
  // 			this.data.second = 2;

  //     }
  // 		this.setData({
  // 			second: this.data.second,
  // 		});
  // 	}
  // },
});
