<template>
  <div class="activity-audio-management">
    <div class="part-box-head">
      <div class="part-box-head-left">
        <h1>
          语音播报
          <span v-if="examInfo"
            >-- {{ examInfo.examName }}（{{ examInfo.examCode }}） --
            场次代码({{ examInfo.activityCode }})</span
          >
        </h1>
      </div>
      <div class="part-box-head-right">
        <el-button type="primary" @click="handleCurrentChange(0)"
          >查询</el-button
        >
        <el-button type="primary" icon="icon icon-add" @click="add"
          >新增</el-button
        >
        <el-button type="primary" icon="el-icon-back" @click="$router.back()"
          >返回</el-button
        >
      </div>
    </div>

    <el-table :data="tableData">
      <el-table-column width="200" prop="id" label="ID"> </el-table-column>
      <el-table-column label="语音内容">
        <span slot-scope="scope">{{ scope.row.content }}</span>
      </el-table-column>
      <el-table-column label="播报时间">
        <span slot-scope="scope"
          >{{ scope.row.type | activityAudioTypeFilter
          }}{{ scope.row.playTime | playTimFilter }}</span
        >
      </el-table-column>
      <el-table-column width="80" label="状态">
        <span slot-scope="scope">{{
          scope.row.enable | booleanEnableDisableFilter
        }}</span>
      </el-table-column>
      <el-table-column width="300" label="语音">
        <template slot-scope="scope">
          <audio
            style="width: 100%; height: 24px;"
            :src="scope.row.attachmentPath"
            controls
          ></audio>
        </template>
      </el-table-column>
      <el-table-column width="120" label="操作人">
        <span slot-scope="scope">{{ scope.row.updateName }}</span>
      </el-table-column>
      <el-table-column sortable width="170" label="更新时间">
        <span slot-scope="scope">{{
          scope.row.updateTime | datetimeFilter
        }}</span>
      </el-table-column>
      <el-table-column :context="_self" label="操作" width="160" fixed="right">
        <div slot-scope="scope">
          <el-button
            v-if="scope.row.audioDefault === 'CUSTOM'"
            size="mini"
            type="primary"
            plain
            @click="edit(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            :type="!scope.row.enable ? 'primary' : 'danger'"
            plain
            @click="enableAudio(scope.row)"
          >
            {{ !scope.row.enable | booleanEnableDisableFilter }}
          </el-button>
        </div>
      </el-table-column>
    </el-table>
    <div class="part-page">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100, 200, 300]"
        @size-change="handleSizeChange"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      />
    </div>

    <ActivityAudioDialog
      ref="ActivityAudioDialog"
      :instance="selectedAudio"
      @reload="searchForm"
    />
  </div>
</template>

<script>
import {
  searchActivityAudios,
  toggleEnableActivityAudio,
} from "@/api/examwork-activity";
import ActivityAudioDialog from "./ActivityAudioDialog";
import { timeNumberToText } from "@/utils/utils";

export default {
  name: "ActivityAudioManagement",
  components: {
    ActivityAudioDialog,
  },
  filters: {
    playTimFilter(val) {
      return val ? timeNumberToText(val * 1000) : "";
    },
  },
  computed: {
    activityId() {
      return this.$route.params.activityId;
    },
  },
  data() {
    return {
      examInfo: null,
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 10,
      selectedAudio: {},
    };
  },
  async created() {
    const examInfo = window.sessionStorage.getItem("examInfo");
    this.examInfo = examInfo ? JSON.parse(examInfo) : null;
    this.searchForm();
  },
  methods: {
    async searchForm() {
      const res = await searchActivityAudios({
        activityId: this.activityId,
        pageNumber: this.currentPage,
        pageSize: this.pageSize,
      });
      this.tableData = res.data.data.records;
      this.total = res.data.data.total;
      if (this.total > 0 && this.tableData.length === 0) {
        this.handleCurrentChange(this.currentPage - 1);
      }
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.searchForm();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.searchForm();
    },
    add() {
      this.selectedAudio = {
        activityId: this.activityId,
      };
      this.$refs.ActivityAudioDialog.openDialog();
    },
    edit(row) {
      this.selectedAudio = row;
      this.$refs.ActivityAudioDialog.openDialog();
    },
    async enableAudio(row) {
      await toggleEnableActivityAudio({
        audioId: row.id,
        enable: !row.enable,
      });
      row.enable = !row.enable;
    },
  },
};
</script>
