<template>
  <div class="url-share" v-show="show">
    <div>
      <div class="tips">
        <h4>{{ $t("share.shareLink") }}</h4>
        <i class="iconfont icon-bs_close" @click="onNo"></i>
      </div>
      <div class="url">{{ copyLink }}</div>
      <div class="btns">
        <ui-button class="ccc" @click="onNo">{{ $t("common.cancel") }}</ui-button>
        <ui-button class="primary" :data-clipboard-text="copyLink" ref="copy$">{{ $t("share.copyLink") }}</ui-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed, watch, nextTick } from "vue";
import { useI18n, getLocale } from "@/i18n";
import { useStore } from "vuex";
import { Dialog } from '@/global_components/'

import { getApp } from "@/app";
import browser from '@/utils/browser'
import ClipboardJS from 'clipboard'


const store = useStore();
const { t } = useI18n({ useScope: "global" });

const url = ref(location.href);
const copy$ = ref(null);

const show = computed(() => store.getters["functions/showShareUrl"]);

let share_url = browser.getURLParam("share_url");
if (share_url) {
  share_url = decodeURIComponent(share_url);
} else {
  share_url = location.href.split("#")[0];
}

const copyLink = computed(() => {
  return share_url.replace("&app", "");
});

const onOk = () => {};

const onNo = () => {
  store.commit("functions/setShareUrl", false);
};

onMounted(() => {
  nextTick(()=>{
  new ClipboardJS(copy$.value.$el).on("success",  (e)=> {
    e.clearSelection();
    onNo()
    Dialog.toast({ content: t("toast.copySuccess"), type: "success" });
  });
  })
});
</script>

<style lang="scss" scoped>
.url-share {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 99999;
  > div {
    position: absolute;
    left: 50%;
    top: 50vh;
    transform: translate(-50%,-50%);
    width: 400px;
    height: 230px;
    background: rgba(27,27,28,0.8);
    box-shadow: 0px 0px 10px 0px rgba(0,0,0,0.3);
    border-radius: 4px;
    border: 1px solid #000000;
    backdrop-filter: blur(4px);
    padding: 26px;
    .tips {
      display: flex;
      align-items: center;
      justify-content: space-between;
      h4 {
        margin: 0;
        font-size: 18px;
        font-weight: normal;
      }
      i {
        font-size: 14px;
        cursor: pointer;
        color: #969799;
      }
    }
    .url {
      color: #fff;
      text-align: center;
      padding: 10px 16px;
      width: 100%;
      margin: 40px auto;
      border-radius: 0.07rem;
      line-height: 1.5;
      font-size: 16px;
      background: rgba(26,27,29,0.8);
      overflow: hidden;
      text-overflow: ellipsis;
      white-space:nowrap;
    }
    .btns {
      display: flex;
      justify-content: flex-end;
      button {
        font-size: 14px;
        width: 88px;
        margin-left: 10px;
      }
      .ccc{
        background: #313131!important;
        border-radius: 4px;
        border-color: transparent;
      }
    }
  }
}

@media screen and (max-width: 600px) {
  .url-share {
    >div{
      width: 90%;
    }
  }
}
</style>
