<template>
  <div class="recording-item">
    <div class="recording-player">
      <div class="player-controls">
        <button class="play-button" :class="{ 'playing': isPlaying }" @click="handleTogglePlay">
          <a-icon :type="isPlaying ? 'pause-circle' : 'play-circle'" :theme="isPlaying ? 'filled' : 'outlined'" />
        </button>
      </div>
      <div class="player-info">
        <div class="recording-title">{{ recording.name || `录音文件 ${recording.id}` }}</div>
        <div class="recording-duration">
          <span class="current-time">{{ formatTime(currentTime) }}</span>
          <span class="divider">/</span>
          <span class="total-time">{{ formatTime(duration) }}</span>
        </div>
      </div>
      <div class="player-progress no-print">
        <div class="progress-bar" @click="handleSeekTo">
          <div class="progress-fill" :style="{ width: progressPercent + '%' }"></div>
          <div class="progress-handle" :style="{ left: progressPercent + '%' }"></div>
        </div>
      </div>
    </div>
    <audio ref="audio" :src="recording.url" @loadedmetadata="onLoadedMetadata" @timeupdate="onTimeUpdate" @ended="onEnded" style="display: none;" />
  </div>
</template>

<script>
export default {
  name: 'RecordingPlayer',
  props: {
    recording: { type: Object, required: true },
    isPlaying: { type: Boolean, default: false }
  },
  data () {
    return { currentTime: 0, duration: 0 }
  },
  computed: {
    progressPercent () {
      return this.duration > 0 ? (this.currentTime / this.duration) * 100 : 0
    }
  },
  watch: {
    isPlaying (newVal) {
      if (newVal) this.play()
      else this.pause()
    },
    recording (newVal, oldVal) {
      if (newVal !== oldVal) this.resetState()
    }
  },
  methods: {
    handleTogglePlay () {
      if (this.isPlaying) this.$emit('pause', this.recording)
      else this.$emit('play', this.recording)
    },
    play () { if (this.$refs.audio) this.$refs.audio.play() },
    pause () { if (this.$refs.audio) this.$refs.audio.pause() },
    handleSeekTo (event) {
      const audio = this.$refs.audio
      if (!audio) return
      const progressBar = event.currentTarget
      const rect = progressBar.getBoundingClientRect()
      const percent = (event.clientX - rect.left) / rect.width
      const newTime = percent * audio.duration
      if (!isNaN(newTime)) audio.currentTime = newTime
    },
    formatTime (seconds) {
      if (!seconds || isNaN(seconds)) return '0:00'
      const mins = Math.floor(seconds / 60)
      const secs = Math.floor(seconds % 60)
      return `${mins}:${secs.toString().padStart(2, '0')}`
    },
    onLoadedMetadata () { if (this.$refs.audio) this.duration = this.$refs.audio.duration },
    onTimeUpdate () { if (this.$refs.audio) this.currentTime = this.$refs.audio.currentTime },
    onEnded () { this.currentTime = 0; this.$emit('ended', this.recording) },
    resetState () {
      this.currentTime = 0
      this.duration = 0
      if (this.$refs.audio) this.$refs.audio.currentTime = 0
    }
  }
}
</script>

<style lang="less" scoped>
.recording-item { background: #f8faff; border: 1px solid #e6f0ff; border-radius: 12px; padding: 3%; transition: all 0.3s ease; }
.recording-item:hover { background: #f0f8ff; border-color: #bdd7ff; box-shadow: 0 2px 8px rgba(59, 130, 246, 0.1); }
.recording-player { display: flex; align-items: center; gap: 12px; }
.play-button { width: 40px; height: 40px; border: none; border-radius: 50%; background: #3b82f6; color: white; cursor: pointer; display: flex; align-items: center; justify-content: center; transition: all 0.3s ease; }
.play-button:hover { background: #2563eb; transform: scale(1.05); }
.play-button.playing { background: #ef4444; }
.player-info { flex: 1; min-width: 0; margin-right: 12px; }
.recording-title { font-size: 14px; font-weight: 600; color: #1f2937; margin-bottom: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recording-duration { display: flex; align-items: center; gap: 4px; font-size: 12px; color: #6b7280; }
.progress-bar { position: relative; height: 6px; background: #e5e7eb; border-radius: 3px; cursor: pointer; }
.progress-fill { height: 100%; background: linear-gradient(90deg, #3b82f6, #1d4ed8); border-radius: 3px; transition: width 0.1s ease; }
.progress-handle { position: absolute; top: 50%; width: 14px; height: 14px; background: #3b82f6; border: 2px solid white; border-radius: 50%; transform: translateY(-50%) translateX(-7px); cursor: grab; opacity: 0; }
</style>
