<template>
  <div class='app-container'>
    <el-button type="primary" icon="el-icon-plus" size="mini" @click="getDialog('新增')"
      v-hasPermi="['operation:feedbacktype:add']">新增</el-button>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="序号" type="index" align="center"></el-table-column>
      <el-table-column label="排序" prop="sort" align="center"></el-table-column>
      <el-table-column label="类型名称" prop="name" align="center"></el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDialog('编辑', scope.row.id)"
            v-hasPermi="['operation:feedbacktype:edit']">编辑</el-button>
          <el-button type="delete" @click="getDelete(scope.row)"
            v-hasPermi="['operation:feedbacktype: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="close">
      <el-form :model="dialogForm" ref="dialogForm" :rules="rules" label-width="100px">
        <el-form-item label="类型名称：" prop="name">
          <el-input v-model="dialogForm.name" placeholder="请输入类型名称" />
        </el-form-item>
        <el-form-item label="排序：" prop="sort">
          <el-input-number v-model="dialogForm.sort" :min="1" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="getSubmit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, detail, remove, submit } from '@/api/operation/feedbacktype'
export default {
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 总数据
      total: 0,
      // 列表
      tableData: [],
      // 弹窗
      dialogVisible: false,
      // 弹窗标题
      title: '新增',
      // 弹窗表单
      dialogForm: {},
      // 校验
      rules: {
        name: [{
          required: true, message: '请输入类型名称', trigger: 'blur'
        }],
        sort: [{
          required: true, message: '请输入排序', trigger: 'blur'
        }]
      }
    }
  },
  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
        }
      })
    },

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

    // 弹窗
    getDialog(title, id) {
      this.dialogVisible = true
      this.title = title
      if (id) {
        detail(id).then(res => {
          this.dialogForm = res.data
        })
      }
    },

    // 关闭
    close() {
      this.dialogVisible = false
      this.dialogForm = {}
    },

    // 确定
    getSubmit() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          submit(this.dialogForm).then(res => {
            if (res.code === 0) {
              this.$message.success('提交成功！')
              this.close()
              this.getList()
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>