<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-container-lt">
        <el-form :inline="true" label-position="left" :model="listQuery">
          <el-form-item>
            <el-input clearable v-model="listQuery.musicName" placeholder="音乐名称"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-select v-model="listQuery.parentId" clearable filterable placeholder="音乐类目">
              <el-option
                v-for="item in musicOptions"
                :key="item.id"
                :label="item.musicCategoryName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-button type="primary" @click="getList()">搜索</el-button >
            <el-button type="primary" @click="add()">新增</el-button >
        </el-form>
      </div>
    </div>
    <div class="table-container">
      <el-table  style="width: 100%" v-loading="listLoading" :key="tableKey" :data="list" row-key="id"
        stripe border fit highlight-current-row>
        <el-table-column label="序号" type="index" width="60" align="center" />
        <el-table-column label="音乐名称" min-width="100" align="center" prop="musicName" />
        <el-table-column label="音乐分类" min-width="130" align="center">
          <template slot-scope="scope">
            {{  scope.row.type == 0 ? '类目' : '音乐' }}
          </template>
        </el-table-column>
        <el-table-column label="音乐时长" min-width="130" align="center">
          <template slot-scope="scope">
            {{  scope.row.musicDuration + '秒' }}
          </template>
        </el-table-column>
        <el-table-column label="音乐地址" min-width="100" align="center" prop="musicUrl" />
        <el-table-column label="操作" align="center" width="160">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="编辑" placement="top">
              <el-button type="success" size="mini" icon="el-icon-edit" circle @click="handleEdit(scope.row)" />
            </el-tooltip>
            <el-tooltip class="more" effect="dark" content="删除" placement="top">
              <el-button type="danger" size="mini" icon="el-icon-delete-solid" circle
                @click="handleDelete(scope.row)" />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <swPage v-if="total > 0" key="2" :listQuery="listQuery" :total="total" pos="btmRight" @retPage="retPage" />
    <musicLibraryUpdater ref="musicLibraryUpdater" @confirm="retPage"></musicLibraryUpdater>
  </div>
</template>

<script>
import waves from "@/directive/waves";
import swPage from "@/views/common/swPage";
import { auditStatus } from "@/constants/index";
import { getValueByKey } from "@/utils/index";
import { fetchMusicConfigList, deteleMusicConfig } from "@/api/musicLibraryManager";
import permission from "@/directive/permission";
import musicLibraryUpdater from "./musicLibraryUpdater";

export default {
  name: "videoCoverManager",
  directives: {
    waves,
    permission
  },
  components: {
    swPage,
    musicLibraryUpdater
  },
  computed: {
  },
  data() {
    return {
      auditStatus,
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: false,
      downloadingRows: {},
      listQuery: {
        currentPage: 1,
        pageSize: 10,
        type: 1,
        musicName: null,
        parentId: null
      },
      musicOptions: []
    };
  },
  //页面创建的时候执行
  created() {
    this.getMusicCategory()
    this.getList();
  },
  methods: {
    getValueByKey,
    getMusicCategory() {
      const params = {
        type: 0
      }
      fetchMusicConfigList(params).then(res => {
        if (res.code == 200) {
          this.musicOptions = res.data.rows;
        }
      });
    },
    getList() {
      this.listLoading = true;
      const params = this.listQuery
      if (!this.listQuery.musicName) {
        delete params.musicName
      }
      if (!this.listQuery.parentId) {
        delete params.parentId
      }
      fetchMusicConfigList(params).then(res => {
        if (res.code == 200) {
          this.total = res.data.total;
          this.list = res.data.rows;
        }
        this.listLoading = false;
      });
    },
    retPage() {
      //分页
      this.getList();
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleEdit(row) {
      this.$refs.musicLibraryUpdater.show(row, 'edit', this.musicOptions);
    },
    add() {
      this.$refs.musicLibraryUpdater.show(null, 'add', this.musicOptions);
    },
    handleDelete(row) {
      //删除
      this.$confirm("确定要删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        const { id } = row;
        deteleMusicConfig([id]).then(() => {
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
            duration: 3000
          });
          const index = this.list.indexOf(row);
          this.list.splice(index, 1);
        });
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "@/styles/layout.scss";

.button {
  .el-button {
    margin-bottom: 10px;

    &:last-child {
      margin-bottom: 0;
    }
  }
}
</style>
