import Vue from 'vue';
import { setTitle } from "@/utils/title"
import waitingDelayApi from "@/api/waitingDelay"
import { isSupportHtml5Plus } from '@/utils/platform'
import imgPathUtils from '@/utils/imgPathUtils'
import _ from "lodash"
import store from '@/store'
Vue.mixin({
    data(){
        return {
            alias: process.env.VUE_APP_ALIAS,
            env: process.env.VUE_APP_ENV,
            isSupportHtml5Plus,
            user: store.getters.user
        }
    },
    computed: {
        isApp() {
            return this.$route.query.isApp || isSupportHtml5Plus
        },
        hasBack() {
			return (this.$navigation.getRoutes().length>1) && !_.includes(this.indexPages, this.$route.path)
		},
        hasTop(){
            if(this.$store.getters.noTop){
                return false
            }
            return !(this.$route.meta&&this.$route.meta.hasTop===false)
        },
		hasBot(){
			if(!this.$route.meta){
				return false
			}
			return (this.$route.meta&&this.$route.meta.hasBot);
		},
        offsetTop(){
            return this.hasTop? '12.26667vw' : '0'
        }
    },
    methods:{
        _refreshPageData(instance, params) {
            let children = []
            if(instance) {
                children = instance.$children
                if(instance.$refreshData && _.isFunction(instance.$refreshData)) {
                    instance.$refreshData(params)
                }

                _.each(children, (ins)=>{
                    this._refreshPageData(ins, params)
                })
            }
            
        },
        $emitRefreshPage(key, params){
            let cache = this.$store.state.navigation.componentInstanceCache
            var page 
            for(var k in cache){
                if(k.indexOf(key+'?')===0){
                    page = cache[k]
                    break
                }
            }
            if(page){
                page = _.find(page.componentInstance.$children, (v)=>{
                    return v.$options.name == "AppMain"
                })
                if(page){
                    this._refreshPageData(page, params)
                }
            }
        },
        setBackRefresh(bool, params) {
            let index = (_.isNumber(bool) && (bool < 0))? bool : -1

            let key = this.$navigation.getRoutes().slice(index-1,index)
            let cache = this.$store.state.navigation.componentInstanceCache
            if(bool === true) {
                if(params) {
                    this.$store.commit("navigation/CACHE_PARAMS", {key, params})
                }
                
                const vnode = cache[key]
                vnode && (vnode.componentInstance.BACK_REFRESH = true)
            } else {
                vnode && (vnode.componentInstance.BACK_REFRESH = false)
            }
        },
        goBack(param) {
            let target = ""
            let num = -1

            if(_.isNumber(param) && param<0) {
                num = param 
            } else if(_.isString(param) && _.isNumber(+param) && param<0) {
                num = param
            } else if(_.isString(param) && param){
                target = param
            }
           
            let routes = this.$navigation.getRoutes()
            let idx = 0
            if(target) {
                idx= _.findLastIndex(routes, function(v) {
                    let reg = new RegExp(`^${target}?`)
                    return reg.test(v)
                })
                num = -(routes.length-1 -idx)
            }

            num = Math.max(num, -(routes.length-1))

            this.$router.go(num)
            
        },
        setNavigatorTitle(title){
            setTitle(title)
            store.dispatch("SetPageTitle",title)
        },
        isWeiXin(){
            var ua = window.navigator.userAgent.toLowerCase();
            if(ua.match(/MicroMessenger/i) == 'micromessenger'){
                return true;
            }else{
                return false;
            }
        },
        async findByHosAndDict(dictName){
            var res = await waitingDelayApi.findByHosAndDict({
                hospital: this.$store.getters.orgCode,
                dictName: dictName
            })
            if(res&&res.status == 200 && res.detailModelList && res.detailModelList[0]){
                return res.detailModelList[0].dictValue
            }
            return ""
        },
        setPatiPhoto(url) {
            return imgPathUtils.setPatiPhoto(url)
        },
        setDocPhoto(url) {
            return imgPathUtils.setDocPhoto(url)
        },
        setImgUrl(url, type) {
            return imgPathUtils.setImgUrl(url, type)
        },
        gotoUrl(path, query){
            if(!path){
                this.$toast("功能建设中..")
                return
            }
            this.$router.push({
                path,
                query: query||{}
            })
        },
        $loading(message){
            this.$toast.loading({
                duration: 0,
                forbidClick: true, // 禁用背景点击
                loadingType: 'loading',
                message: message || ""
            })
        },
        formatSex(v){
            return v==1? "男" : v==2? "女" : "未知"
        },
        $formatDatetime(date){
            if(!date){
                return ''
            }
            return this.$moment(date).format('YYYY-MM-DD HH:mm:ss')
        },
        $formatDate(date){
            if(!date){
                return ''
            }
            return this.$moment(date).format('YYYY-MM-DD')
        },
    }
})