<!--设备管理 设备分类 -->
<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:category: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="状态" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 1 ? '' : 'danger'">
            {{ selectDictLabel(statusOptions, scope.row.status) }}
          </el-tag>
        </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:category:edit']"
          >
            编辑
          </el-button>
          <el-button
            type="delete"
            @click="getDelete(scope.row)"
            v-hasPermi="['device:category: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="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">停用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="分类排序：" prop="sort">
          <el-input-number
            v-model="form.sort"
            :min="1"
            :step="1"
            step-strictly
          />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button size="mini" @click="getCancel">取消</el-button>
        <el-button type="primary" size="mini" @click="getCreate"
          >确定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { create, edit, list, remove } from "@/api/device/category";
import { dialogCallBack } from "@/utils/DialogUtil";
export default {
  name: "DeviceCategory",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 表格数据
      tableData: [],
      // 弹窗
      dialogVisible: false,
      title: "",
      // 提交表单
      form: {
        name: "",
        status: 1,
      },
      // 表单验证
      rules: {
        sort: [
          {
            required: true,
            message: "请选择标签排序",
            trigger: "change",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入分类名称",
            trigger: "blur",
          },
        ],
        status: [
          {
            required: true,
            message: "请选择状态",
            trigger: "change",
          },
        ],
      },
      // 设备状态
      statusOptions: [
        {
          value: 1,
          label: "正常",
        },
        {
          value: 2,
          label: "停用",
        },
      ],
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 分类列表
    getList() {
      this.loading = true;
      list().then((res) => {
        if (res.code === 0) {
          this.tableData = res.data;
          this.loading = false;
        }
      });
    },

    // 新增 / 编辑按钮
    getChange(row) {
      this.dialogVisible = true;
      if (row) {
        var aaa = JSON.stringify(row);
        var bbb = JSON.parse(aaa);
        this.form = bbb;
      } else {
        this.form = {
          name: "",
          status: 1,
        };
      }
      this.title = row ? "编辑" : "新增";
    },

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

    // 确定
    getCreate() {
      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;
      var id = row.id;
      dialogCallBack(that, function () {
        that
          .$confirm(`是否删除${row.name}?`, "提示：", {
            type: "warning",
          })
          .then(() => {
            remove(id).then((res) => {
              if (res.code === 0) {
                that.getList();
                that.$message.success("删除成功！");
              }
            });
          })
          .catch(() => {});
      });
    },
  },
};
</script>