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", "currentTime"]),
        ...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,item.salesType);
            } 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,salesType) {
            let params = { goodsId }
            let query = { type }
            if(salesType===4){
                this.$router.push({ name: "blind-box", params, query })
            }else{
                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()
            })
        },
        genEnumMap(status, statusEnum) {
            let map = {};
            Object.keys(statusEnum).forEach((key) => {
              map[key] = status == statusEnum[key];
            });
            return map;
          },
        getVideoBase64(url,width=400,height=240) {
            return new Promise(function (resolve, reject) {
                let dataURL=""
                // 在缓存中创建video标签
                var video = document.createElement("video")
                video.setAttribute('crossOrigin', 'anonymous');//处理跨域
                video.setAttribute('useCORS', true);//处理跨域
                video.setAttribute('src', url);
                  // 再添加一个静音的属性，否则自动播放会有声音
                  video.setAttribute('muted', 'muted')
                // 通过setAttribute给video dom元素添加自动播放的属性，因为视频播放才        
                video.setAttribute('autoplay', 'autoplay')
                video.setAttribute('width', width);
                video.setAttribute('height', height);
                video.setAttribute('controls','controls')
                // document.body.appendChild(video)
                // // video注册canplay自动播放事件
                video.addEventListener('loadeddata', function() {
                    let canvas = document.createElement("canvas"),
                        width = video.width, //canvas的尺寸和图片一样
                        height = video.height;
                    canvas.width = width;
                    canvas.height = height;
                    // document.body.appendChild(canvas)
                    canvas.getContext("2d").drawImage(video, 0, 0, width, height); //绘制canvas
                    dataURL = canvas.toDataURL('image/jpeg'); //转换为base64
                    // console.log(dataURL)
                    video.remove()
                    canvas.remove()
                    resolve(dataURL); 
                });
            })
          
            
        }
    },
    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);
        },
    }
})