<template>
  <el-dialog
    v-dialogDrag
    :title="title"
    :visible="show"
    width="965px"
    @close="close"
    :close-on-click-modal="false"
    append-to-body
    destroy-on-close
  >
    <PlatformFunction
      ref="platform"
      :currentUser="currentUser"
      @callback="changeClose"
    ></PlatformFunction>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmAuth()">保 存</el-button>
      <el-button @click="close()">关 闭</el-button>
    </div>
  </el-dialog>
</template>
<script>
import PlatformFunction from "./PlatformFunction.vue";
import { AddAuthListApi } from "@/api/staffApi.js";

export default {
  watch: {
    showdialog() {
      this.dialogFormVisible = this.showdialog;
      this.defaulDataParent = [];
    },
  },
  components: {
    PlatformFunction,
  },
  data() {
    return {
      title: "",
      dialogFormVisible: this.showdialog,
      OperType: 0, //0 新增  1修改
      formList: {},
      params: {
        //搜索条件
        pageIndex: 1,
        pageSize: 10,
        keyword: "",
        total: 0,
      },
      rules: {
        // employeeId: [{required: true, message: "请选择用户", trigger: "blur" }],
      },
      defaulDataParent: "", //上级部门默认回选数据
      defaulData: [],
      tableDataEmployee: [],
      resultArr: [], //选择用户下拉得到对应名称数组
      showdialogMoveSuper: false,
      fileList: [],
      disabledUser: false,
      multipleArr: [],
      AuthAppRoledata: [], //默认查询应用角色列表树
      defaultProps: {
        children: "responses",
        label: "name",
      },
      rolecheck: false,
      PrevRoleIds: [],
      AuthAppRoledataAll: [], //显示所有应用列表以及二级角色列表
      show: false,
      currentUser: {},
    };
  },
  methods: {
    // 打开
    open(data, type, isRole) {
      this.show = true;

      setTimeout(() => {
        this.title = `功能权限设置（${data.map((e) => e.userName).join(",")}）`;
        this.currentUser = {
          employeeIds: isRole ? undefined : data.map((e) => e.employeeId),
          searchType: 2,
          roleId: isRole ? data[0]?.roleId : undefined,
        }; //可能根据权限设置 也可能根据人来设置
      });
    },
    close() {
      this.show = false;
    },
    getTreeData(arr) {
      //递归遍历部门
      arr.forEach((item) => {
        item.label = item.name;
        item.value = item.id;
        if (item.children && item.children.length < 1) {
          item.children = undefined;
        } else if (item.children && item.children.length > 0) {
          this.getTreeData(item.children);
        }
      });
    },
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let employeeIds = [];
          this.multipleArr.forEach((item) => {
            employeeIds.push(item.id);
          });
          let roleIds = [];
          if (this.$refs.tree.getCheckedNodes().length) {
            this.$refs.tree.getCheckedNodes().forEach((item) => {
              if (item.dataType) {
                roleIds.push(item.id);
              }
            });
          } else {
            this.$message({ type: "warning", message: "请选择应用角色权限" });
            return;
          }
          let obj2 = { domainId: this.$route.query.domainId };

          let GetShowFalsePrevRoleIds = []; //获取隐藏选中的角色id
          this.PrevRoleIds.length &&
            this.PrevRoleIds.forEach((item) => {
              this.AuthAppRoledataAll.length &&
                this.AuthAppRoledataAll.forEach((its) => {
                  its.responses.length &&
                    its.responses.forEach((iks) => {
                      if (item == iks.id && !iks.show) {
                        GetShowFalsePrevRoleIds.push(item);
                      }
                    });
                });
            });
          let roleIdsArr = roleIds.concat(GetShowFalsePrevRoleIds);

          AddAuthListApi(employeeIds, roleIdsArr, obj2).then((response) => {
            //新增
            if (response.status == 200) {
              let newobj = JSON.parse(JSON.stringify(this.formList));
              this.$emit("refurlist", newobj); //确定后发送$emit绑定父数据
              this.cancelForm();
            }
          });
        } else {
          return false;
        }
      });
    },
    cancelForm() {
      //取消关闭
      this.$emit("update:showdialog", false);
      this.$nextTick(function () {
        this.$refs.ruleForm.resetFields(); //重置输入框提示
      });
    },
    // 回调
    changeClose(item) {
      console.log(item);
      this.show = item;
    },
    // 确认
    confirmAuth() {
      this.$refs.platform.saveAuth();
    },
  },
};
</script>
<style lang="scss" scope>
.el-dialog-contract {
  .elcolEndDate {
    position: relative;
    .checkbox {
      position: absolute;
      top: 10px;
      right: 0;
    }
  }
}

.el-dialog__body {
  height: 60vh;
  min-height: 600px;
}
</style>
