<template>
  <div class="audio-wrapper">
    <span class="audio-play" @click="playAudio">
      <SvgIcon size="20" icon-class="audio" />
    </span>
    <audio ref="audio" :src="url" preload="metadata"></audio>
  </div>
</template>

<script>
import { GetFileURLMap } from '@/api/app';

export default {
  name: 'AudioPlay',
  props: {
    fileId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      url: ''
    };
  },
  watch: {
    fileId: {
      handler(val) {
        if (!val) return;
        GetFileURLMap({ file_id_list: [val] }).then(({ url_map }) => {
          this.url = url_map[val];
        });
      },
      immediate: true
    }
  },
  methods: {
    playAudio() {
      this.$refs.audio.play();
    }
  }
};
</script>

<style lang="scss" scoped>
.audio-wrapper {
  .audio-play {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    cursor: pointer;
    background-color: $main-color;
    border-radius: 50%;
  }
}
</style>
