<template>
  <!-- 组织配置信息-->
  <el-dialog title="配置信息" :visible.sync="authData.visible">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane
        :label="authData.currOrgGroupName + '组织角色资源'"
        name="orgGroupRes"
      >
        <el-form
          ref="orgGroupResForm"
          label-position="right"
          label-width="100px"
          style="width: 400px; margin-left:50px;"
        >
          <el-form-item label="资源">
            <el-tree
              :data="resTree"
              :props="defaultProps"
              :show-checkbox="!systemFlag"
              node-key="resId"
              ref="resTree"
              highlight-current
              default-expand-all
              style="background-color: #ecf5ff"
            >
            </el-tree>
          </el-form-item>
        </el-form>
        <el-button
          v-if="!systemFlag"
          v-has="'sysOrgGroupResRel:save'"
          type="primary"
          @click="handleOrgGroupResSave()"
          style="float: right"
          >确认</el-button
        >
      </el-tab-pane>
    </el-tabs>
  </el-dialog>
</template>

<script>
import waves from "@/directive/waves"; // 水波纹指令
import { getOrgGroupResTree, saveOrgGroupRes } from "@/api/auth/orgGroup";
export default {
  name: "authDialog",
  directives: {
    waves
  },
  props: ["authData"],
  data() {
    return {
      resTree: null, //资源树
      activeName: "orgGroupRes",

      defaultProps: {
        label: "name",
        isLeaf: "leaf"
      },
      isCategoryAll: null,
      categoryCheckAll: null,
      checkedCategorys: null,
      topCategoryList: null,
      systemFlag: null
    };
  },
  created() {},
  watch: {
    "authData.currOrgGroupId": {
      handler: function(val, oldVal) {
        if (val != null) {
          this.orgGroupResTree();
        }
      },
      immediate: true
    }
  },
  methods: {
    handleClick() {
      //切换tab
    },
    orgGroupResTree() {
      //资源树
      let query = {
        orgGroupId: this.authData.currOrgGroupId
      };
      getOrgGroupResTree(query).then(response => {
        this.resTree = response.data.list;
        this.systemFlag = response.data.system;
        //获取button是否选中
        if (
          response.data.selectedList != null &&
          response.data.selectedList.length > 0
        ) {
          this.$refs.resTree.setCheckedKeys(response.data.selectedList);
        }
      });
    },
    handleOrgGroupResSave() {
      //保存组织角色 对应 的资源
      var checkedKeys = this.$refs.resTree
        .getCheckedKeys(false)
        .concat(this.$refs.resTree.getHalfCheckedKeys());
      var orgGroupResData = {
        orgGroupId: this.authData.currOrgGroupId,
        resIdSet: checkedKeys
      };
      console.log(orgGroupResData);
      // -> /sysOrgGroupResRel/save
      saveOrgGroupRes(orgGroupResData).then(() => {
        this.authData.visible = false;
        this.$notify({
          title: "成功",
          message: "更新成功",
          type: "success",
          duration: 3000
        });
        //重新加载
        this.$emit("refresh");
      });
    }
  }
};
</script>

<style scoped></style>
