let _Http = getApp().globalData.http,
  DataCarousel = null;

import {
  initWorkbench,
  isHistory,
  isRemindChangePassword
} from "../../../utils/work/work";

Page({
  data: {
    appid: 'wxc1b6ae925ac1d06a',
    bannerList: [], //banner列表
    gridList: [],
    notice: "",
    msgCount: 1,
    msgList: [], //消息列表
  },
  onLoad(options) {
    this.setData({ //获取胶囊位置信息
      capsule: wx.getMenuButtonBoundingClientRect()
    })
    this.refreshData() //更新权限等信息
    this.getTabBar().unReadMessageCount(); //更新信息数量
    isHistory();
    isRemindChangePassword();
  },
  /* 更新站点信息 */
  refreshData() {
    this.setData({
      user: wx.getStorageSync('userMsg')
    })
    if (wx.getStorageSync('userauth').length != 0) {
      initWorkbench(this)
    } else {
      setTimeout(this.refreshData, 10);
      return;
    }
  },
  /* 获取最新信息 */
  queryMessage(i = 0) {
    _Http.basic({
      "classname": "system.message.Message",
      "method": "queryMessage",
      content: {
        nocache: true,
        pageNumber: 1,
        pageSize: 5,
        pageTotal: 1,
        type: "",
        where: {}
      },
    }, false).then(res => {
      if (res.msg != '成功') return (i <= 5) ? this.queryMessage(i + 1) : wx.showToast({
        title: res.msg,
        icon: "none"
      })
      const authComparison = wx.getStorageSync('authComparison');
      res.data = res.data.map(v => {
        let data = authComparison.find(s => (s.objectname || "") == v.objectname);
        if (data) {
          switch (data.objectname) {
            case "sa_customers":
              data.name = '客户'
              //客户
              break;
            case "sat_orderclue":
              data.name = '线索'
              //线索
              break;
            case "sa_order":
              data.name = '订单'
              break;
          }
          return Object.assign(v, data)
        }
        return v
      })
      this.setData({
        msgList: res.data,
        notice: res.data[0]
      })
      if (this.data.msgList.length > 2) this.startDataCarousel();
    })
  },
  /* 开启消息轮播 */
  startDataCarousel() {
    clearInterval(DataCarousel);
    DataCarousel = setInterval(() => {
      let count = this.data.msgCount < this.data.msgList.length ? this.data.msgCount : 0;
      this.setData({
        msgCount: count + 1,
        notice: this.data.msgList[count]
      })
    }, 5000)
  },
  /* 去消息详情 */
  toMsg(e) {
    const {
      item
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/tabbar/message/details?item=' + JSON.stringify(item)
    })
  },
  /* banner */
  bannerClick(e) {
    const {
      item
    } = e.currentTarget.dataset,
      hyperlink = item.hyperlink.split(":");
    if (hyperlink[0] == 'path') wx.navigateTo({
      url: hyperlink[1]
    })
  },
  onShow() {
    this.getTabBar().init();
    if (this.data.msgList.length > 2) this.startDataCarousel();
    this.queryMessage(0); //更新最新消息
  },
  onHide() {
    clearInterval(DataCarousel);
  },
  /* 设置页面高度 */
  setListHeight() {
    this.selectComponent("#ListBox").setHeight(".grld-title", this);
  }
})