<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form inline label-width="100px" size="mini" @submit.native.prevent>
      <el-form-item>
        <el-button type="primary" icon="el-icon-plus" plain @click="getChange()">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="权限ID" prop="id" align="center" />
      <el-table-column label="功能名称" prop="name" align="center" />
      <el-table-column label="功能分类" prop="type" align="center"></el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getChange(scope.row)">编辑</el-button>
          <el-button type="delete" @click="getDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增弹窗 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="500px" :before-close="getCancel">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="功能名称：" prop="name">
          <el-input v-model="form.name" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="功能分类：" prop="type">
          <el-input-number v-model="form.type" />
        </el-form-item>
        <el-form-item label="启用状态：" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="0">正常</el-radio>
            <el-radio :label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="getCancel">取消</el-button>
        <el-button type="primary" @click="getSubmit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { functionList, functionAdd, functionEdit } from "@/api/device/function";
export default {
  dicts: ["sys_normal_disable"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 表格数据
      tableData: [],
      // 弹窗
      dialogVisible: false,
      title: "",
      form: {
        name: "",
        type: "",
        status: 0,
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入功能名称",
            trigger: "blur",
          },
        ],
        type: [
          {
            required: true,
            message: "请选择功能分类",
            trigger: "change",
          },
        ],
        status: [
          {
            required: true,
            message: "请选择状态",
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 权限列表
    getList() {
      this.loading = true;
      functionList({ isDelete: 0 }).then((res) => {
        if (res.data.code === 0) {
          this.tableData = res.data.data;
          this.loading = false;
        }
      });
    },

    // 新增 / 编辑
    getChange(row) {
      this.dialogVisible = true;
      this.form = row
        ? row
        : {
            name: "",
            type: "",
            status: 0,
          };
      this.title = row ? "编辑" : "新增";
    },

    // 取消
    getCancel() {
      this.$refs.form.clearValidate();
      this.dialogVisible = false;
    },

    // 确定
    getSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id) {
            // 编辑
            functionEdit(this.form).then((res) => {
              if (res.code === 0) {
                this.$message.success("修改成功！");
                this.dialogVisible = false;
                this.getList();
              }
            });
          } else {
            // 新增
            functionAdd(this.form).then((res) => {
              if (res.code === 0) {
                this.$message.success("创建成功！");
                this.dialogVisible = false;
                this.getList();
              }
            });
          }
        } else {
          return false;
        }
      });
    },

    // 删除
    getDelete(row) {
      this.form = row;
      this.form.isDelete = 1;
      functionEdit(this.form).then((res) => {
        if (res.code === 0) {
          this.$message.success("已删除！");
          this.getList();
        }
      });
    },
  },
};
</script>

<style>
</style>