import {
  ApiModel
} from "../../utils/api";
const _Http = new ApiModel;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataTypes: 0, //0-全部数据 1-单独数据
    tabsList: ["私域直播", "展会直播"], //tabs列表
    tabsIndex: 0, //tabs下标
    accountStatus: null, //直播账号状态 1-账号正常 2-账号审核中 3-没有账号
    liveDataCount: {}, //实时数据统计
    liveDataCountForSession: {}, //单场次数据统计
    liveSessionList: [], //直播场次列表
    liveUserList: [], //直播用户观看记录
    pageNumber: 1, //当前页码
    pageTotal: 1, //列表总页数
    userPageNumber: 1, //用户当前页码
    userPageTotal: 1, //用户总列表
    optionRow: -1, //列表选中项
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取直播账号
    this.getLiveInfo()
  },
  /* 获取直播账号详情 */
  getLiveInfo() {
    const method = (this.data.tabsIndex == 0) ? 'getSYLiveInfo' : 'getLiveInfo';
    _Http.basic({
      "accesstoken": wx.getStorageSync('userData').token,
      "classname": "customer.live.live",
      "method": method,
      "content": {}
    }).then(res => {
      this.setData({
        accountStatus: res.code,
        accountMsg: res.data[0]
      })
      /* 直播数据统计 */
      this.getLiveDataCount()
      /* 直播场次列表查询 */
      this.getLiveSessionList()
    })
  },
  /* 直播数据统计 */
  getLiveDataCount() {
    //判断私域还是展会
    const method = (this.data.tabsIndex == 0) ? 'getSYLiveDataCount' : 'getLiveDataCount'
    _Http.basic({
      "accesstoken": wx.getStorageSync('userData').token,
      "classname": "customer.live.live",
      "method": method,
      "content": {
        "channelid": this.data.accountMsg.channelid
      }
    }).then(res => {
      if (res.msg != '成功') return wx.showToast({
        title: res.data,
        icon: "none"
      });
      this.setData({
        liveDataCount: res.data
      })
    })
  },
  /* 直播场次列表查询 */
  getLiveSessionList() {
    const method = (this.data.tabsIndex == 0) ? "getSYLiveSessionList" : "getLiveSessionList"
    _Http.basic({
      "accesstoken": wx.getStorageSync('userData').token,
      "classname": "customer.live.live",
      "method": method,
      "content": {
        "getdatafromdbanyway": true,
        "pageNumber": this.data.pageNumber,
        "pageSize": 10,
        "channelid": this.data.accountMsg.channelid
      }
    }).then(res => {
      console.log(res)
      if (res.msg != '成功') return wx.showToast({
        title: res.data,
        icon: "none"
      });
      for (let i = 0; i < res.data.length; i++) {
        let checkdate = res.data[i].starttime;
        res.data[i].starttime = checkdate.slice(0, checkdate.lastIndexOf('.'));
      }
      this.setData({
        liveSessionList: res.data,
        pageTotal: res.pageTotal
      })
    })
  },

  /* 查看场次详情 */
  viewDetails(e) {
    const {
      index
    } = e.currentTarget.dataset;
    const sessionid = this.data.liveSessionList[index].sessionid;
    this.setData({
      showStartTime: this.data.liveSessionList[index].starttime,
      sessionid
    })
    /* 场次详情统计 */
    _Http.basic({
      "accesstoken": wx.getStorageSync('userData').token,
      "classname": "customer.live.live",
      "method": "getSYLiveDataCountForSession",
      "content": {
        "sessionid": sessionid
      }
    }).then(res => {
      console.log(res)
      if (res.msg != '成功') wx.showToast({
        title: '数据统计获取失败，请稍后再试',
        icon: "none"
      })
      this.setData({
        liveDataCountForSession: res.data
      })
    });
    this.LiveUserList();
    this.setData({
      dataTypes: 1
    })
  },

  /* 观看用户列表 */
  LiveUserList() {
    /* 用户观看列表 */
    const method = (this.data.tabsIndex == 0) ? "getSYLiveUserList" : "getLiveUserList";
    _Http.basic({
      "accesstoken": wx.getStorageSync('userData').token,
      "classname": "customer.live.live",
      "method": method,
      "content": {
        "getdatafromdbanyway": true,
        "pageNumber": this.data.userPageNumber,
        "pageSize": 10,
        "sessionid": this.data.sessionid,
        "channelid": this.data.accountMsg.channelid
      }
    }).then(res => {
      console.log(res)
      if (res.msg != '成功') wx.showToast({
        title: '观看列表获取失败，请稍后再试',
        icon: "none"
      })
      for (let i = 0; i < res.data.length; i++) {
        const index = res.data[i].datetime.lastIndexOf('.');
        res.data[i].datetime = res.data[i].datetime.slice(0, index)
      }
      this.setData({
        userPageTotal: res.pageTotal,
        liveUserList: res.data
      })
    });
  },
  /* 返回场次列表 */
  returnLiveData() {
    this.setData({
      dataTypes: 0,
      userPageNumber: 1,
      userPageTotal: 1
    })
  },
  /* 场次列表选中 */
  optionItemIndex(e) {
    const {
      index
    } = e.currentTarget.dataset;
    if (index == this.data.optionRow) return this.setData({
      optionRow: -1
    });
    this.setData({
      optionRow: index
    })
  },
  /* 下一页 */
  buttonRightClick() {
    if (this.data.dataTypes == 0) {
      if (this.data.pageNumber == this.data.pageTotal) return wx.showToast({
        title: '已经到达最后一页',
        icon: "none"
      });
      this.setData({
        pageNumber: this.data.pageNumber + 1
      })
      this.getSYLiveSessionList()
    } else {
      if (this.data.userPageNumber == this.data.userPageTotal) return wx.showToast({
        title: '已经到达最后一页',
        icon: "none"
      });
      this.setData({
        userPageNumber: this.data.userPageNumber + 1
      })
      this.LiveUserList()
    }
  },
  /* 上一页 */
  buttonLifeClick() {
    if (this.data.dataTypes == 0) {
      if (this.data.pageNumber == 1) return wx.showToast({
        title: '已经在第一页了哦',
        icon: "none"
      });
      this.setData({
        pageNumber: this.data.pageNumber - 1
      })
      this.getSYLiveSessionList()
    } else {
      if (this.data.userPageNumber == 1) return wx.showToast({
        title: '已经在第一页了哦',
        icon: "none"
      });
      this.setData({
        userPageNumber: this.data.userPageNumber - 1
      })
      this.LiveUserList()
    }
  },
  /* tabs回调 */
  setIndex({
    detail
  }) {
    this.setData({
      tabsIndex: detail,
      pageNumber: 1,
      pageTotal: 1,
      userPageNumber: 1,
      userPageTotal: 1
    })
    this.getLiveInfo();
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})