<template>
  <div class='app-container'>
    <el-form>
      <el-form-item>
        <el-button type="primary" plain icon="Plus" @click="getDialog()"
          v-hasPermi="['content:class:add']">新增分类</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="data.tableData" v-loading="loading">
      <el-table-column label="分类ID" prop="id" 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 #default="scope">
          <el-button type="primary" link @click="getDialog(scope.row.id)"
            v-hasPermi="['content:class:edit']">编辑</el-button>
          <el-button type="danger" link @click="getDelete(scope.row)"
            v-hasPermi="['content:class:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗 -->
    <el-dialog v-model="dialogVisible" :title="title" width="500" :before-close="getClose">
      <el-form ref="dialogForm" :model="data.dialogForm" :rules="data.rules">
        <el-form-item label="分类名称：" prop="name">
          <el-input v-model="data.dialogForm.name" placeholder="请输入分类名称" />
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button @click="getClose">取消</el-button>
        <el-button type="primary" @click="getSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { list, detail, submit, remove } from '@/api/content/class'

const { proxy } = getCurrentInstance();
// 遮罩层
const loading = ref(false)

const data = reactive({
  // 列表
  tableData: [],
  // 弹窗表单
  dialogForm: {},
  // 校验
  rules: {
    name: [{
      required: true, message: '请输入分类名称', trigger: 'blur'
    }]
  }
})

// 列表
function getList() {
  list(data.form).then(res => {
    data.tableData = res.data
  })
}
getList()

// 弹窗
const dialogVisible = ref(false)
// 弹窗标题
const title = ref("新增")
// 打开弹窗
function getDialog(id, key) {
  dialogVisible.value = true
  title.value = '新增'
  if (id) {
    getDetail(id)
  }
}

// 详情
function getDetail(id) {
  title.value = '编辑'
  detail({ id: id }).then(res => {
    if (res.code === 0) {
      data.dialogForm = res.data
    }
  })
}

// 取消
function getClose() {
  dialogVisible.value = false
  data.dialogForm = {}
  proxy.resetForm('dialogForm')
}

// 提交
function getSubmit() {
  proxy.$refs['dialogForm'].validate(valid => {
    if (valid) {
      submit(data.dialogForm).then(res => {
        if (res.code === 0) {
          proxy.$modal.msgSuccess("提交成功！");
          getClose()
          getList()
        }
      })
    } else {
      return false
    }
  })
}

// 删除
function getDelete(row) {
  proxy.$modal.confirm(`是否删除分类名称为：${row.name}的数据?`, '提示', {
    type: 'warning'
  }).then(() => {
    remove({ id: row.id }).then(res => {
      if (res.code === 0) {
        proxy.$modal.msgSuccess('删除成功！')
        getList()
      }
    }).catch(() => { })
  })
}
</script>