import eventEmitter from '../../utils/eventEmitter'
import { isPhoneX } from './../../utils/tools'
let app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    inactiveColor: {
      type: String,
      value: '#79868F'
    },
    activeColor: {
      type: String,
      value: '#1FE4DC'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    active: 0,
    tabItems: [
      {
        title: '首页',
        url: '/pages/index/index',
        icon: {
          normal: '/image/4Dage/tab/home.svg',
          active: '/image/4Dage/tab/home-active.svg'
        }
      },
      {
        title: '消息',
        url: '/pages/chat-list/chat-list',
        info: app.globalData.unViewMsg,
        icon: {
          normal: '/image/4Dage/tab/message.svg',
          active: '/image/4Dage/tab/message-active.svg'
        }
      },
      {
        title: '我的',
        url: '/pages/my/my',
        icon: {
          normal: '/image/4Dage/tab/my.svg',
          active: '/image/4Dage/tab/my-active.svg'
        }
      },
    ],
    isPhoneX: false
  },

  attached: function() {
  },
  pageLifetimes: {
    show () {
      const currentRoute = getCurrentPages()[0].route
      const { tabItems } = this.data
      tabItems[1].info = app.globalData.unViewMsg
      tabItems.forEach((item, index) => {
        if (item.url.indexOf(currentRoute) !== -1) {
          this.setData({
            active: index,
            tabItems: tabItems,
          })
        }
      })
      isPhoneX().then(res => {
        this.setData({
          isPhoneX: res
        })
      })
      this.init()
    },
    hide () {
      getApp().getIMHandler().removeOnReceiveMessageListener({listener: this.fn})
    }
  },
  
  /**
   * 组件的方法列表
   */
  methods: {
    onChange(e) {
      const index = e.detail, { tabItems } = this.data
      wx.switchTab({
        url: tabItems[index].url,
      })
    },
    init () {
      this.fn = (msg) => {
        
        const { tabItems } = this.data
        app.globalData.unViewMsg = app.globalData.unViewMsg ? app.globalData.unViewMsg+1 : 1
        tabItems[1].info = app.globalData.unViewMsg
        this.setData({
          tabItems
        })
        app.getNewMessage(msg)
      }
      getApp().getIMHandler().setOnReceiveMessageListener({
        listener: this.fn
    });
    }
  }
})
