<template>
  <div>
    <el-dialog
      title="选择人员"
      :visible.sync="userVisible"
      width="70%"
      :show-close="false"
    >
      <el-row :gutter="20">
        <!--部门数据-->
        <el-col :span="6" :xs="24">
          <div class="head-container">
            <el-input
              v-model="deptName"
              placeholder="请输入部门名称"
              clearable
              size="small"
              prefix-icon="el-icon-search"
              style="margin-bottom: 20px"
            />
          </div>
          <div class="head-container">
            <el-tree
              :data="deptOptions"
              :props="defaultProps"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              ref="tree"
              node-key="id"
              :default-expand-all="true"
              highlight-current
              @node-click="handleNodeClick"
            />
          </div>
        </el-col>
        <!--用户数据-->
        <el-col :span="18" :xs="24">
          <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
          >
            <el-form-item label="用户姓名" prop="nickName">
              <el-input
                v-model.trim="queryParams.nickName"
                placeholder="请输入用户姓名"
                clearable
                style="width: 150px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input
                v-model.trim="queryParams.phonenumber"
                placeholder="请输入手机号码"
                clearable
                style="width: 150px"
                @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" type="primary" @click="resetQuery"
                >重置</el-button
              >
            </el-form-item>
          </el-form>
          <el-table
            @row-click="clickRow"
            v-show="checkType === 'multiple'"
            ref="dataTable"
            v-loading="loading"
            :row-key="getRowKey"
            :data="userList"
            @select="handleSelect"
            @selection-change="handleMultipleUserSelect"
          >
            <el-table-column
              type="selection"
              :reserve-selection="true"
              width="50"
              align="center"
            />
            <el-table-column
              label="用户编号"
              align="center"
              key="userId"
              prop="userId"
              v-if="columns[0].visible"
            />
            <!-- <el-table-column
              label="用户昵称"
              align="center"
              key="nickName"
              prop="nickName"
              v-if="columns[1].visible"
              :show-overflow-tooltip="true"
            /> -->
            <el-table-column
              label="用户姓名"
              align="center"
              key="nickName"
              prop="nickName"
              v-if="columns[2].visible"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="部门"
              align="center"
              key="deptName"
              prop="dept.deptName"
              v-if="columns[3].visible"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="手机号码"
              align="center"
              key="phonenumber"
              prop="phonenumber"
              v-if="columns[4].visible"
              width="120"
            />
          </el-table>
          <el-table
            v-show="checkType === 'single'"
            v-loading="loading"
            :data="userList"
            @current-change="handleSingleUserSelect"
          >
            <el-table-column width="55" align="center">
              <template slot-scope="scope">
                <el-radio v-model="radioSelected" :label="scope.row.userId">{{
                  ""
                }}</el-radio>
              </template>
            </el-table-column>
            <el-table-column
              label="用户编号"
              align="center"
              key="userId"
              prop="userId"
              v-if="columns[0].visible"
            />
            <el-table-column
              label="登录账号"
              align="center"
              prop="nickName"
              v-if="columns[1].visible"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="用户姓名"
              align="center"
              key="nickName"
              prop="nickName"
              v-if="columns[2].visible"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="部门"
              align="center"
              key="deptName"
              prop="dept.deptName"
              v-if="columns[3].visible"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="手机号码"
              align="center"
              key="phonenumber"
              prop="phonenumber"
              v-if="columns[4].visible"
              width="120"
            />
          </el-table>
          <pagination
            v-show="total > 0"
            :total="total"
            :page-sizes="[5, 10]"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleUserVisible(false)">取 消</el-button>
        <el-button type="primary" @click="checkUserComplete">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listUserNew, deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { StrUtil } from "@/utils/StrUtil";

export default {
  name: "FlowUser",
  dicts: ["sys_normal_disable", "sys_user_sex"],
  components: { Treeselect },
  // 接受父组件的值
  props: {
    // 回显数据传值
    selectValues: {
      type: Number | String | Array,
      default: null,
      required: false,
    },
    selectNameValues: {
      type: Number | String | Array,
      default: null,
      required: false,
    },
    // 表格类型
    checkType: {
      type: String,
      default: "multiple",
      required: true,
    },
  },
  data() {
    return {
      userVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined,
      },
      // 列信息
      columns: [
        { key: 0, label: `用户编号`, visible: true },
        { key: 1, label: `用户名称`, visible: true },
        { key: 2, label: `用户昵称`, visible: true },
        { key: 3, label: `部门`, visible: true },
        { key: 4, label: `手机号码`, visible: true },
        { key: 5, label: `状态`, visible: true },
        { key: 6, label: `创建时间`, visible: true },
      ],
      radioSelected: null, // 单选框传值
      selectUserList: [], // 回显数据传值
      checkList: [],
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
    // selectValues: {
    //   handler(newVal) {
    //     if (StrUtil.isNotBlank(newVal)) {
    //       if (newVal instanceof Number) {
    //         this.radioSelected = newVal;
    //       } else {
    //         this.selectUserList = newVal;
    //       }
    //     }
    //   },
    //   immediate: true,
    // },
    // selectNameValues: {
    //   handler(newVal) {
    //     if (StrUtil.isNotBlank(newVal)) {
    //       this.selectNameValues = newVal;
    //       // console.log("选中的userId=", this.selectUserList);
    //       // console.log("选中的昵称=", newVal);
    //       let userIdList = this.selectUserList.split(",");
    //       let nickNameList = newVal.split(",");
    //       // console.log(userIdList,nickNameList)
    //       for (let i in userIdList) {
    //         this.handleCheckList(userIdList[i], nickNameList[i]);
    //         console.log("2222");
    //       }
    //     }
    //   },
    //   immediate: true,
    // },
    userList: {
      handler(newVal) {
        if (StrUtil.isNotBlank(newVal) && newVal.length > 0) {
          if (this.selectUserList && this.selectUserList.length > 0) {
            this.$nextTick(() => {
              this.$refs.dataTable.clearSelection();
              // console.log("this.selectUserList=", this.selectUserList);
              this.selectUserList?.split(",").forEach((key) => {
                let item = newVal.find((item) => key == item.userId);
                if (item) {
                  this.$refs.dataTable.toggleRowSelection(item, true);
                  this.handleCheckList(item.userId, item.nickName);
                }
              });
            });
          } else {
            this.$refs.dataTable.clearSelection();
          }
        }
      },
      immediate: true, // 立即生效
      deep: true, //监听对象或数组的时候，要用到深度监听
    },
    userVisible(newVal, oldVal) {
      if (newVal === true) {
        this.initialize();
      }
    },
  },
  // beforeDestroy() {
  //   console.log("销毁组件1");
  // },
  // destroyed() {
  //   console.log("销毁组件2");
  // },
  created() {
    // this.getList();
    // this.getDeptTree();
  },
  methods: {
    initialize() {
      this.checkList = [];
      // 遮罩层
      this.loading = true;
      // 选中数组
      // this.ids = [];
      // 非单个禁用
      this.single = true;
      // 非多个禁用
      this.multiple = true;
      // 显示搜索条件
      this.showSearch = true;
      // 总条数
      this.total = 0;
      // 用户表格数据
      // this.userList = [];
      // 弹出层标题
      this.title = "";
      // 部门树选项
      this.deptOptions = undefined;
      // 是否显示弹出层
      this.open = false;
      // 部门名称
      this.deptName = undefined;
      // 表单参数
      this.form = {};
      this.defaultProps = {
        children: "children",
        label: "label",
      };
      // 查询参数
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        nickName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined,
      };
      this.getList();
      this.getDeptTree();
      if (this.selectValues) {
        this.selectUserList = this.selectValues;
        // console.log("选中的userId=", this.selectUserList);
        // console.log("选中的昵称=", newVal);
        let userIdList = this.selectUserList.split(",");
        let nickNameList = this.selectNameValues.split(",");
        // console.log("接收的数据", userIdList, nickNameList);
        for (let i in userIdList) {
          this.handleCheckList(userIdList[i], nickNameList[i]);
        }
      } else {
        this.selectUserList = null;
        this.checkList = [];
        if (this.$refs.dataTable) {
          this.$refs.dataTable.clearSelection();
        }
      }
    },
    clickRow(row) {
      this.$refs.dataTable.toggleRowSelection(row);
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUserNew(this.queryParams).then((response) => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    handleCheckList(userId, nickName) {
      this.checkList.push({
        userId: userId.toString(),
        nickName: nickName,
      });
      // console.log("========未去重数组", this.checkList);
      let obj = {};
      let nArr = this.checkList.reduce((cur, next) => {
        obj[next.userId] ? "" : (obj[next.userId] = true && cur.push(next));
        return cur;
      }, []);
      this.checkList = nArr;
      // console.log("现在的checkList=", this.checkList);
    },
    handleUserVisible(userVisible) {
      this.userVisible = userVisible;
    },
    /*用户选中赋值*/
    checkUserComplete() {
      this.userVisible = false;
      // this.checkType = "";
    },
    // 保存选中的数据id,row-key就是要指定一个key标识这一行的数据
    getRowKey(row) {
      return row.id;
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    handleSelect(selection, row) {
      let posIndex;
      let flag = this.checkList.some((item, index) => {
        if (item.userId === row.userId.toString()) {
          posIndex = index;
          return true;
        }
      });
      if (flag) {
        this.checkList.splice(posIndex, 1);
      }
    },
    // 多选框选中数据
    handleMultipleUserSelect(selection) {
      for (let i in selection) {
        this.handleCheckList(selection[i].userId, selection[i].nickName);
      }
      this.$emit("handleUserSelect", this.checkList);
    },
    // 单选框选中数据
    handleSingleUserSelect(selection) {
      this.radioSelected = selection.userId; //点击当前行时,radio同样有选中效果
      this.$emit("handleUserSelect", selection);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.deptId = undefined;
      this.$refs.tree.setCurrentKey(null);
      this.handleQuery();
    },
  },
};
</script>
<style>
/*隐藏radio展示的label及本身自带的样式*/
/*.el-radio__label{*/
/*  display:none;*/
/*}*/
</style>
