<template>
  <el-dialog
    class="modify-marker-bind modify-marker-question"
    :visible.sync="modalIsShow"
    append-to-body
    top="10px"
    width="900px"
    title="绑定评卷员"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :show-close="false"
    @opened="visibleChange"
  >
    <el-row type="flex" :gutter="10">
      <el-col :span="12">
        <div class="marker-box">
          <div class="user-title">评卷员</div>
          <div class="user-search">
            <el-input
              v-model.trim="filterLabel"
              placeholder="请输入评卷员名称"
              clearable
              size="mini"
              prefix-icon="el-icon-search"
              @input="labelChange"
            ></el-input>
          </div>
          <div class="user-types">
            <div
              :class="['user-type', { 'is-active': isOrgTab }]"
              @click="switchUserType('org')"
            >
              组织架构
            </div>
            <div
              :class="['user-type', { 'is-active': !isOrgTab }]"
              @click="switchUserType('course')"
            >
              课程
            </div>
          </div>
          <div class="user-tree">
            <el-tree
              ref="UserTree"
              :data="userTree"
              node-key="id"
              :default-checked-keys="selectedUserIds"
              :props="defaultProps"
              :default-expand-all="!isOrgTab"
              :default-expanded-keys="isOrgTab ? userOrgKeys : []"
            >
              <span class="custom-tree-node" slot-scope="{ node, data }">
                <el-checkbox
                  v-if="data.isUser"
                  v-model="node.checked"
                  :disabled="checkNodeDisabled(data)"
                  @change="(val) => userChange(val, data)"
                >
                  {{ node.label }}（{{ data.loginName }}）
                </el-checkbox>
                <span v-else>{{ node.label }}</span>
                <div title="全选" @click.stop>
                  <el-checkbox
                    v-if="!data.isUser && data.children.length"
                    v-model="data.selected"
                    @change="(checked) => selectNodeAll(checked, data)"
                  ></el-checkbox>
                </div>
              </span>
            </el-tree>
          </div>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="marker-box marker-box-uq">
          <el-form
            ref="modalFormRef"
            :rules="rules"
            :model="{}"
            label-width="100px"
            label-position="top"
          >
            <el-form-item prop="users" label="已选评卷员：">
              <el-tag
                v-for="user in selectedUsers"
                :key="user.id"
                :closable="!checkNodeDisabled(user)"
                :disable-transitions="false"
                @close="toDeleteUser(user)"
              >
                {{ user.name }}（{{ user.loginName }}）
              </el-tag>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>

    <div class="marker-footer">
      <el-button type="primary" @click="confirm">确认</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>

    <div slot="footer"></div>
  </el-dialog>
</template>

<script>
import { deepCopy } from "@/plugins/utils";
import { organizationList } from "../../../base/api";
import { markMarkerBindList } from "../../api";

export default {
  name: "modify-mark-group",
  props: {
    data: {
      type: Object,
      default() {
        return {};
      },
    },
    courseId: {
      type: String,
      default: "",
    },
  },
  data() {
    const usersValidator = (rule, value, callback) => {
      if (!this.selectedUserIds.length) {
        callback(new Error("请选择评卷员"));
      } else {
        callback();
      }
    };

    return {
      modalIsShow: false,
      filterLabel: "",
      userType: "course",
      courseUsers: [],
      orgUsers: [],
      userTree: [],
      userList: [],
      selectedUsers: [],
      disabledUserIds: [],
      selectedUserIds: [],
      userOrgKeys: [this.$ls.get("orgId")],
      defaultProps: {
        children: "children",
        label: "label",
      },
      rules: {
        users: [
          {
            required: true,
            validator: usersValidator,
            trigger: "change",
          },
        ],
      },
    };
  },
  computed: {
    isOrgTab() {
      return this.userType === "org";
    },
  },
  mounted() {
    this.getOrgData();
  },
  methods: {
    async visibleChange() {
      this.filterLabel = "";
      this.userType = "course";
      const res = await markMarkerBindList(this.data);
      this.selectedUserIds = res || [];
      this.disabledUserIds = [...this.selectedUserIds];
      this.labelChange();

      this.$nextTick(() => {
        const users = this.$refs.UserTree.getCheckedNodes(true);
        this.selectedUsers = users;
      });
    },
    checkNodeDisabled(data) {
      return this.disabledUserIds.includes(data.id);
    },
    cancel() {
      this.modalIsShow = false;
      this.$emit("cancel");
    },
    open() {
      this.modalIsShow = true;
    },
    // user
    switchUserType(type) {
      this.filterLabel = "";
      this.userType = type;

      this.userTree =
        type === "org" ? deepCopy(this.orgUsers) : deepCopy(this.courseUsers);
      this.$refs.UserTree.setCheckedKeys(this.selectedUserIds);
    },
    async getOrgData() {
      let params = {
        specialPrivilege: "MARKER",
      };
      if (this.courseId) params.courseId = this.courseId;
      const data = await organizationList(params);
      this.parseUserData(data);
      this.getUserList();
    },
    parseUserData(data) {
      const parseUser = (list) => {
        return list.map((item) => {
          // org
          let nitem = {
            id: item.id,
            label: item.name,
            isUser: false,
            selected: false,
            children: [],
          };

          if (item["children"] && item["children"].length) {
            nitem.children = [...nitem.children, ...parseUser(item.children)];
          }
          // user
          if (item["sysUserList"] && item["sysUserList"].length) {
            let sysUserList = item.sysUserList;
            const users = sysUserList.map((user) => {
              const nuser = {
                id: user.id,
                userId: user.id,
                label: user.realName,
                name: user.realName,
                orgName: item.name,
                loginName: user.loginName,
                selected: false,
                isUser: true,
              };
              return nuser;
            });
            nitem.children = [...nitem.children, ...users];
          }

          if (item["courseUserList"] && item["courseUserList"].length) {
            nitem.courseUserList = item.courseUserList.map((user) => {
              return {
                id: user.id,
                userId: user.id,
                label: user.realName,
                name: user.realName,
                orgName: user.orgName,
                loginName: user.loginName,
                selected: false,
                isUser: true,
              };
            });
          }
          return nitem;
        });
      };
      this.orgUsers = parseUser(data);
      this.userTree = deepCopy(this.orgUsers);
      if (this.courseId && this.orgUsers[0].courseUserList) {
        this.courseUsers = deepCopy(this.orgUsers[0].courseUserList);
      }
      this.getUserList();
    },
    getUserList() {
      let userList = [];
      const fetchUser = (users) => {
        users.forEach((item) => {
          if (item["children"] && item["children"].length) {
            fetchUser(item.children);
          } else {
            if (item.isUser) {
              let nitem = { ...item };
              nitem.label = `${nitem.name}`;
              userList.push(nitem);
            }
          }
        });
      };
      fetchUser(this.orgUsers);

      this.userList = userList;
    },
    labelChange() {
      if (!this.filterLabel) {
        this.switchUserType(this.userType);
      } else {
        const escapeRegexpString = (value = "") =>
          String(value).replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");
        const reg = new RegExp(escapeRegexpString(this.filterLabel), "i");

        if (this.userType === "org") {
          this.userTree = this.userList.filter((item) => reg.test(item.name));
        } else {
          this.userTree = this.courseUsers.filter((item) =>
            reg.test(item.name)
          );
        }
      }
      this.$refs.UserTree.setCheckedKeys(this.selectedUserIds);
    },
    selectNodeAll(checked, data) {
      let users = [];
      const getUserIds = (list) => {
        list.forEach((item) => {
          item.selected = checked;
          if (item.children && item.children.length) {
            getUserIds(item.children);
          } else {
            if (item.isUser) users.push(item);
          }
        });
      };
      getUserIds(data.children);

      const userIds = users.map((u) => u.id);
      const selectedUserIds = this.selectedUsers.map((item) => item.id);

      let deleteUserIds = [];
      userIds.forEach((userId, uindex) => {
        const userPos = selectedUserIds.indexOf(userId);
        const includeUser = userPos !== -1;
        if (checked) {
          if (!includeUser) this.selectedUsers.push(users[uindex]);
        } else {
          if (includeUser) {
            deleteUserIds.push(userId);
          }
        }
      });
      this.selectedUsers = this.selectedUsers.filter(
        (u) => !deleteUserIds.includes(u.id)
      );
      this.selectedUserIds = this.selectedUsers.map((item) => item.id);
      this.$refs.UserTree.setCheckedKeys(this.selectedUserIds);
      this.$refs.modalFormRef.validateField("users");
    },
    updateSelectedUsersFromUserIds() {
      this.selectedUsers = this.userList.filter((user) =>
        this.selectedUserIds.includes(user.id)
      );
    },
    userChange(checked, user) {
      if (checked) {
        this.selectedUsers.push(user);
      } else {
        this.selectedUsers = this.selectedUsers.filter(
          (item) => item.id !== user.id
        );
      }
      this.selectedUserIds = this.selectedUsers.map((item) => item.id);
      this.$refs.modalFormRef.validateField("users");
    },
    toDeleteUser(user) {
      const pos = this.selectedUsers.findIndex((item) => item.id === user.id);
      this.selectedUsers.splice(pos, 1);
      this.selectedUserIds = this.selectedUsers.map((item) => item.id);
      this.$refs.UserTree.setCheckedKeys(this.selectedUserIds);
      this.$refs.modalFormRef.validateField("users");
    },
    // confirm
    async confirm() {
      const valid = await this.$refs.modalFormRef.validate().catch(() => {});
      if (!valid) return;

      this.$emit(
        "modified",
        this.selectedUsers.map((item) => {
          return {
            id: item.id,
            name: item.name,
          };
        })
      );
      this.modalIsShow = false;
    },
  },
};
</script>
