<template>
  <van-popup
    class="fbt-van-popup"
    v-model="showFlag"
    :lock-scroll="true"
    :close-on-click-overlay="false">
    <p>打开小程序</p>
    <div class="btns">
      <a
        href="javascript:;"
        @click="funJumpIndex">取消</a>
      <div>
        <a
          href="javascript:;"
          @click="funJumpIndex">允许</a>
        <wx-open-launch-weapp
          ref="launchBtn"
          :username="accountId"
          :path="appPath"
          style="position: absolute;left:0;top:0;z-index:1;width: 100%;height: 100%;"
          @error="errorHandle">
          <script type="text/wxtag-template">
            <style>
              .jump-btn {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;
                line-height: 100%;
                text-align: center;
                font-size: 12px;
              }
            </style>
            <div class="jump-btn">打开小程序</div>
          </script>
        </wx-open-launch-weapp>
      </div>
    </div>
  </van-popup>
</template>

<script>
import { Toast, Popup, Dialog } from 'vant'
import { getJumpConfig } from './api'
import { isSupportJumpWXmini } from '../../../utils'

export default {
  name: 'index',
  components: {
    'van-popup': Popup
  },
  props: {
    redirectUrl: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      showFlag: false,
      accountId: '',
      appPath: '',
      toast: null
    }
  },
  methods: {
    init () {
      this.toast = Toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      this.handleJump()
    },
    funJumpIndex () {
      const { name } = this.$route
      this.showFlag = false
      if (name === 'MarketingSave') {
        return
      }
      this.$nextTick(() => {
        this.$router.push({ name: 'MarketingSave' })
      })
    },
    async handleJump () {
      const linkUrl = this.redirectUrl.split('api/transfer/')
      try {
        const { status, data, msg } = await getJumpConfig(linkUrl[1])
        this.toast.clear()
        if (status) {
          // type 类型：1-h5 2-小程序
          const { type, url } = data
          this.accountId = data.app_id
          this.appPath = data.page_path
          if (type === 2 && isSupportJumpWXmini()) {
            this.showFlag = true
          } else {
            top.location.href = url
          }
        } else {
          Toast(msg)
        }
      } catch (e) {
        this.toast.clear()
      }
    },
    errorHandle (err) {
      Dialog.alert({
        message: JSON.stringify(err)
      }).then(() => {
        // on close
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.fbt-van-popup {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 273px;
  background: #fff;
  border-radius: 5px;

  p, a {
    line-height: 20px;
    font-size: 14px;
  }

  p {
    font-weight: bold;
    padding: 23px 0;
    color: #222;
  }

  a {
    padding: 13px 0;
    text-align: center;
  }

  .btns {
    display: flex;
    align-items: center;
    width: 100%;
    border-top: 1px solid #F3F3F3; /*no*/

    & > a {
      flex: 1;
      color: #999999;
    }

    & > div {
      position: relative;
      left: 0;
      top: 0;
      flex: 1;

      &:before {
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        height: 100%;
        content: '';
        border-left: 1px solid #F3F3F3;
      }

      a {
        display: block;
        width: 100%;
        font-weight: bold;
        color: #222;
      }
    }
  }
}
</style>
