<template>
  <div class="app-container">
    <el-button type="primary" icon="el-icon-plus" size="mini" @click="getDialog('新增')" v-hasPermi="['music:classify:add']">新增</el-button>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column type="index" label="序号" align="center" />
      <el-table-column label="分类名称" prop="title" align="center" />
      <el-table-column label="更新时间" align="center">
        <template slot-scope="scope">
          {{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDialog('编辑', scope.row.id)" v-hasPermi="['music:classify:edit']">编辑</el-button>
          <el-button type="delete" @click="getDelete(scope.row)" v-hasPermi="['music:classify:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="form.pageNum" :limit.sync="form.pageSize"
      @pagination="getList" />
    <!-- 弹窗 -->
    <el-dialog :visible.sync="dialogVisible" :title="title" width="500px" :before-close="cancel">
      <el-form :model="dialogForm" :rules="rules" ref="dialogForm" label-width="auto">
        <el-form-item label="分类名称：" prop="title">
          <el-input v-model="dialogForm.title" placeholder="请输入分类名称" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="getSubmit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, submit, detail, remove } from '@/api/music/classify'
export default {
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 列表
      tableData: [],
      // 总数据
      total: 0,
      // 弹窗
      dialogVisible: false,
      // 弹窗名称
      title: '新增',
      // 弹窗表单
      dialogForm: {},
      // 校验
      rules: {
        title: [{
          required: true, message: '请输入分类名称', trigger: 'blur'
        }]
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 新增 编辑
    getDialog(title, id) {
      this.dialogVisible = true
      this.title = title
      if (id) {
        detail(id).then(res => {
          if (res.code === 0) {
            this.dialogForm = res.data
          }
        })
      }
    },

    // 提交
    getSubmit() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          submit(this.dialogForm).then(res => {
            if (res.code === 0) {
              this.$message.success(`${this.title}成功!`)
              this.cancel()
              this.getList()
            }
          })
        } else {
          return false
        }
      })
    },

    // 取消
    cancel() {
      this.$refs.dialogForm.resetFields()
      this.dialogForm = {}
      this.dialogVisible = false
    },

    // 列表
    getList() {
      this.loading = true
      list(this.form).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      })
    },

    // 删除
    getDelete(row) {
      this.$confirm(`是否删除${row.title}?`, '提示', {
        type: 'warning'
      }).then(() => {
        remove(row.id).then(res => {
          if (res.code === 0) {
            this.$message.success('删除成功!')
            this.getList()
          }
        })
      }).catch(() => { })
    }
  }
}
</script>