<template>
  <div class="conent">
    <!-- 新增按钮 -->
    <div class="btn">
      <el-button type="primary" @click="dialogVisible2 = true">新增</el-button>
    </div>

    <!-- 点击新增弹出表单 -->
    <div class="from">
      <el-dialog
        title="新增角色"
        :visible.sync="dialogVisible2"
        width="30%"
        :close-on-click-modal="false"
      >
        <div>
          <el-form label-width="80px">
            <el-form-item label="角色名称">
              <el-input v-model="newrole"></el-input>
            </el-form-item>
            <div class="from-btn">
              <el-button type="danger" @click="dialogVisible2 = false"
                >取消</el-button
              >
              <el-button @click="onSubmit">确认</el-button>
            </div>
          </el-form>
        </div>
      </el-dialog>
    </div>

    <!-- 角色表单 -->
    <div class="table">
      <el-table :data="tableList" style="width: 100%">
        <el-table-column prop="id" label="ID"> </el-table-column>
        <el-table-column prop="name" label="角色名称"> </el-table-column>
        <el-table-column label="权限内容">
          <template slot-scope="scope">
            <span v-for="item in scope.row.permissions" :key="item.id"
              >{{ item.name }}&nbsp;&nbsp;</span
            >
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="onEdit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 点击编辑弹出权限框 -->
    <el-dialog
      title="编辑权限"
      :visible.sync="dialogVisible"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-tree
        v-if="isShowtree"
        :data="Role_Permission"
        show-checkbox
        :default-expand-all="false"
        node-key="id"
        ref="tree"
        highlight-current
        :props="defaultProps"
        :default-checked-keys="chekedKeys"
        v-model="permissionIds"
      >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="getCheckedKeys()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  roleList,
  roleAdd,
  jurisdiction,
  jurisdictionEdit,
} from "../../api/role.js";
export default {
  data() {
    return {
      id: "", //点击角色的id
      tableList: [], //列表数据
      rolePermissions: [],
      newrole: "", //添加角色框input
      dialogVisible: false, //编辑按钮打开表单变量
      dialogVisible2: false, //新增按钮打开表单变量
      permissionIds: [], //复选框选中权限的ID
      allpermissionIds: {}, //id数组添加进对象
      Role_Permission: [], //权限列表数据
      defaultProps: {
        children: "children",
        label: "name",
      },
      isShowtree: false,
      chekedKeys: [],
    };
  },

  mounted() {
    this.getroleList();
    this.getJurisdiction();
  },
  methods: {
    //角色表单数据请求
    getroleList() {
      roleList().then((res) => {
        this.tableList = res.data.data;
        console.log("我是角色权限", this.tableList);
      });
    },

    //角色权限数据请求
    getJurisdiction() {
      jurisdiction().then((res) => {
        this.Role_Permission = res.data.data;
        console.log("我是所有权限", this.Role_Permission);
      });
    },

    // 点击新增
    onSubmit() {
      let roleName = {
        name: this.newrole,
      };
      roleAdd(roleName).then((res) => {
        this.dialogVisible2 = false;
        this.getroleList();
        if (res.status == 201) {
          this.$message({
            message: "角色添加成功！",
            type: "success",
          });
        }
      });
    },

    // 点击编辑
    onEdit(row) {
      this.id = row.id;
      console.log(row);
      let arr = row.permissions.map((item) => {
        return item.id;
      });
      this.chekedKeys = arr;
      this.dialogVisible = true;
      this.isShowtree = true;
    },

    // 点击取消
    cancel() {
      this.dialogVisible = false;
      this.isShowtree = false;
    },

    //编辑权限确认
    getCheckedKeys() {
      let id = this.id; //选中角色的ID
      this.permissionIds = this.$refs.tree.getCheckedKeys(); //选中权限的ID
      this.allpermissionIds = {
        permissionIds: this.permissionIds,
      };
      jurisdictionEdit(id, this.allpermissionIds).then((res) => {
        if (res.status === 200) {
          this.dialogVisible = false;
          this.getroleList();
          this.$message({
            message: "权限修改成功！",
            type: "success",
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.conent {
  width: 100%;
  min-height: calc(100vh - 200px);
}
.btn {
  height: 104px;
  border-radius: 5px;
  display: flex;
  align-items: center;
  .el-button {
    width: 114px;
    height: 44px;
    border-radius: 3px;
  }
}
.table {
  margin-top: 20px;
}

.from-btn {
  display: flex;
  justify-content: space-around;
  align-content: center;
}
</style>
