<template>
  <div class="permissions">
    <div class="title">权限设置</div>
    <div class="now flex-vertical-center-l">当前选中用户：{{$route.query.name}}</div>
    <div class="tree">
      <el-tree
        ref="tree"
        :data="menuData"
        :default-checked-keys="menuRight"
        node-key="id"
        :props="menuProps"
        show-checkbox
        @check="handleCheckChange"
      ></el-tree>
    </div>
    <div class="flex-vertical-center-l">
      <div class="save flex-center" @click.once="setPermToRole()">保存</div>
      <div class="back flex-center" @click="$router.back()">返回</div>
    </div>
  </div>
</template>
<script>
import { getPermissionsMenu, setPermToRole } from "@/api/system.js";
export default {
  data() {
    return {
      menuProps: {
        children: "list",
        label: "title"
      },
      count: 1,
      menuData: [],
      menuRight: [],
      checked: []
    };
  },
  created() {
    this.getPermissionsMenu();
  },
  methods: {
    handleCheckChange(data, checked) {
      this.checked = checked.checkedKeys;
    },
    dealKeys() {
      let data = [];
      // this.checked.forEach(item=>{
      //     this.menuData.forEach(item1=>{
      //         if(item==item1.)
      //     })
      // })
    },
    // 获取权限菜单信息
    async getPermissionsMenu() {
      let params = {
        RoleID: this.$route.query.id
      };
      let res = await getPermissionsMenu(params);
      if (res.ResultCode == 0) {
        res.Data.RoleRight = res.Data.RoleRight.split(",");
        this.menuData = res.Data.MenuAll;
        this.menuRight = res.Data.RoleRight;
      }
    },
    // 设置权限
    async setPermToRole() {
      // console.log();
      // return

      this.checked = this.checked.filter(item => {
        return item.length > 1;
      });
      let params = {
        roleID: this.$route.query.id,
        roleRight: this.checked.join(",")
      };
      const loading = this.$loading({
        lock: true,
        text: "正在保存",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      let res = await setPermToRole(params).catch(err => {
        loading.close();
      });

      if (res.ResultCode == 0) {
        loading.close();
        this.$message({
          message: "提交成功",
          type: "success",
          showClose: true
        });
        this.$parent.$children[0].getMenuPermiss();
        setTimeout(() => {
          this.$router.back();
        }, 2000);
      }
    }
  }
};
</script>
<style lang="scss" scoped>
@import "../../style/common.scss";

.permissions {
  height: 100%;
  overflow: auto;
  box-sizing: border-box;
  padding: 10px 10px;
  background: #ffffff;
  border-radius: 5px;

  .title {
    font-size: 16px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #333333;
  }

  .now {
    margin-top: 10px;
    height: 54px;
    background: #f5f5f5;
    border-left: 6px solid #5386f6;
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #333333;
    padding-left: 10px;
  }

  .tree {
    margin-top: 10px;
  }

  .save {
    margin-top: 20px;
    width: 74px;
    height: 36px;
    background: #5386f6;
    border-radius: 4px;
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #ffffff;
    cursor: pointer;
  }

  .back {
    margin-top: 20px;
    width: 74px;
    height: 36px;
    background: #ffae45;
    border-radius: 4px;
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #ffffff;
    cursor: pointer;
    margin-left: 20px;
  }
}
</style>