<template>
  <div class='app-container'>
    <el-button type="primary" icon="el-icon-plus" size="mini" @click="getDetail()"
      v-hasPermi="['operation:agreement:add']">新增</el-button>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="协议名称" align="center" prop="name" />
      <el-table-column label="协议类型" align="center" prop="type" :formatter="typeFormatter" />
      <el-table-column label="更新时间" align="center" prop="updateTime" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDetail(scope.row.id, true)">查看</el-button>
          <el-button type="text" @click="getDetail(scope.row.id)" v-hasPermi="['operation:agreement:edit']">编辑</el-button>
          <el-button type="delete" @click="getDelete(scope.row)"
            v-hasPermi="['operation:agreement: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" />
  </div>
</template>

<script>
import { list, remove } from '@/api/operation/agreement'
export default {
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 列表
      tableData: [],
      // 总数据
      total: 0,
      // 协议类型
      typeOptions: [{
        value: 0,
        label: '用户协议'
      }, {
        value: 1,
        label: '隐私协议'
      }, {
        value: 2,
        label: '音乐服务许可协议'
      }, {
        value: 3,
        label: '儿童隐私保护指南'
      }, {
        value: 4,
        label: 'VIP音乐服务协议'
      }, {
        value: 5,
        label: '爱听付费协议'
      }, {
        value: 6,
        label: '关于我们'
      }]
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 列表
    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
        }
      })
    },

    // 详情
    getDetail(id, boolean) {
      this.$router.push({
        path: `/operation/agreement/detail`,
        query: {
          id: id,
          boolean: boolean
        }
      })
    },

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

    // 字典翻译
    typeFormatter(row) {
      return this.selectDictLabel(this.typeOptions, row.type)
    }
  }
}
</script>