<template>
  <view class="share-store-root" v-if="storeMaps.length != 0">
    <view
      class="store-vender"
      v-for="item in storeMaps"
      :key="item.vendorStoreID"
      @tap="shareStoreFn(item.vendorID, item.vendorStoreID, item.vendorOrgCode)"
    >
      <view class="left">
        <text :class="`icon icon-${item.vendorID}`"></text>
        <view class="vendor-name">{{
          vendorName[item.vendorID] ? vendorName[item.vendorID] : '未知平台'
        }}</view>
      </view>
      <view class="right">
        <jx-icon icon="erweima" :size="60" color="rgb(71, 71, 71)" />
        <text class="code-text">店铺码</text>
      </view>
    </view>
  </view>
  <jx-empty title="您还没有开通任何店铺哟" v-else />

  <jx-popup ref="share" type="center">
    <view class="share-root">
      <!-- 分享界面 -->
      <view class="qrCode">
        <view class="title">{{ getStorage('storeName') }}</view>
        <view class="canvas">
          <canvas
            canvas-id="shareCanvas"
            style="width: 120px; height: 120px; overflow: hidden"
          ></canvas>
        </view>
      </view>

      <!-- 关闭分享 -->
      <view @tap="share.close()" class="closeQrCode">关闭</view>
    </view>
  </jx-popup>
</template>

<script lang="ts" setup >
import merchant from "@/api/https/merchant"
import configCms from '@/utils/configCms'
import { getStorage } from '@/utils/storage'
import { onLoad } from '@dcloudio/uni-app'
import { computed, onBeforeUnmount, ref } from 'vue'
import drawQrcode from '@/utils/weapp.qrcode.esm.js'
import { clearList } from '@/utils/tools'

onLoad(async () => {
  await getStoreInfo()
})

/**
 * 获取店铺数据
 */
const storeMaps = ref<Array<AnyObject>>([])
async function getStoreInfo() {
  let data = {
    storeID: getStorage('storeID'),
  }
  let storeInfoRes = await merchant.get_stores(data)
  storeMaps.value = storeInfoRes.data.stores[0].StoreMaps
}

/**
 * 平台名字
 */
const vendorName = computed(() => {
  let storeInfo:AnyObject = configCms.serveInfo
  return storeInfo.vendorName
})

/*************************************************
 * 生成二维
 */
const share = ref<any>(null)
const shareText = ref<string>('')
async function shareStoreFn(
  vendorID: number,
  vendorStoreID: number,
  vendorOrgCode: number
) {
  uni.showLoading({ title: '请稍后' })
  let path: string = ''
  if (vendorID == 9) {
    // 京西商城
    let data = {
      scene: `storeID=${getStorage('storeID')}`+ `-type=${getStorage('terrace') == 'jxcs' ? 'c4' : getStorage('terrace') == 'jxgy' ? 'gy' : 'gm' }`,
    }
    let res = await merchant.get_weixin_unlimited(data)
    if (res.code == 0) {
      path = res.data.wxcode
      shareText.value = '微信扫码进店/'
      share.value.open()
      getQrCode(path)
    } else {
      uni.jxAlert({
        title: '提示',
        content: '获取店铺二维码失败',
      })
    }
    uni.hideLoading()
  } else if (vendorID == 0) {
    // 京东
    shareText.value = '京东/微信/支付宝扫码进店/'
    share.value.open()
    path = `https://daojia.jd.com/activity/storeShare/index.html?channel=o2ostore&storeId=${vendorStoreID}&orgCode=${vendorOrgCode}`
    getQrCode(path)
  } else if (vendorID == 1) {
    // 美团
    shareText.value = '美团/微信/支付宝扫码进店/'
    share.value.open()
    path = `https://h5.waimai.meituan.com/waimai/mindex/menu?mtShopId=${vendorStoreID}`
    getQrCode(path)
  } else if (vendorID == 3) {
    // 饿了么
    shareText.value = '淘宝闪购/微信/支付宝扫码进店/'
    share.value.open()
    path = `https://h5.ele.me/newretail/p/shop/?&id=${vendorStoreID}`
    getQrCode(path)
  } else if (vendorID == 5) {
    // 京东商城
    shareText.value = '京东/微信/支付宝扫码进店/'
    share.value.open()
    path = `https://mini-app-static.jd.com/apps/mpshare/index.html?category=jump&des=jdmp&vapptype=1&appId=10569615&param={"storeId":"${vendorStoreID}","source":"qrcode","skuId":""}`
    getQrCode(path)
  } else {
    uni.hideLoading()
    uni.jxAlert({
      title: '提示',
      content: '该品牌暂不支持扫码进店',
    })
  }
}

function getQrCode(path: string) {
  setTimeout(() => {
    return new Promise((resolve, reject) => {
      const ctx = uni.createCanvasContext('shareCanvas')
      ctx.clearRect(0, 0, 120, 120)
      drawQrcode({
        canvasId: 'shareCanvas',
        text: String(path),
        width: 120,
        height: 120,
        callback() {
          uni.hideLoading()
        },
      })
    })
  }, 100)
}

/*************************************************
 * 收尾工作
 */
onBeforeUnmount(() => {
  clearList()
})
</script>

<style lang="scss" scoped>
@import './shareStore.scss';
</style>