import Vue from 'vue'
import {mapState, mapGetters } from "vuex"
import { formatTime,AppMsg } from "@/utils"
import { GoodsType } from "@/enums"
import Clipboard from 'clipboard'

Vue.mixin({
    computed: {
        ...mapState(["appPlatform"]),
        ...mapGetters(['hasLogin'])
    },
    methods: {
        ...AppMsg,
        openUrl(url) {
            switch (this.appPlatform) {
                case "mall-ym":
                  this.appOpenNewPage("web_title", "", url);
                  break;
                default:
                  window.location.href = url;
                  break;
              }
          },
          openBanner(item) {
            if (item.bannerType == 1) {
              this.goGoods(item.number, item.goodsType);
            } else {
              this.openUrl(item.url)
            }
          },
        goBack() {
            // if (this.$route.name == 'search-result') {
            //     this.$router.go(this.$store.state.searchFlag * -1)
            //     this.$store.commit("setSearchFlag", 0)
            //     return
            // }
            return this.$router.back()
        },
        goHome() {
            this.$router.push({ name: "home" })
        },
        goGoods(goodsId, type = GoodsType.NORMAL) {
            let params = { goodsId }
            let query = { type }
            this.$router.push({ name: "goods", params, query })
        },
        goShop(merchantId) {
            let params = { merchantId }
            this.$router.push({ name: "shop", params })
        },


        
        checkLogin(source) {
         if (!this.hasLogin) {
                this.$confirm({
                    content: "是否前往登录？",
                    confirm: () => {
                        this.$router.push({ name: "login",query:{source:source} });
                },
            });
        }
        return this.hasLogin;
        },
        handleCopy(val) {
            let c = new Clipboard('#app', {
                text: () => { return val }
            })
            c.on('success', e => {
                this.$toast("复制成功~")
                c.destroy()
            })
            c.on('error', e => {
                this.$toast("复制失败~")
                c.destroy()
            })
        },
    },
    filters: {
        timeFilter(val) {
            if (!val) {
                return '--'
            }
            return formatTime(val)
        },
        floatFilter(val) {
            if (!val) {
                val = 0
            }
            return parseFloat(val).toFixed(2)
        },
        hashFilter(val) {
          if (!val) {
            return "--";
          }
          return val.substr(0, 16) + "...." + val.substr(val.length - 10);
        },
    }
})