<template>
  <view class="dec-voice">
    <view
      class="weixin"
      :class="arg.listStyle"
      v-if="arg.styleArg === 'weChat'"
    >
      <image
        class="image"
        mode="widthFix"
        :src="decImgDomain(arg.titleImg, 'temp-user.jpg')"
      />
      <view class="text" @click="action">
        <image
          class="r-image"
          mode="widthFix"
          :src="decImgDomain('', 'wechat-' + arg.listStyle + '.png')"
        />
      </view>
    </view>
    <!-- 通用播放器 -->
    <view class="simple" v-if="arg.styleArg === 'simple'">
      <view class="wrap">
        <image
          class="image"
          :src="decImgDomain(arg.titleImg, 'ic-cover.png')"
          @click="action"
        />
        <text class="text">
          {{ arg.title || "左侧编辑『标题』" }}
        </text>
      </view>
    </view>
  </view>
</template>

<script>
// 设置店招
export default {
  inject: ["decImgDomain"],
  props: {
    tab: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      arg: null,
      audioTarget: null,
      playing: false,
    };
  },
  created() {
    this.arg = this.tab.arg;
    this.initAudio();
  },
  methods: {
    initAudio() {
      const innerAudioContext = uni.createInnerAudioContext();
      innerAudioContext.autoplay = false;
      if (this.arg.source) {
        innerAudioContext.src = this.arg.source;
      } else {
        innerAudioContext.src =
          "https://bjetxgzv.cdn.bspapp.com/VKCEYUGU-hello-uniapp/2cc220e0-c27a-11ea-9dfb-6da8e309e0d8.mp3";
      }
      innerAudioContext.onPlay(() => {});
      innerAudioContext.onPause(() => {});
      innerAudioContext.onStop(() => {});
      innerAudioContext.onError(() => {});
      this.audioTarget = innerAudioContext;
    },
    action() {
      if (this.audioTarget.paused) {
        this.audioTarget.play();
      } else {
        if (this.arg.play === "continue") {
          this.audioTarget.pause();
        } else {
          this.audioTarget.stop();
        }
      }
    },
  },
  destroyed() {
    this.audioTarget.destroy();
  },
};
</script>
