<template>
   <PageSpace>
    <template v-slot:default="{adjustHeight}">
    <SplitPanel>
      <OrgEditor
        slot="left"
        ref="orgtree"
        :initData="initData"
        :metainfo="metainfo"
        @controlctl="controlctl"
        @drawaction="drawaction"
        @modisave="orgsave"
        @deletenode="orgsave"
        @onselnode="onselnode"
      ></OrgEditor>
      <UserInfo
        ref="userinfo"
        slot="right"
        :tblheight="adjustHeight-60"
        :beforesave="beforesave"
        :beforedit="beforedit"
        :beforeadd="beforeadd"
      ></UserInfo>
    </SplitPanel>
        </template>
  </PageSpace>
</template>
  <script>
import { getApp, saveAuths, findAuthsByRoles } from "@/api/meta";
import PageSpace from "@/view/components/pagespace/page_space.vue";
import { saveFormData, getOrgs } from "@/api/data";
import OrgEditor from "@/view/components/tables/forms/jsoneditor.vue";
import SplitPanel from "@/view/components/split-pane2/split-pane2.vue";
import UserInfo from "./userinfo_page.vue";
import config from "@/config";
export default {
  name: "treeinfo_page",
  props: {},
  components: {
    OrgEditor,
    SplitPanel,
    UserInfo,
    PageSpace
  },
  data() {
    return {
      metainfo: {
        name: "节点编辑",
        main: [
          {
            title: "授权信息",
            key: "baseinfo",
            cols: 2,
            ctls: [
              {
                type: "switch",
                label: "是否岗位",
                prop: "isPosition",
                opentext: "是",
                closetext: "否",
                trurcolor: "",
                falsecolor: "",
                placeHolder: "",
                style: ""
              },
              {
                type: "switch",
                label: "是否负责人",
                prop: "isMain",
                opentext: "是",
                closetext: "否",
                trurcolor: "",
                falsecolor: "",
                placeHolder: "",
                style: "",
                isHide: true
              },
              {
                type: "input",
                label: "监控",
                prop: "orgtag",
                placeHolder: "输入监控的部门编码（非岗位），逗号分隔",
                style: "",
                rules: [],
                isHide: true
              },
              {
                type: "model-select",
                refModel: "auth.role",
                isMulti: true,
                label: "角色",
                prop: "roles",
                labelField: "name",
                valueField: "id",
                style: { "margin-left": "10px", width: "165px" },
                isHide: true
              }
            ]
          }
        ],
        lists: null
      }
    };
  },
  mounted() {
 
  },
  methods: {
    onselnode(root,node,data){
        this.$refs.userinfo.searchWhere({opath:data.code})
    },
    orgsave(treedata, modidata) {
      saveFormData("/web/common/companyCtl/setOrgs", {
        orgJson: treedata,
        curdata:modidata
      }).then(res => {
        let rtn = res.data;
        console.log("ddddddddddddddddd", rtn);
        if (rtn.status == 0) {
          let treedata2 = rtn.data.orgJson;
          console.log("save org...............................", treedata2);
          this.$refs.orgtree.treedata = treedata2;
          this.$refs.orgtree.refreshTree();
          this.$Message.success("当前操作成功完成")
        } else {
        }
      });
    },
    beforeadd(row, cbk) {
      if (!this.$refs.orgtree.currentData) {
        return cbk({ value: false, message: "请选择岗位节点" });
      } else {
        if (!this.$refs.orgtree.currentData.isPosition) {
          return cbk({ value: false, message: "请选择岗位节点" });
        } else {
          return cbk({ value: true, message: null });
        }
      }
    },
    beforedit(row, cbk) {
      return cbk({ value: true, message: null });
      // if(!this.$refs.orgtree.currentData){
      //   return cbk({value:false,message:"请选择组织结构"})
      // }else{
      //   return cbk({value:true,message:null})
      // }
    },
    beforesave(key, row, cbk) {
      //对row添加组织结构需要的字段
      if (
        this.$refs.orgtree.currentData &&
        this.$refs.orgtree.currentData.isPosition
      ) {
        row.opath=this.$refs.orgtree.currentData.orgpath
        row.ptags = this.$refs.orgtree.currentData.orgtag;
      }
      console.log("before.save", key, row);
      return cbk(row);
    },
    setHeight() {
      let $orgdiv = $("#orgdiv");
      let el = $orgdiv.get()[0];
      let h = window.innerHeight - el.offsetTop;
      $orgdiv.css("height", h + "px");
      this.$refs.userinfo.setHeight(h - 120);
    },
    drawaction(v, fm) {
      if (fm.formModel.isPosition) {
        fm.setCtlVisable("isMain", true);
        fm.setCtlVisable("orgtag", true);
        fm.setCtlVisable("roles", true);
      } else {
        fm.setCtlVisable("isMain", false);
        fm.setCtlVisable("orgtag", false);
        fm.setCtlVisable("roles", false);
      }
    },
    controlctl(v, prop, fm) {
      if (prop == "isMain") {
        let cnode = this.$refs.orgtree.currentNode;
        let pnode = this.$refs.orgtree.findParentByNode(cnode);
        let fmodel = fm.formModel;
        if (fmodel.orgtag && fmodel.orgtag.indexOf(pnode.code) < 0) {
          let ttmp = fmodel.orgtag
            .split(",")
            .push(pnode.code)
            .join(",");
          fmodel.orgtag = ttmp;
        } else {
          fmodel.orgtag = pnode.code;
        }
      }
      if (prop == "isPosition") {
        if (v) {
          fm.setCtlVisable("isMain", true);
          fm.setCtlVisable("orgtag", true);
          fm.setCtlVisable("roles", true);
        } else {
          fm.setCtlVisable("isMain", false);
          fm.setCtlVisable("orgtag", false);
          fm.setCtlVisable("roles", false);
        }
      }
    },
    initData(cbk) {
      getOrgs().then(res => {
        let rtn = res.data;
        if (rtn.status == 0) {
          let treedata2 = rtn.data.orgJson;
          cbk(treedata2);
        }else{
          throw new Error("获取组织数据出错")
        }
      });
    }
  }
};
</script>
  <style>
</style>
