<template>
  <div>

    <el-upload
      class="upload-demo"
      drag
      :action="`${uploadServer}/mgr/zoneinfo/upload/${partid}`"
      :show-file-list="false"
      accept=".mp3"
      :on-success="uploaded"
    >
      <i class="el-icon-upload" />
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      <div slot="tip" class="el-upload__tip">只能上传mp3文件</div>
    </el-upload>
    <en-table-layout
      :table-data="tableData"
      :loading="loading"
      height="300"
    >
      <!--表头-->
      <template slot="table-columns">
        <el-table-column
          label="序号"
          type="index"
          width="50"
        />
        <el-table-column prop="file_name" label="文件名称" align="center" />
        <el-table-column prop="file_size" label="文件大小" width="300" align="center" />
        <el-table-column
          label="操作"
          align="center"
          width="240"
        >
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="handlerDelete(scope.row.file_name)">
              删除
            </el-button>
            <el-button type="primary" size="mini" @click="selectFile(scope.row)">
              选择
            </el-button>
          </template>
        </el-table-column>

      </template>
    </en-table-layout>
  </div>
</template>

<script>
import * as API_BroadCast from '@/api/calling-broadcast'
import { serverUrl } from '@/utils/domain'
export default {
  name: 'BroadCastFilesViewer',
  data() {
    return {
      tableData: [],
      loading: false,
      uploadServer: serverUrl,
      partid: this.$store.getters.partId
    }
  },
  mounted() {
    this.getFileList()
  },
  methods: {
    getFileList() {
      API_BroadCast.getZoneFileList(this.$store.getters.partId).then(res => {
        this.tableData = [...res]
      })
    },
    uploaded() {
      this.getFileList()
    },
    handlerDelete(filename) {
      this.$confirm('删除操作后数据不可复原，您确定要删除此数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API_BroadCast.deleteZoneFile(this.$store.getters.partId, { file_name: filename }).then(
          response => {
            this.getFileList()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          }
        ).catch(response => {
          this.$message({
            type: 'info',
            message: response.message
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    selectFile(row) {
      this.$emit('selected', row)
    }
  }
}
</script>

<style scoped>

</style>
