<template>
  <el-dialog custom-class="custom-dialog" class="border" :border="false" closeFree width="600px"
             :title="title" :visible.sync="visible" v-model="visible" append-to-body :close-on-click-modal="true"
             :destroy-on-close="true">
    <div>
      <div class="picker">
        <div class="candidate" v-loading="loading">
          <div style="padding: 5px 8px;">
            <el-input v-model="filterText" style="width: 100%;" size="small"
                      clearable placeholder="输入关键字进行过滤" prefix-icon="Search"/>
          </div>
          <!-- 部门 check-strictly-->
          <el-empty :image-size="100" description="似乎没有数据" v-show="deptList.length === 0"/>
          <el-scrollbar style="height:350px">
            <el-tree :data="deptList" ref="tree" :props="deptProps" empty-text="" node-key="value" default-expand-all
                     :show-checkbox="showCheckbox" highlight-current :check-strictly="multiple===false"
                     @check-change="handleCheckChange" @node-click="(node,check)=>handle(node,check)"
                     :filter-node-method="filterNode">
              <template #default="{ node, data }">
                <el-icon style="margin-right: 5px">
                  <FolderOpened/>
                </el-icon>
                {{ node.label }}
              </template>
            </el-tree>
          </el-scrollbar>
        </div>
        <div class="selected">
          <div class="count">
            <span>已选 {{ selectList.length }} 项</span>
            <span @click="clearSelected">清空</span>
          </div>
          <div class="org-items" style="height: 350px;">
            <el-empty :image-size="100" description="请点击左侧列表选择数据" v-show="selectList.length === 0"/>
            <div v-for="(selectItem, selectIndex) in selectList" :key="selectIndex" class="org-item">
              <i class="el-icon-folder-opened"></i>
              <span>{{ selectItem.label }}</span>
              <i class="el-icon-close" @click="noSelected(selectItem)" v-if="showCheckbox===false"></i>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="visible = false">取 消</el-button>
      <el-button size="mini" type="primary" @click="selectConfirm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script setup>
import {getDepartmentTree} from "@/api/workflow/process-user";
import {computed, defineExpose, defineProps} from "vue";
import {ElMessage,ElMessageBox} from "element-plus";

const emit = defineEmits(["input"]);
const props = defineProps({
  value: {
    type: Array,
    default: () => {
      return [];
    }
  },
  multiple: { //是否多选
    default: true,
    type: Boolean
  },
  showCheckbox: { //是否显示左侧选择框
    default: true,
    type: Boolean
  }
});
const visible = ref(false);
const loading = ref(false);
const title = ref("请选择");
const selectList = ref([]);
const deptList = ref([]);
const filterText = ref("");
const tree = ref([]);
const deptProps = reactive({
  value: "value",
  label: "label",
  children: "children"
});
watch(() => filterText, (newVal, oldVal) => {
  tree.value.filter(newVal);
});

const _value = computed({
  get() {
    return props.value;
  }
  ,
  set(val) {
    emit("input", val);
  }
});
const getDepartmentTreeList = () => {
  //获取部门信息
  getDepartmentTree().then(res => {
    deptList.value = res.data;
    // console.log("获取部门信息===========", res.data);
  });
};
const filterNode = (value, data) => {
  //通过关键字过滤树节点
  if (!value) return true;
  return data.deptName.indexOf(value) !== -1;
};
const showDeptPicker = () => {
  //用于弹开部门选择
  visible.value = true;
};
/**
 * 选中部门
 * @param data 选择的每个节点item
 * @param checked 是否选中
 */
const handleCheckChange = (data, checked) => {
  // 左侧有选择框
  if (props.showCheckbox) {
    // 左侧有选择框 + 多选
    if (props.multiple) {
      //不添加重复的数据到右边
      for (let i = 0; i < selectList.value.length; i++) {
        if (selectList.value[i].value === data.value) {
          selectList.value.splice(i, 1);
          break;
        }
      }
      if (checked) {
        if (data.children === undefined) {
          selectList.value.push(data);
        }
      } else if (data === "1") {
        tree.value.setCheckedKeys([]);
        selectList.value = [];
      }
    } else {// 左侧有选择框 + 单选
      //不添加重复的数据到右边
      for (let i = 0; i < selectList.value.length; i++) {
        if (selectList.value[i].value === data.value) {
          selectList.value.splice(i, 1);
          break;
        }
      }
      if (checked) {
        tree.value.setCheckedNodes([data]);
        // this.$refs.tree.setCheckedKeys([]);
        selectList.value = [data];
      } else if (data === "1") {
        selectList.value = [];
        tree.value.setCheckedKeys([]);
      }
    }
  }
  // _value.value = selectList.value
};

//左侧没有选择框时,点击tree-item
/**
 * 可以点击树节点deptName,进行选择
 * @param node 选择的每个节点item
 * @param check checked(checkbox选择框)是否选中
 */
const handle = (node, check) => {
  if (check.isLeaf !== false) {
    if (props.multiple) {
      //不添加重复的数据到右边
      for (let i = 0; i < selectList.value.length; i++) {
        if (selectList.value[i].value === node.value) {
          selectList.value.splice(i, 1);
          break;
        }
      }
      check.checked = true;
      selectList.value.push(node);
    } else {
      check.checked = true;
      selectList.value = [node];
    }
  }
  // _value.value = selectList.value
};
const noSelected = (selectItem) => {
  //左侧无选择框时,右侧显示×
  for (let i = 0; i < selectList.value.length; i++) {
    if (selectList.value[i].value === selectItem.value) {
      selectList.value.splice(i, 1);
      tree.value.setCheckedKeys(i);
      break;
    }
  }
  if (props.showCheckbox) {
    // 左侧有选择框 + 单选
    if (props.multiple === false) {
      tree.value.setCheckedKeys([]);
    }
  }
};
//清空
const clearSelected = () => {
  ElMessageBox.confirm("您确定要清空已选中的项?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    if (!props.showCheckbox) {
      selectList.value = [];
    } else {
      handleCheckChange("1");
    }
  });
};
//确定按钮
const selectConfirm = () => {
  emit("ok", selectList.value);
  visible.value = false;
};
defineExpose({
  showDeptPicker
});
getDepartmentTreeList();
</script>

<style lang="scss" scoped>
$containWidth: 264px;

///deep/ .el-tree-node {
//  .is-leaf + .el-checkbox .el-checkbox__inner {
//    display: inline-block;
//  }
//
//  .el-checkbox .el-checkbox__inner {
//    display: none;
//  }
//}

.el-dialog__body {
  padding: 10px 20px;
}

.picker {
  height: 402px;
  position: relative;
  text-align: left;

  .candidate {
    left: 0;
    top: 0;
  }
}

.candidate, .selected {
  position: absolute;
  display: inline-block;
  width: $containWidth;
  height: 400px;
  border: 1px solid #e8e8e8;
}

.selected {
  border-left: none;
  right: 0;
  top: 0;

  .count {
    width: calc(var($containWidth) - 20px);
    padding: 10px;
    display: inline-block;
    border-bottom: 1px solid #e8e8e8;
    margin-bottom: 5px;

    & > span:nth-child(2) {
      float: right;
      color: #c75450;
      cursor: pointer;
    }
  }
}

.org-items {
  overflow-y: auto;
  height: 350px;

  .el-icon-close {
    position: absolute;
    right: 5px;
    cursor: pointer;
    font-size: larger;
  }

  .org-item {
    margin: 0 5px;
    border-radius: 5px;
    position: relative;
    padding: 7px 5px;
    display: flex;
    align-items: center;

    cursor: pointer;

    &:hover {
      background: #f1f1f1;
    }

    > span {
      margin-left: 5px;
    }
  }
}

.el-scrollbar .el-scrollbar__wrap {
  overflow-x: hidden;
}

::-webkit-scrollbar {
  float: right;
  width: 4px;
  height: 4px;
  background-color: white;
}

::-webkit-scrollbar-thumb {
  border-radius: 16px;
  background-color: #efefef;
}

</style>
