<!-- 添加考生 -->
<template>
  <div>
    <el-dialog
      :before-close="handleClose"
      :close-on-click-modal="false"
      :visible="visible"
      custom-class="bbsl-location-dialog"
      title="添加考生"
      top="5vh"
      width="75%"
    >
      <el-container>
        <el-aside class="left-aside" width="230px">
          <el-card class="box-card org-find-card" shadow="never">
            <div slot="header" class="clearfix">
              <span>按岗位查找</span>
            </div>
            <div class="common-tree-container">
              <el-button size="mini" @click="expandHandle"
                >{{ expandAll ? '全部收起' : '全部展开' }}
              </el-button>
              <el-input
                v-model="filterText"
                clearable
                placeholder="输入关键字进行过滤"
              >
              </el-input>
              <el-tree
                v-if="jobsList.length"
                ref="tree"
                :data="jobsList"
                :expand-on-click-node="expandAll"
                :filter-node-method="filterNode"
                :props="defaultProps"
                highlight-current
                node-key="id"
                @node-click="handleNodeClick"
              >
              </el-tree>
            </div>
          </el-card>
        </el-aside>
        <el-container>
          <el-header class="middle-header">
            <div style="padding: 10px 0">
              <el-input
                v-model="querys[0].value"
                clearable
                placeholder="请输入名称"
                prefix-icon="el-icon-search"
                size="small"
                style="width:380px"
                @keyup.enter.native="searchEnterFun"
              >
              </el-input>
              <el-button
                icon="el-icon-search"
                size="small"
                style="margin-left: 20px"
                type="primary"
                @click="searchEnterFun()"
                >查询
              </el-button>
              <el-button icon="el-icon-refresh" size="small" @click="reset()"
                >重置
              </el-button>
            </div>
          </el-header>
          <el-main style="padding:0">
            <el-table
              ref="multipleSelection"
              :data="EquipmentList"
              :row-key="binRowKey"
              border
              height="400"
              size="medium"
              stripe
              @select="manualSelection"
              @selection-change="handleSelectionChange"
            >
              <el-table-column
                :reserve-selection="true"
                :selectable="selectable"
                align="center"
                type="selection"
                width="45"
              >
              </el-table-column>
              <el-table-column
                label="考生账号"
                prop="account"
              ></el-table-column>
              <el-table-column
                label="考生名称"
                prop="userName"
              ></el-table-column>
            </el-table>
          </el-main>
          <el-pagination
            ref="paginationPanel"
            :current-page="queryParam.pageBean.page"
            :page-size="queryParam.pageBean.pageSize"
            :page-sizes="[20, 30, 40, 50]"
            :total="total"
            align="right"
            background
            layout="prev, pager, next,total, sizes, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </el-container>
        <el-aside class="select-aside" width="200px">
          <el-header class="select-header">
            <span>已选择</span>
          </el-header>
          <div class="select-tree-div">
            <el-tree
              ref="selectTree"
              :data="selectUser"
              :props="selectUserProps"
              node-key="name"
            ></el-tree>
          </div>
        </el-aside>
      </el-container>
      <span slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button size="small" type="primary" @click="dialogConfirm"
          >确认</el-button
        >
        <el-button size="small" @click="handleClose">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {TableMixin} from '@/mixins/tableMixin'
import paperMenu from '@/api/examination/paperMenu.js'

export default {
  mixins: [TableMixin],
  name: 'SelectOrganize',
  props: ['userTypeId'],
  data() {
    return {
      querys: [
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'u.fullname_',
          relation: 'AND',
          value: ''
        }
      ],
      newParam: {
        pageBean: {
          pageSize: 20,
          page: 1
        }
      },

      search: '',
      treeData: [
        {
          code: '',
          id: '',
          name: '专业装备',
          type: 0,
          children: []
        }
      ],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      visible: false,
      UserData: [],
      selectUser: [],
      selectUserProps: {
        label: 'userName'
      },
      nameArr: [],
      multipleSelection: null,
      params: {
        majorIds: [],
        subjectIds: []
      },
      jobsList: [],
      EquipmentList: [],
      userInfoVos: [],
      userlength: null,
      paperId: null, //试卷id
      filterText: '',
      expandAll: false
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {},
  mounted() {},
  methods: {
    expandHandle() {
      console.log(this.$refs.tree.store)
      this.expandAll = !this.expandAll
      this.expandNodes(this.$refs.tree.store.root)
    },
    // 遍历树形数据，设置每一项的expanded属性，实现展开收起
    expandNodes(node) {
      node.expanded = this.expandAll
      for (let i = 0; i < node.childNodes.length; i++) {
        node.childNodes[i].expanded = this.expandAll
        if (node.childNodes[i].childNodes.length > 0) {
          this.expandNodes(node.childNodes[i])
        }
      }
    },
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    add(val) {
      this.EquipmentList = []
      this.userInfoVos = []
      this.visible = true
      this.paperId = val.id
      paperMenu.detailIdPaper(this.paperId, res => {
        if (res) {
          res.userInfoVos.forEach(e => {
            e.state = true
          })

          this.userInfoVos = res.userInfoVos
          this.selectUser = JSON.parse(JSON.stringify(res.userInfoVos))
          this.userlength = this.userInfoVos.length
        }
      })
      this.getTree()
    },
    searchName() {
      this.getTree(1)
    },
    // 获取列表
    loadData(page) {},

    selectable(row) {
      let type = true
      this.userInfoVos.forEach((u, ui) => {
        if (ui <= this.userlength) {
          if (row.account == u.account) {
            type = false
          }
        }
      })
      return type
      // return true
    },

    getTree(page) {
      if (page) {
        this.newParam.pageBean.page = page
      }
      paperMenu.getOrgPostPage(this.newParam, res => {
        if (res) {
          this.jobsList = res
          this.total = res.total
        }
      })
    },
    // 显示
    handleShow(val) {
      this.visible = true
      this.selectUser = []
      this.multipleSelection = null
      this.getTree(1)
      this.loadData(1)
    },

    // 关闭新增人员窗口
    handleClose() {
      this.visible = false
      this.treeData = [
        {
          code: '',
          id: '',
          name: '专业装备',
          type: 0,
          children: []
        }
      ]
      this.UserData = []
      this.selectUser = []
    },
    // 树
    handleNodeClick(val) {
      if (val) {
        this.queryParam.querys = [
          {
            group: 'defaultQueryGroup',
            operation: 'EQUAL',
            property: 'POS_ID_',
            relation: 'AND',
            value: val.id
          }
        ]
      }

      paperMenu.getOrgUserPage(this.queryParam, res => {
        if (res.rows) {
          this.EquipmentList = []
          res.rows.forEach(r => {
            this.EquipmentList.push({
              positionId: r.relId,
              account: r.account,
              userId: r.userId,
              userName: r.fullName
            })
          })
          //判断人员列表是否存在已选人员  存在赋予勾选状态
          this.selectUser.forEach(s => {
            this.EquipmentList.forEach(e => {
              if (s.account == e.account) {
                this.$refs.multipleSelection.toggleRowSelection(e, true)
              }
            })
          })
          this.total = res.total
        }
      })
    },
    getOrgUserPage(val) {
      if (val) {
        this.queryParam.pageBean.page = val
      }
      if (this.querys[0].value) {
        this.queryParam.querys.push(this.querys[0])
      }
      paperMenu.getOrgUserPage(this.queryParam, res => {
        if (res.rows) {
          this.EquipmentList = []
          res.rows.forEach(r => {
            this.EquipmentList.push({
              positionId: r.relId,
              account: r.account,
              userId: r.userId,
              userName: r.fullName
            })
          })
          //判断人员列表是否存在已选人员  存在赋予勾选状态
          this.selectUser.forEach(s => {
            this.EquipmentList.forEach(e => {
              if (s.account == e.account) {
                this.$refs.multipleSelection.toggleRowSelection(e, true)
              }
            })
          })
          this.total = res.total
        }
      })
    },
    // 中间列表
    searchEnterFun() {
      // this.loadData(1)
      this.getOrgUserPage(1)
    },
    // 重置
    reset() {
      this.querys[0].value = ''
      this.getOrgUserPage(1)
    },
    dialogConfirm() {
      //去除之前选择的考生
      let arr = JSON.parse(JSON.stringify(this.selectUser))
      arr.splice(0, this.userlength)
      if (arr.length > 0) {
        let params = {
          id: this.paperId,
          userInfoVos: arr
        }
        paperMenu.addUser(params, res => {
          if (res.state) {
            this.$message({
              message: '添加成功',
              type: 'success'
            })
          }
          this.handleClose()
        })
      } else {
        this.$message({
          message: '请选择添加考生',
          type: 'warning'
        })
      }
    },
    handleSelectionChange(val) {},
    manualSelection(selection, row) {
      //先判断当前操作flag
      //true勾选还是false取消
      let flag = selection.some(item => {
        if (item.account === row.account) return true
      })
      if (flag) {
        this.selectUser.push(row)
      } else {
        this.selectUser.forEach((s, si) => {
          if (s.account == row.account) {
            this.selectUser.splice(si, 1)
          }
        })
      }
    },
    handleSizeChange(val) {
      this.queryParam.pageBean.pageSize = val
      this.handleNodeClick()
    },
    handleCurrentChange(val) {
      this.queryParam.pageBean.page = val
      this.handleNodeClick()
    },
    binRowKey(row) {
      return row.userId
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__body {
  height: 100% !important;
  max-height: 80%;
}

.left-aside {
  border-right: 1px solid #eee;
  padding: 10px;
}

.select-aside {
  border-left: 1px solid #eee;
}

::v-deep .el-table__header-wrapper .el-checkbox {
  //找到表头那一行，然后把里面的复选框隐藏掉
  display: none;
}

::v-deep .el-tree {
  overflow-y: auto;
  max-height: 400px;
  border: none;
}
</style>
