const moment=require('../utils/moment-with-locales.min')
moment.locale('zh-cn');

// 引入全局配置的图片资源地址 
import {globalImgUrls} from "../utils/config"
import store from '@/store'
import {defaultTabBarListConfig} from '@/utils/config.js'

// const tabbarPathList=['pages/voice/voice','pages/roadShow/video/list','pages/video/videoList','pages/report/report','pages/question/question']
const tabbarPathList=defaultTabBarListConfig.map(item=>item.pagePath)

module.exports = {
  watch: {
    tabbarList(){
      this.handleSetTabBarItem()
    }
  },
  computed: {
    tabbarList(){
      return store.state.user.tabbarList
    },
    userInfo(){//个人信息
      return this.$store.state.user.userInfo
    }
  },
  filters: {
    /**
     * 活动时间格式化
     * @param {2021-11-12T09:25:01+08:00} start 开始时间
     * @param 2021-11-12T09:25:01+08:00 end 结束时间
     * @returns 2020-06-04 15:30-16:30 星期一
     */
    formatActivityTime(start,end){
      const week=moment(start).format('dddd');
      const day=moment(start).format('YYYY-MM-DD');
      const startTime=moment(start).format('HH:mm');
      const endTime=moment(end).format('HH:mm');
      return `${day} ${startTime}-${endTime} ${week}`
    },
    /**
     * 格式化音频显示时间
     * @param 120  时长整数秒
     * @returns 02:09
     */
    formatVoiceTime(e){
      let minus=parseInt(e/60)
      let sec=parseInt(e%60)
      return `${minus>9?minus:'0'+minus}分${sec>9?sec:'0'+sec}秒`
    },

    /**
     * 报告时间格式化
     */
    formatReportTime(e){
      return moment(e).format('YYYY-MM-DD')
    }

  },
  data() {
    return {
      globalImgUrls:globalImgUrls,// 图片资源
      globalBgMusic:uni.getBackgroundAudioManager(),
      globalRecorder:uni.getRecorderManager(),//录音
    };
  },
  onLoad(options) {
    console.log('mixin onLoad',options);
    const page = this.$mp&&this.$mp.page
    console.log('当前页面路由',page.route);
    uni.getSystemInfo({
      success: function (res) {
        console.log('宽度：',res.windowWidth);
        console.log('设备：',res.platform);
        if(page.route=='pages/login'||page.route=='pages/pc') return
        if (res.windowWidth > 750||['windows','mac'].includes(res.platform)) {
          const params=options
          let paramsStr=`xcxPath=${decodeURIComponent(page.route)}`
          for(const key in params){
            paramsStr=`${paramsStr}&${key}=${params[key]}`
          }
          console.log('进入pc');
          uni.reLaunch({
            url: `/pages/pc?${paramsStr}`,
          });
        }
      },
      fail:function(res){
      }
    })
  },
  onShow(){
    this.handleActivityListPageRefresh()

    const page = this.$mp&&this.$mp.page;
    if(page&&tabbarPathList.includes(page.route)){
      this.$store.dispatch('getTabBar')
    }

    this.handleSetTabBarItem()
  },
  methods: {
    // 设置tabbar的选中态
    handleSetTabBarItem(){
      const page = this.$mp&&this.$mp.page;
      const tabbarList=this.$store.state.user.tabbarList
      if(!page||tabbarList.length==0) return
      
      let selected=page.route
      // 如果用户的tabbar 改变了 并且他点击的tabbar选项正好不存在了 则重定向到第一个tabbar
      let temarr=tabbarList.map(item=>item.pagePath)
      if(tabbarPathList.includes(selected)&&!temarr.includes(selected)){
        selected=tabbarList[0].pagePath
        uni.switchTab({ url: `/${selected}` })
      }

      if(tabbarPathList.includes(page.route)){
        if (page&&typeof page.getTabBar === "function" && page.getTabBar()) {
          page.getTabBar().setData({
            selected: selected,
            list:tabbarList
          });
        }
      }
    },

    // 控制是否刷新活动列表页
    handleActivityListPageRefresh(){
      const page = this.$mp&&this.$mp.page;
      if(page&&page.route&&page.route=='pages/activity/activity') return
      
      if(page&&page.route&&tabbarPathList.includes(page.route)){
        this.$store.commit('setActivityListPageRefreshStatus', true)
      }else{
        this.$store.commit('setActivityListPageRefreshStatus', false)
      }
    },

    //全局检测用户是否绑定过方法（用户点击前的判断或者其他时候的判断）
    checkUserIsBind(){
      return new Promise((resovle,reject)=>{
        if(store.state.user.userInfo.is_bind===0){
          wx.showModal({
            title: '温馨提示',
            content: '为了优化您的用户体验，\n 请登录后查看更多信息！',
            confirmText:'去登录',
            cancelColor:'#666',
            confirmColor:'#E6B77D',
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定');
                uni.reLaunch({
                  url:'/pages/login'
                })
              } else if (res.cancel) {
                console.log('用户点击取消');
              }
            }
          });
          reject()
        }else{
          resovle('不用登录')
        }
      })
    }

  },
};
