<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" 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="人员" >
          <el-tree
            class="tree-border"
            :data="staffOptions"
            show-checkbox
            ref="staff"
            node-key="id"
            empty-text="加载中，请稍候"
            :props="defaultProps"
            default-expand-all
            :default-checked-keys="defaultCheckedKeys"
          ></el-tree>
        </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";


export default {
  name: "Roles",
  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"
      },
      // 人员列表
      staffOptions: [],
      // 默认选中的节点 id 数组
      defaultCheckedKeys: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "name",
        disabled: "disabled" // 添加 disabled 属性
      },
    };
  },
  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();
    },
    // 表单重置
    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,
        staffOptions:[]
      };
      this.defaultCheckedKeys = [];  // 重置默认选中的节点
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 分配人员按钮操作 */
    handleAllocation(row) {
      this.reset();
      this.getStaffTreeselect(row);
      this.form = row;
      this.open = true;
      this.title = "分配人员";
    },

    /** 查询员工树结构 */
    getStaffTreeselect(data) {
      staffTreeselect(data).then(response => {
        this.staffOptions = response.data;
      });
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const checkedKeys = this.getStaffAllCheckedKeys();
          if (checkedKeys.length === 0) {
            this.$message.error("请至少选择一个人员");
            return;
          }
          this.form.userId = checkedKeys.join(",");
          console.log("form",this.form)
          proDeptRole(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },

    // 所有菜单节点数据
    getStaffAllCheckedKeys() {
      // 目前被选中的菜单节点
      let checkedKeys = this.$refs.staff.getCheckedKeys();
      // 半选中的菜单节点
      // let halfCheckedKeys = this.$refs.staff.getHalfCheckedKeys();
      // checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      checkedKeys.unshift.apply(checkedKeys);
      return checkedKeys;
    },
  },
};
</script>
