<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()"
          v-hasPermi="['device:function:add']"
          >新增</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)"
            v-hasPermi="['device:function:edit']"
          >
            编辑
          </el-button>
          <el-button
            type="delete"
            @click="getDelete(scope.row)"
            v-hasPermi="['device:function:delete']"
          >
            删除
          </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 { create, edit, list } from "@/api/device/function";
import { callBack } from "@/utils/DialogUtil";
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;
      list({ isDelete: 0 }).then((res) => {
        if (res.code === 0) {
          this.tableData = res.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) {
            // 编辑
            edit(this.form).then((res) => {
              if (res.code === 0) {
                this.$message.success("修改成功！");
                this.dialogVisible = false;
                this.getList();
              }
            });
          } else {
            // 新增
            create(this.form).then((res) => {
              if (res.code === 0) {
                this.$message.success("创建成功！");
                this.dialogVisible = false;
                this.getList();
              }
            });
          }
        } else {
          return false;
        }
      });
    },

    // 删除
    getDelete(row) {
      var that = this;
      callBack(that, function () {
        that
          .$confirm(`是否删除${row.name}?`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
          .then(() => {
            that.form = row;
            that.form.isDelete = 1;
            edit(that.form).then((res) => {
              if (res.code === 0) {
                that.$message.success("已删除！");
                that.getList();
              }
            });
          });
      });

      // this.$alert(`删除后数据不可恢复，请谨慎进行该操作！`, "温馨提示", {
      //   confirmButtonText: "确定",
      // }).then(() => {
      //   setTimeout(() => {
      //     this.$confirm(`是否删除${row.name}?`, "提示", {
      //       confirmButtonText: "确定",
      //       cancelButtonText: "取消",
      //       type: "warning",
      //     }).then(() => {
      //       this.form = row;
      //       this.form.isDelete = 1;
      //       edit(this.form).then((res) => {
      //         if (res.code === 0) {
      //           this.$message.success("已删除！");
      //           this.getList();
      //         }
      //       });
      //     });
      //   }, 300);
      // });
    },
  },
};
</script>