let modules;
import {treeScolly} from "@base/utils/global.js";

modules = {
  props: {
    visiable: Boolean,
    option: Object
  },

  components: {},
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
  },
  data() {
    return {
      showDialog: true,
      filterText: "",
      defaultProps: {
        children: "children",
        label: "fileName",
        isLeaf: "leaf",
      },
      showEditCategoryDialog: false,
      editCategory: {},
      currentFileCategory: null,
      rootCategory: false,
      categoryPageSize: 1000,
      treeNodeArr: [],
      currentNode: null,
      formData: {},
      showBillDialog: false,
      billDialogContent: null,
      checkBillDatas: [],
      currentUser: {},
      treeFlag: false,
      title: null
    };
  },
  computed: {
    storeAreaCode() {
      return this.option.storeAreaCode;
    },
    current_prefix() {
      return this.option.prefix;
    },
    isPrivate() {
      return this.option.private || false;
    },
    dialogTitle() {
      let titleMap = {
        0: this.$t2('文件还原', 'components.fileLibrary.revertFile'),
        1: this.$t2('移动文件', 'components.fileLibrary.moveFile'),
        2: this.$t2('复制文件', 'components.fileLibrary.copyFile')
      };
      return titleMap[this.handleType];
    },
  },
  created() {

  },
  mounted() {
    this.initTitle();
    this.$nextTick(() => {
      let f = ".nextDom";
      let nextDom = this.$el.querySelector(f);
      treeScolly({
        target: this,
        type: "selectDialog",
        nextDom: nextDom,
      });
    });
  },
  methods: {
    initTitle() {
      this.$http({
        method: "post",
        url: USER_PREFIX + "/file_store_area/getByCode",
        data: {
          stringOne: this.storeAreaCode,
        },
        isLoading: true,
        success: (res) => {
          let fileStoreArea = res.objx;
          if (fileStoreArea) {
            this.title = fileStoreArea.storeAreaName;
            this.getCurrentUser();
          } else {
            this.$baseAlert(this.$t2('文档工作区未维护', 'components.fileLibrary.fileStoreAreaTip'));
          }
        },
      });
    },
    getCurrentUser() {
      return this.$http({
        url: USER_PREFIX + "/user/currentUser",
        method: "post",
        isLoading: true,
        success: (res) => {
          this.currentUser = res.objx;
          this.treeFlag = true;
        },
      });
    },
    changeCategory(row, node) {
      this.handleNodeData(node);
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.categoryName.indexOf(value) !== -1;
    },
    handleNodeClick(data, node, v) {
      if (!data.moreBtn) {
        this.changeCategory(data, node);
      } else {
        this.showModeCategory(data, node, v);
      }
    },
    showModeCategory(data, node, v) {
      let param = {
        parent: data.parent,
        dirs: true,
        storeAreaCode: this.storeAreaCode,
        size: this.categoryPageSize,
        current: data.next,
      };
      if (this.isPrivate) {
        param.objectForeignId = [this.currentUser.id];
        param.privateDir = 1;
      }
      this.$http({
        url: this.current_prefix + "/file_obj/listPage",
        method: "post",
        data: param,
        isLoading: true,
        success: (res) => {
          let page = res.objx;
          let rows = page && page.records ? page.records : [];
          this.$refs.tree.remove(node);
          if (page.pages > page.current) {
            rows.push({
              parent: param.parent,
              fileName: this.$t2('更多', 'components.fileLibrary.more'),
              moreBtn: true,
              leaf: true,
              pages: page.pages,
              current: page.current,
              next: page.current + 1,
            });
          }
          rows.forEach((value) => {
            value.leaf = !value.hasChild;
          });
          rows.forEach((row) => {
            this.$refs.tree.append(row, param.parent);
          });
        },
      });
    },
    // 异步树叶子节点懒加载逻辑
    loadNode(node, resolve) {
      let id = node && node.data && node.data.id ? node.data.id || "" : "";
      let url, param;
      url = this.current_prefix + "/file_obj/listPage";
      if (!id) {
        param = {
          dirs: true,
          storeAreaCode: this.storeAreaCode,
          size: this.categoryPageSize,
        };
      } else {
        param = {
          dirs: true,
          parent: id,
          storeAreaCode: this.storeAreaCode,
          size: this.categoryPageSize,
        };
      }
      if (this.isPrivate) {
        param.objectForeignId = [this.currentUser.id];
        param.privateDir = 1;
      }
      this.$http({
        url: url,
        method: "post",
        data: param,
        success: (res) => {
          let page = res.objx;
          let rows = page && page.records ? page.records : [];
          if (page.pages > page.current) {
            rows.push({
              parent: param.parent,
              fileName: this.$t2('更多', 'components.fileLibrary.more'),
              moreBtn: true,
              leaf: true,
              pages: page.pages,
              current: page.current,
              next: page.current + 1,
            });
          }

          // moreBtn
          rows.forEach((value) => {
            value.leaf = !value.hasChild;
          });
          if (node.level === 0) {
            setTimeout(() => {
              this.$refs.tree.setCurrentKey(node.childNodes[0].data);
              this.handleNodeData(node.childNodes[0]);
              node.childNodes[0].expand();
            }, 200);
          }
          let items = [];
          if (node.level === 0) {
            let item = {
              parent: null,
              fileName: this.title,
              leaf: rows.length == 0,
              allFileBtn: true,
              id: 0,
              children: rows,
            };
            items.push(item);
          } else {
            items = rows;
          }
          resolve(items);
        },
      });
    },
    handleNodeData(node) {
      let treeNodeArr = [];
      let tree = this.$refs.tree;
      // let firstNode = tree.root.childNodes[0];
      if (node) {
        treeNodeArr.push(node);
        let end = false;
        let pNode = node;
        while (!end) {
          pNode = pNode.parent;
          if (pNode && pNode.data) {
            let pData = pNode.data;
            treeNodeArr.push(pNode);
            end = pNode.level <= 1;
          } else {
            end = true;
          }
        }
        treeNodeArr = treeNodeArr.reverse();
      }
      /*treeNodeArr.unshift(firstNode)
      if (!node) {
        node = firstNode;
      }*/
      this.currentFileCategory = node && node.data ? node.data : null;
      this.currentNode = node;
      this.$set(this, "treeNodeArr", treeNodeArr);
    },
    doNav(t) {
      let node = this.$refs.tree.getNode(t.data.id + "");
      this.$refs.tree.setCurrentKey(node.data);
      this.handleNodeClick(node.data, node, this);
    },
    dialogClose() {
      this.$emit("update:visiable", false);
    },
    dialogPrimary() {
      if (!this.currentFileCategory) {
        this.$message({
          message: this.$t2('请选择文件夹', 'components.fileLibrary.pleaseChooseCategory'),
          type: "error",
          duration: 3000,
        });
        return;
      }
      if (this.currentFileCategory.id == 0) {
        let fileName = this.title;
        this.$message({
          message: this.$t2("文件路径[" + fileName + "]不允许操作", 'components.fileLibrary.categoryAuthTip', {fileName: fileName}),
          type: "error",
          duration: 3000,
        });
        return;
      }

      if (!this.currentFileCategory.hasAuth) {
        let fileName = this.currentFileCategory.fileName;
        this.$message({
          message: this.$t2("文件路径[" + fileName + "]不允许操作", 'components.fileLibrary.categoryAuthTip', {fileName: fileName}),
          type: "error",
          duration: 3000,
        });
        return;
      }

      let categoryArr = this.treeNodeArr.map((item) => item.data);
      this.$emit("confirm", this.currentFileCategory, categoryArr);
      this.dialogClose();
    }
  }
};
export default modules;
