<template>
  <div class="content">
    <p class="font-w">使用简介</p>
    <p class="text-i">网页内快速接入客服，让网页拥有客服窗口，请把一下代码复制到网页最底部。</p>
    <Divider />
    <p class="typetitle">获取代码</p>

    <div class="fenlei">
      <div class="code-content-wrap">
        <textarea id="NormalCodeTextarea1" class="code" rows="22">
<script>
  (function() {
  _s = document.createElement('script');
  _s.src="{{ siteUrl }}/customerServer.js"
  _s.onload = function(){
  var option = {
        "authInit":true,
        openUrl: '{{ siteUrl }}',
        token: '{{ uniqueToken }}',
        isShowTip: true, // 初始化成功后，界面右下角会自动创建 “联系客服按钮”， 如无需默认展示，则填写false即可,默认为true
        mobileIcon: '', //  手机端悬浮客服图片
        pcIcon: '', // pc端悬浮客服图片
        windowStyle:'',//默认空 右下角小弹窗， center 普通中间弹窗样式
      };
      var canCustomerServer = new initCustomerServer(option);
      canCustomerServer.init();
  }
  document.head.appendChild(_s)
  })();
</script>
            </textarea
        >
        <div class="other-wrap">
          <a class="btn btn-blue btn-large mr10" href="javascript:void(0);" @click="jiazai">点击体验</a>
          <a class="btn btn-blue btn-large" href="javascript:void(0);" @click="getCopy('NormalCodeTextarea1')"><span>复制代码</span></a>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { defineProps, defineEmits } from 'vue';
import { getInfo } from '@/api/login';
const props = defineProps<{
  tokeninfo?: any;
  siteUrl?: string;
}>();

const emit = defineEmits<{ (e: 'cgetCopy', id: string): void }>();
const uniqueToken = ref('');
function jiazai() {
  // const token = props.tokeninfo?.token_md5 || '';
  const token = uniqueToken.value || '';
  window.open(`${location.origin}/chat/index?token=${token}&noCanClose=1`);
}

async function fetchUniqueToken() {
  const res = await getInfo();
  console.log(res.data.tokenInfo);
  if (res.data && res.data.tokenInfo) {
      uniqueToken.value = res.data.tokenInfo.uniqueToken || '';
    }
}

function getCopy(id: string) {
  emit('cgetCopy', id);
}

onMounted(() => {
  fetchUniqueToken();
});
</script>
<style>
.a {
  display: none;
  display: block;
  border-radius: 8px;
  overflow: hidden;
}
</style>
