<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true">
      <el-form-item label="菜单名称:" prop="typeName">
        <el-input
          v-model="queryParams.typeName"
          placeholder="请输入菜单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态:" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="权限状态"
        >
          <el-option label="全部" value=""></el-option>
          <el-option label="正常" value="1"></el-option>
          <el-option label="禁用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button-group>
          <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          >新增
          </el-button>
          <el-button type="primary" icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠</el-button>
        </el-button-group>
      </el-form-item>
    </el-form>
    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="menuList"
      row-key="id"
      border
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="序号" type="index"></el-table-column>
      <el-table-column prop="typeName" label="状态名称" :show-overflow-tooltip="true" width="160"></el-table-column>
      <el-table-column prop="type" label="状态标识" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="status" label="权限状态" width="80">
      </el-table-column>
      <el-table-column prop="remark" label="备注" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"

            @click="handleUpdate(scope.row)"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleAdd(scope.row)"
          >新增
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>


    <el-dialog :title="formMap[formName]" :visible.sync="open" width="45%">
      <el-form :model="form" ref="dataForm" :rules="rules">
        <el-row>
          <el-form-item label="所属类型:" label-width="100px" v-if="form.parentId !== null">
            <el-input
              v-model="form.parentName"
              style="width: 90%"
              clearable
              disabled
              size="small"
            ></el-input>
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="类型名称:" label-width="100px" prop="typeName">
            <el-input
              v-model="form.typeName"
              style="width: 90%"
              clearable
              size="small"
            ></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="类型标识:" label-width="100px" prop="type">
            <el-input
              v-model="form.type"
              style="width: 90%"
              clearable
              size="small"
            ></el-input>
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="备注:" prop="remark" label-width="100px">
            <el-input
              v-model="form.remark"
              size="small"
              clearable
              style="width: 90%"
            ></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="状态:" prop="status" label-width="100px">
            <el-radio-group v-model="form.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="0">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click.native="cancel" size="small">取消</el-button>
        <el-button type="primary" size="small" @click.native="submitForm()"
        >提交
        </el-button
        >
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listStatus, delStatus, addStatus, updateStatus} from "@/api/system/dataStatus";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "dataStatus",
  data() {
    return {
      loading: true,
      menuList: [],
      menuOptions: [],
      title: "",
      open: false,
      isExpandAll: false,
      refreshTable: true,
      queryParams: {
        typeName: undefined,
        status: undefined
      },
      form: {},
      formMap: {
        add: "数据权限-新增",
        edit: "数据权限-编辑",
      },
      formName: null,
      rules: {
        type: [
          {required: true, message: "数据标识不能为空", trigger: "blur"}
        ],
        typeName: [
          {required: true, message: "标识名称不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "权限状态不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 选择图标
    selected(name) {
      this.form.icon = name;
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = true;
      listStatus(this.queryParams).then(response => {
        this.menuList = this.handleTree(response.data.list, "id");
        this.loading = false;
      });
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.menuId,
        label: node.menuName,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.formName = "add";
      this.reset();

      if (row != null) {
        this.form.parentId = row.id;
        this.form.parentName = row.typeName;
      }

      this.open = true;
      this.title = "数据权限-新增";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    handleUpdate(row) {
      this.formName = "edit";
      this.reset();
      this.form = row;
      this.open = true;
      this.title = "数据权限-编辑";
    },


    submitForm: function () {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateStatus(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStatus(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
    ,
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除名称为"' + row.typeName + '"的数据项？').then(function () {
        let query = {
          id: row.id
        };
        return delStatus(query);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
  }
}
;
</script>
