<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="部门名称" prop="orgName">
        <el-input
          v-model="queryParams.orgName"
          maxlength="28"
          placeholder="请输入部门名称"
          clearable
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="角色名称" prop="deptRoleName">
        <el-input
          v-model="queryParams.deptRoleName"
          maxlength="28"
          placeholder="请输入角色名称"
          clearable
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="人员名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          maxlength="28"
          placeholder="请输入人员名称"
          clearable
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="typeList" >
      <el-table-column label="序号" align="center" width="80" type="index">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="部门名称" align="center" prop="orgName" :show-overflow-tooltip="true" />
      <el-table-column label="角色名称" align="center" prop="deptRoleName" :show-overflow-tooltip="true" />
      <el-table-column label="人员名称" align="center" prop="userName" :show-overflow-tooltip="true" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.reviewerStatus == null"
            @click="handleAllocation(scope.row)"
          >分配人员</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="部门名称" prop="orgName">
          <el-input v-model="form.orgName" :disabled="true" />
        </el-form-item>
        <el-form-item label="角色名称" prop="deptRoleName">
          <el-input v-model="form.deptRoleName" :disabled="true" />
        </el-form-item>

        <el-form-item label="人员：" prop="checkList">
          <treeselect v-model="form.checkList" :multiple="multiple" :flat="true" :options="personOptions"
                      :normalizer="normalizer" placeholder="选择部门负责人" @input="handleSelect"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getRoleUserList, proDeptRole} from "@/api/process/rolesRemind";
import { treeselect as staffTreeselect, roleStaffTreeselect } from "@/api/process/tree";
import {getInfoById} from "@/api/process/roles";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Roles",
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptRoleName: undefined,
        orgName: undefined,
        userName: undefined,
        departmentId:"-1"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        checkList: [
          { required: true, message: "请选择人员", trigger: "change" },
        ],
      },
      defaultProps: {
        children: "children",
        label: "name",
        disabled: "disabled" // 添加 disabled 属性
      },
      personOptions: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      getRoleUserList(this.queryParams).then(response => {
          this.typeList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.form.checkList=[];
    },
    // 表单重置
    reset() {
      this.form = {
        orgId: undefined,
        orgName: undefined,
        departmentId: undefined,
        deptRoleId: undefined,
        deptRoleName: undefined,
        userId: undefined,
        userName: undefined,
        reviewerStatus: undefined,
        reviewerTime: undefined,
        reviewerUserId: undefined,
        roleType: undefined,
        checkList: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 分配人员按钮操作 */
    handleAllocation(row) {
      this.reset();
      const orgId = row.orgId;
      const deptRoleId = row.deptRoleId
      getInfoById(orgId,deptRoleId).then(response => {
        this.form = response.data;
      })
      this.form.checkList=[];

      this.getStaffTreeselect(row);
      this.multipleJudge(row);
      this.open = true;
      this.title = "分配人员";
    },

    multipleJudge(row){
      if(row.deptRoleId ==1 || row.deptRoleId ==2 || row.deptRoleId ==3|| row.deptRoleId ==4){
        this.multiple = false;
      }else{
        this.multiple = true;
      }
    },

    /** 查询员工树结构 */
    getStaffTreeselect(data) {
      staffTreeselect(data).then(response => {
        this.personOptions = this.changeData(response.data);
      });
    },
    setCheck(data) { // 存在子集就不让禁止，父级让禁止
      data.map(item => {
        if (item.isCheck == 1) {
          this.form.checkList.push(item.id)
        }
        item.children && item.children.length > 0 && this.setCheck(item.children);
      })
    },

    //数据操作
    changeData(data) { // 存在子集就不让禁止，父级让禁止
      if (!Array.isArray(data)) {
        console.error('Expected an array, but received:', data);
        return [];
      }
      data.map(item => {
        if (item.disabled) {
          if (item.isCheck == 0) {
            item.isChecked = false
            item.isDisabled = true
          } else {
            item.isChecked = true
            item.isDisabled = false
          }
        } else if (!item.disabled) {
          item.isChecked = false
          item.isDisabled = false
        }
        item.children && item.children.length > 0 && this.changeData(item.children);
      })
      return data;
    },

    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        // isDisabled: node.disabled,
        children: node.children
      };
    },

    async handleSelect(value, instanceId) {
      console.log("value",value)
      console.log("instanceId",instanceId)
      this.form.checkList=value;
      // 在这里处理选择事件
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.multiple){
            this.form.userId = this.form.checkList.join(',');
          }else{
            this.form.userId = this.form.checkList;
          }
          proDeptRole(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
  },
};
</script>
