<!-- 专业装备 -->
<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">
              <div class="title">
                <el-input
                  v-model="newParam.querys[0].value"
                  placeholder="请输入关键字搜索"
                  @keyup.enter.native="searchName"
                >
                  <i
                    slot="suffix"
                    class="el-input__icon el-icon-search"
                    style="color:#CCCCCC;cursor:pointer;"
                    @click="searchName()"
                  ></i>
                </el-input>
              </div>
              <el-tree
                ref="tree"
                :current-node-key="currentKey"
                :data="treeData"
                :expand-on-click-node="false"
                :props="defaultProps"
                default-expand-all
                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[2].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 size="small" @click="reset()" icon="el-icon-refresh">重置</el-button> -->
            </div>
          </el-header>
          <el-main style="padding:0">
            <el-table
              ref="multipleTable"
              :data="UserData"
              :row-key="binRowKey"
              border
              max-height="400"
              size="medium"
              stripe
              @select="manualSelection"
            >
              <el-table-column
                align="center"
                type="selection"
                width="45"
              ></el-table-column>
              <el-table-column label="名称" prop="majorName"></el-table-column>
              <el-table-column label="类型" prop="typeName"></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-card class="box-card org-find-card" shadow="never">
            <div slot="header" class="clearfix">
              <span>已选择</span>
            </div>
            <div class="common-tree-container">
              <el-tree
                ref="selectTree"
                :data="selectUser"
                :props="selectUserProps"
                :render-content="renderContent"
                node-key="majorName"
              >
              </el-tree>
            </div>
          </el-card>
        </el-aside>
        <!-- <el-aside width="200px" class="select-aside">
					<el-header class="select-header">
						<span>已选择</span>
					</el-header>
					<div class="select-tree-div">
						<el-tree ref="selectTree" node-key="majorName" :props="selectUserProps" :data="selectUser" :render-content="renderContent">
						</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 dictUtils from '@/components/dict/DictSelectUtils.js'
import subMenu from '@/api/examination/subMenu.js'

export default {
  mixins: [TableMixin],
  name: 'SelectOrganize',
  data() {
    return {
      querys: [
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'user_type_id',
          relation: 'AND',
          value: ''
        },
        {
          group: 'main',
          operation: 'NOT_IN',
          parentGroup: '',
          property: 'id_',
          relation: 'AND',
          value: ''
        },
        {
          group: 'main',
          operation: 'RIGHT_LIKE',
          parentGroup: '',
          property: 'name',
          relation: 'AND',
          value: ''
        }
      ],
      newParam: {
        pageBean: {
          pageSize: 20,
          page: 1
        },
        querys: [
          {
            group: 'main',
            operation: 'RIGHT_LIKE',
            parentGroup: '',
            property: 'name',
            relation: 'AND',
            value: ''
          }
        ]
      },

      search: '',
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      visible: false,
      UserData: [],
      selectUser: [],
      selectUserProps: {
        label: 'majorName'
      },
      nameArr: [],
      multipleSelection: [],
      lxList: [],
      currentKey: null,
      userlength: null,
      userInfoVos: []
    }
  },
  created() {
    this.lxList = dictUtils.getDictItemsFromCache('lx')
  },
  mounted() {},
  watch: {
    currentKey: {
      handler(value) {
        if (value) {
          this.$nextTick(() => {
            this.$refs.tree.setCurrentKey(this.currentKey)
          })
        }
      },
      immediate: true,
      valType: false
    }
  },
  methods: {
    add(val, row) {
      console.log(val, row)
      this.currentKey = null
      this.querys[1].value = ''
      this.visible = true
      this.selectUser = []
      this.userInfoVos = []
      this.userlength = null
      this.multipleSelection = []
      if (row.length > 0) {
        this.userInfoVos = row
        this.selectUser = JSON.parse(JSON.stringify(row))
        this.userlength = row.length
        this.$set(this.selectUser)
      }
      if (val) {
        this.valType = true
        // console.log();
        this.querys[0].value = val
      } else {
        this.valType = false
      }
      this.getTree(1)
      // this.loadData(1)
    },
    searchName() {
      this.getTree(1)
    },
    // 获取列表
    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.querys[1].value) {
        this.queryParam.querys.push(this.querys[1])
      }
      if (this.querys[2].value) {
        this.queryParam.querys.push(this.querys[2])
      }
      subMenu.professionalQuery(this.queryParam, res => {
        this.total = res.total
        // this.UserData = res.rows
        res.rows.forEach(item => {
          this.lxList.forEach(ee => {
            if (item.type == ee.value) {
              item['typeName'] = ee.name
            }
          })
          this.UserData.push({
            majorId: item.id,
            majorName: item.name,
            typeName: item.typeName
          })
        })
        this.selectUser.forEach(s => {
          this.UserData.forEach(u => {
            if (s.majorId == u.majorId) {
              this.$refs.multipleTable.toggleRowSelection(u, true)
            }
            // else{
            // 	this.$refs.multipleTable.clearSelection()
            // }
          })
        })
        this.$set(this.UserData)
      })
    },
    // 获取人员类别树
    getTree(page) {
      if (page) {
        this.newParam.pageBean.page = page
      }
      subMenu.getJobPage(this.newParam, res => {
        this.treeData = res.rows
        this.currentKey = res.rows[0].id
        this.querys[0].value = this.currentKey
        this.$set(this.querys[0])
        // this.$refs.tree.setCurrentKey(res.rows[0].id)
        this.loadData(1)
      })
    },
    fetchTreeData() {
      this.$refs.tree.setCurrentKey('0')
    },

    // 显示
    handleShow(val) {
      this.querys[0].value = val
      this.visible = true
      this.selectUser = []
      this.multipleSelection = []
      this.getTree(1)
      this.loadData(1)
      // this.newData(val)
    },
    newData(item) {
      this.querys[0].value = item
      this.loadData(1)
    },
    // 关闭新增人员窗口
    handleClose() {
      this.visible = false
      this.treeData = [
        {
          code: '',
          id: '',
          name: '行政维度',
          type: 0,
          children: []
        }
      ]
      this.UserData = []
      this.selectUser = []
    },
    // 树
    handleNodeClick(item) {
      this.valType = false
      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() {
      console.log(this.selectUser)
      this.$emit('professionalgetUser', this.selectUser)
      this.handleClose()
    },
    binRowKey(row) {
      return row.majorId
    },
    selectable(row) {
      let type = true
      this.userInfoVos.forEach((u, ui) => {
        if (ui <= this.userlength) {
          console.log(row.majorId, u.majorId, row.majorId == u.majorId)
          if (row.majorId == u.majorId) {
            type = false
          }
        }
      })
      console.log(type)
      return type
      // return true
    },
    manualSelection(selection, row) {
      let flag = selection.some(item => {
        if (item.majorId === row.majorId) return true
      })
      if (flag) {
        this.selectUser.push(row)
      } else {
        this.selectUser.forEach((s, si) => {
          if (s.majorId == row.majorId) {
            this.selectUser.splice(si, 1)
          }
        })
      }
    },
    handleSelectionChange(val) {
      console.log(val)
    },
    renderContent(h, {node, data, store}) {
      return (
        <span
          class="custom-tree-node"
          on-mouseover={() => {
            this.show = true
          }}
          on-mouseout={() => {
            this.show = false
          }}
        >
          <span>{data.majorName}</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) {
      console.log(val)
      this.selectUser.forEach((u, ui) => {
        if (u.majorId == val.majorId) {
          this.selectUser.splice(ui, 1)
        }
      })
      this.userInfoVos = JSON.parse(JSON.stringify(this.selectUser))
      this.userlength = this.selectUser.length
      this.loadData(1)
    }
  }
}
</script>

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

.left-aside {
  margin-right: 10px;
}

.select-aside {
  margin-left: 10px;

  ::v-deep .el-card__body {
    padding: 0 !important;
  }
}

.box-card {
  height: 99%;
}
</style>
