<template>
  <el-row>
    <el-row :md="6" :sm="12" :xs="12">
      <el-table
        :data="dataList"
        style="width: 100%"
      >
        <el-table-column
          type="index">
        </el-table-column>
        <el-table-column
          prop="publishAt"
          label="发布时间">
        </el-table-column>
        <el-table-column
          prop="title"
          label="音频名字">
          <template slot-scope="scope">
            <router-link target="_blank" :to="`/audio/${scope.row.audioId}`">
              <span>{{scope.row.title}}</span>
            </router-link>
          </template>
        </el-table-column>
        <el-table-column
          prop="duration"
          label="时长">
        </el-table-column>
        <el-table-column
          prop="codec"
          label="编码">
        </el-table-column>
        <el-table-column
          prop="scope"
          label="可见范围">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="点击修改可见范围" placement="top-end">
              <el-button
                v-if="scope.row.scope === 1"
                size="mini"
                @click="handleScope(scope.$index, scope.row)">所有人可见</el-button>
              <el-button
                v-else-if="scope.row.scope === 2"
                size="mini"
                type="success"
                @click="handleScope(scope.$index, scope.row)">验证码可见</el-button>
              <el-button
                v-else-if="scope.row.scope === 3"
                size="mini"
                type="warning"
                @click="handleScope(scope.$index, scope.row)">VIP 可见</el-button>
              <el-button
                v-else
                size="mini"
                type="danger"
                @click="handleScope(scope.$index, scope.row)">本人可见</el-button>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>

    <!-- 修改可见范围对话框 -->
    <el-dialog
      append-to-body
      :visible.sync="showEditScopeDialog"
      width="30%"
      center
    >
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>修改可见范围</span>
          <el-button style="float: right; padding: 3px 0" type="text" @click="onUpdateScope">更新</el-button>
        </div>
        <div class="text item">
          <el-select v-model="form.scope" placeholder="选择可见范围">
            <el-option label="所有人可见" value="1" />
            <el-option label="验证码可见" value="2" />
            <el-option label="VIP 可见" value="3" />
            <el-option label="仅自己可见" value="4" />
          </el-select>
        </div>
      </el-card>
    </el-dialog>
  </el-row>
</template>

<script>
import { updateAudioScope, deleteAudioPost } from "@/api/audio";

export default {
  name: 'AudioPost',
  props: {
    dataList: {
      type: Array,
      default: []
    },
  },
  components: {},
  data() {
    return {
      showEditScopeDialog: false,
      form: {
        audioId: null,
        scope: 1
      }
    }
  },
  created() {
  },
  methods: {
    handleScope(index, row) {
      this.form.audioId = row.audioId
      this.form.scope = ''+row.scope
      this.showEditScopeDialog = true
    },
    handleEdit(index, row) {
      const path = '/post/edit/audio/' + row.audioId
      this.$router.push(path)
    },
    handleDelete(index, row) {
      this.$confirm('确定要删除 ' + row.title + '?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAudioPost(row.audioId).then(res => {
          if (res.code === 0) {
            this.$notify({
              title: '稿件已删除',
              type: 'warning',
              duration: 3000
            })
            //this.$router.go(0)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    onUpdateScope() {
      this.showEditScopeDialog = false
      updateAudioScope(this.form).then(res => {
        if (res.code === 0) {
          this.$notify({
            title: '提示',
            message: '可见范围已更新',
            type: 'warning',
            duration: 3000
          })
        }
      }).catch(error => {
        this.$notify({
          title: '提示',
          message: error.message,
          type: 'warning',
          duration: 3000
        })
      })
    }
  }
}
</script>

<style>
</style>
