<!-- 专业装备 -->
<template>
  <el-dialog
    :before-close="handleClose"
    :close-on-click-modal="false"
    :visible.sync="visible"
    custom-class="bbsl-location-dialog"
    title="参训考生"
    top="5vh"
    width="75%"
    append-to-body
  >
    <el-container>
      <el-aside class="left-aside" width="270px">
        <el-card class="box-card org-find-card" shadow="never">
          <div slot="header" class="clearfix">
            <span>按单位查找</span>
          </div>
          <div class="common-tree-container">
            <div class="container-head">
              <el-input
                v-model="filterText"
                clearable
                placeholder="输入关键字进行过滤"
              >
              </el-input>
              <img
                v-if="expandAll"
                src="@/assets/img/quanjusuoxiao2.png"
                @click="expandHandle"
              />
              <img
                v-else
                src="@/assets/img/quanping.png"
                @click="expandHandle"
              />
              <i class="el-icon-refresh" @click="getTree"></i>
            </div>
            <el-tree
              v-if="treeData.length"
              ref="tree"
              :current-node-key="currentKey"
              :data="treeData"
              :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="searchValue"
              clearable
              placeholder="姓名、账号、手机号码"
              prefix-icon="el-icon-search"
              size="small"
              style="width:70%"
              @keyup.enter.native="searchEnterFun"
            >
            </el-input>
            <el-button
              icon="el-icon-search"
              size="small"
              style="margin-left: 20px"
              type="primary"
              @click="searchEnterFun()"
              >查询
            </el-button>
          </div>
        </el-header>
        <el-main style="padding:0">
          <el-table
            ref="multipleTable"
            :data="UserData"
            :row-key="binRowKey"
            border
            height="400"
            size="medium"
            stripe
            @selection-change="manualSelection"
          >
            <el-table-column
              align="center"
              type="selection"
              :reserve-selection="true"
              width="45"
            ></el-table-column>
            <el-table-column
              label="序号"
              type="index"
              :index="
                index =>
                  index +
                  1 +
                  (queryParam.pageBean.page - 1) * queryParam.pageBean.pageSize
              "
            ></el-table-column>
            <el-table-column label="姓名" prop="fullname"></el-table-column>
            <el-table-column label="单位" prop="orgName"></el-table-column>
            <el-table-column label="账号" prop="username"></el-table-column>
            <el-table-column label="联系方式" prop="mobile"></el-table-column>
          </el-table>
        </el-main>
        <el-pagination
          ref="paginationPanel"
          :current-page="queryParam.pageBean.page"
          :page-size="queryParam.pageBean.pageSize"
          :page-sizes="[10, 20, 30, 40]"
          :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">
        <div class="select-header">
          <span>已选择</span>
        </div>
        <div class="select-tree-div">
          <el-tree
            ref="selectTree"
            :data="selectUser"
            :props="selectUserProps"
            :render-content="renderContent"
            node-key="fullname"
          ></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>
</template>

<script>
import {TableMixin} from '@/mixins/tableMixin'
import dictUtils from '@/components/dict/DictSelectUtils.js'
import {treeDataByDem, getOrgUserQuery} from '@/api/systemInterface.js'
import {set} from 'lodash'
export default {
  mixins: [TableMixin],
  name: 'SelectOrganize',
  props: ['userTypeId'],
  data() {
    return {
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 10,
          total: 0
        }
      },
      querys: [
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'ORG_ID_',
          relation: 'AND',
          value: ''
        }
      ],

      newParam: {
        demId: '1258608622670319616'
      },
      searchValue: '',
      search: '',
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      visible: false,
      UserData: [],
      selectUser: [],
      selectUserProps: {
        label: 'fullname'
      },
      nameArr: [],
      multipleSelection: null,
      params: {
        majorIds: [],
        subjectIds: []
      },

      currentKey: null,
      trainTypeListL: [],
      filterText: '',
      expandAll: false
    }
  },
  created() {
    this.trainTypeList = dictUtils.getDictItemsFromCache('xllb')
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    },
    currentKey: {
      handler(value) {
        if (value) {
          this.$nextTick(() => {
            this.$refs.tree.setCurrentKey(this.currentKey)
          })
        }
      },
      immediate: true,
      valType: false
    }
  },
  mounted() {},
  methods: {
    expandHandle() {
      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(row) {
      this.selectUser = []
      this.visible = true
      console.log(111111, row)

      if (row.length > 0) {
        this.selectUser = row
        setTimeout(() => {
          this.selectUser.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row)
          }, 10)
        })
      } else {
        this.selectUser = []
      }

      this.getTree()
    },
    searchName() {
      this.getTree()
    },
    // 获取列表
    async loadData(page) {
      if (page) {
        this.queryParam.pageBean.page = page
      }
      this.UserData = []
      this.queryParam.querys = []
      if (this.querys[0].value) {
        this.queryParam.querys.push(this.querys[0])
      }
      if (this.searchValue) {
        let arr = [
          {
            property: 'account',
            value: this.searchValue,
            group: 'quick',
            relation: 'OR',
            operation: 'LIKE'
          },
          {
            property: 'fullname',
            value: this.searchValue,
            group: 'quick',
            relation: 'OR',
            operation: 'LIKE'
          },
          {
            property: 'mobile',
            value: this.searchValue,
            group: 'quick',
            relation: 'OR',
            operation: 'LIKE'
          }
        ]
        arr.forEach(item => {
          this.queryParam.querys.push(item)
        })
      }

      let res = await getOrgUserQuery(this.queryParam)

      this.UserData = res.rows

      this.total = res.total
      this.$set(this.UserData)
    },
    // 获取人员类别树
    async getTree() {
      // if (page) {
      //   this.newParam.pageBean.page = page
      // }

      this.treeData = [
        {
          code: '',
          id: '0',
          name: '单位信息',
          type: 0,
          children: await treeDataByDem()
        }
      ]

      if (this.treeData.length > 0) {
        this.currentKey = this.treeData[0].id
        this.querys[0].value = this.currentKey

        this.loadData(1)
      }
    },
    // 显示
    handleShow(val) {
      this.visible = true
      this.selectUser = []
      this.multipleSelection = null
      this.getTree()
      this.loadData(1)
    },

    // 关闭新增人员窗口
    handleClose() {
      this.$refs.multipleTable.clearSelection()
      this.visible = false
      this.treeData = []
      this.UserData = []
      this.selectUser = []
    },
    // 树
    handleNodeClick(item) {
      this.querys[0].value = item.id
      this.loadData(1)
    },
    // 中间列表
    searchEnterFun() {
      this.loadData(1)
    },
    // 重置
    reset() {
      this.querys[0].value = ''
      this.querys[1].value = ''
      this.loadData(1)
    },
    dialogConfirm() {
      this.$emit('getUser', this.selectUser)
      this.handleClose()
    },
    binRowKey(row) {
      return row.userId
    },

    manualSelection(selection) {
      this.selectUser = selection
    },

    renderContent(h, {node, data, store}) {
      return (
        <span
          class="custom-tree-node"
          on-mouseover={() => {
            this.show = true
          }}
          on-mouseout={() => {
            this.show = false
          }}
        >
          <span>{data.fullname}</span>
          <span>
            <el-button
              style="width:100%;border:0;margin:0;"
              size="mini"
              // icon="el-icon-delete"
              type="text"
              on-click={() => this.remove(data)}
            >
              删除
            </el-button>
          </span>
        </span>
      )
    },
    remove(val) {
      this.selectUser.forEach((u, ui) => {
        if (u.userId == val.userId) {
          this.selectUser.splice(ui, 1)
        }
      })

      this.loadData(1)
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-dialog__body {
  height: 100% !important;
  max-height: 80%;
}
::v-deep .el-pagination {
  margin: 10px 10px 0;
}
.left-aside {
  border-right: 1px solid #eee;
  // padding: 10px;
}

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

.el-card {
  height: 100% !important;
  box-sizing: border-box;
}

.el-tree {
  max-height: 480px;

  overflow-y: auto;
  border: none;
}

.select-header {
  padding: 20px;
  line-height: 12px;
  border-bottom: 1px solid #ebeef5;
}
::v-deep .el-card__header {
  padding: 20px;
  line-height: 12px;
}
::v-deep .el-table__header-wrapper .el-checkbox {
  //找到表头那一行，然后把里面的复选框隐藏掉
  display: inline-block !important;
}

.container-head {
  display: flex;
  align-items: center;

  img {
    width: 28px;
    height: 28px;
    margin: auto 5px;
    cursor: pointer;
  }

  i {
    font-size: 28px;
    font-weight: 600;
    cursor: pointer;
  }
}
</style>
