<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 type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center"  width="55" prop="id" />
      <el-table-column label="订单编号" align="center" prop="ocode" />
      <el-table-column label="师傅" align="center" prop="workerName" />
      <el-table-column label="录音文件" align="center" prop="file">
        <template slot-scope="scope">
          <span v-if="scope.row.file" style="cursor:pointer;">
            <svg v-if="playingRowId !== scope.row.id" @click="startAudio(scope.row)" width="24" height="24" viewBox="0 0 24 24" fill="#409EFF" style="vertical-align: middle;">
              <polygon points="8,5 19,12 8,19" />
            </svg>
            <svg v-else @click="stopAudio()" width="24" height="24" viewBox="0 0 24 24" fill="#F56C6C" style="vertical-align: middle;">
              <rect x="6" y="6" width="12" height="12" rx="2"/>
            </svg>
            <!-- 进度条 -->
            <div style="display:inline-block;width:100px;vertical-align:middle;margin:0 8px;">
              <el-progress
                :percentage="audioProgress[scope.row.id] || 0"
                :show-text="false"
                :stroke-width="4"
                style="vertical-align:middle;"
              />
            </div>
            <!-- 当前时间/总时长 -->
            <span style="font-size:12px;color:#888;">
              {{ formatTime(audioCurrentTime[scope.row.id] || 0) }}/{{ formatTime(audioDuration[scope.row.id] || 0) }}
            </span>
          </span>
          <span v-else style="color: #ccc;">无</span>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center" prop="createdAt" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </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()
      }
    },
    immediate: true
  },
  mounted(){
    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>
