<template>
  <el-dialog
    title="录音文件"
    :visible.sync="visible"
    width="70%"
    :before-close="handleClose"
    append-to-body
  >
    <div class="dialog-content">
      <div class="dialog-header">
        <div class="order-info">
          <span>订单号: {{ orderInfo ? orderInfo.orderId : '' }}</span>
          <span style="margin-left: 20px">客户: {{ orderInfo ? orderInfo.name : '' }}</span>
        </div>
        <el-button type="primary" size="mini" icon="el-icon-refresh" @click="refresh">刷新</el-button>
      </div>

      <el-table
        :max-height="400"
        :data="recordList"
        style="width: 100%"
        v-loading="loading"
        border
        stripe
      >
        <el-table-column label="录音时间" align="center" width="180">
          <template slot-scope="scope">
            {{ parseTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="录音时长" align="center" width="120">
          <template slot-scope="scope">
            {{ formatDuration(scope.row.duration) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="playAudio(scope.row.audioUrl)"
              icon="el-icon-headset"
            >播放</el-button>
            <el-button
              type="text"
              @click="downloadAudio(scope.row.audioUrl)"
              icon="el-icon-download"
            >下载</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div v-if="!recordList || recordList.length === 0" class="empty-record">
        <i class="el-icon-document"></i>
        <p>暂无记录信息</p>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { getAudioRecords } from "@/api/system/Order"

export default {
  name: "AudioRecord",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    orderInfo: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      recordList: [],
      loading: false
    }
  },
  watch: {
    visible(val) {
      if (val && this.orderInfo) {
        this.getList()
      }
    }
  },
  methods: {
    getList() {
      this.loading = true
      getAudioRecords(this.orderInfo.orderId).then(response => {
        this.recordList = response.data
        this.loading = false
      }).catch(() => {
        this.loading = false
        this.$message.error('获取记录失败')
      })
    },
    formatDuration(seconds) {
      const minutes = Math.floor(seconds / 60)
      const remainingSeconds = seconds % 60
      return `${minutes}分${remainingSeconds}秒`
    },
    playAudio(url) {
      // 实现音频播放逻辑
      console.log('播放音频:', url)
    },
    downloadAudio(url) {
      // 实现音频下载逻辑
      console.log('下载音频:', url)
    },
    refresh() {
      this.getList()
    },
    handleClose() {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-content {
  min-height: 300px;
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.order-info {
  font-weight: bold;
  font-size: 14px;
}

.empty-record {
  padding: 40px 0;
  text-align: center;
  color: #909399;
}

.empty-record i {
  font-size: 40px;
  margin-bottom: 15px;
}
</style> 