<!-- 专业装备 -->
<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="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"
                :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="querys[1].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="multipleTable"
              :data="UserData"
              border
              height="390"
              size="medium"
              stripe
              @select="handleSelectionChange"
            >
              <el-table-column
                align="center"
                type="selection"
                width="45"
              ></el-table-column>
              <el-table-column label="科目信息" prop="name"></el-table-column>
              <el-table-column label="科目编码" prop="code"></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-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 questionListMenu from '@/api/examination/questionListMenu.js'
// import questionListMenu from '@/api/examination/questionListMenu.js'
export default {
  mixins: [TableMixin],
  name: 'SelectOrganize',
  props: ['userTypeId'],
  data() {
    return {
      querys: [
        {
          group: 'main',
          operation: 'NOT_IN',
          parentGroup: '',
          property: 's.id_',
          relation: 'AND',
          value: ''
        },
        {
          group: 'main',
          operation: 'RIGHT_LIKE',
          parentGroup: '',
          property: 'name',
          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: 'name'
      },
      nameArr: [],
      multipleSelection: null,
      params: {
        majorIds: [],
        subjectIds: []
      },
      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) {
      // console.log(data);
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    add(val) {
      console.log(val)
      // if (val.length > 0) {
      // 	this.querys[0].value = val[0].id
      // } else {
      // 	this.querys[0].value = ""
      // }
      this.visible = true
      this.multipleSelection = val
      this.getTree(1)
      this.loadData(1)
    },
    searchName() {
      this.getTree(1)
    },
    // 获取列表
    loadData(page) {
      console.log(this.params)
      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])
      }
      questionListMenu.findByMajorIds(this.queryParam, res => {
        this.total = res.total
        this.UserData = res.rows
        this.$nextTick(() => {
          this.multipleSelection.forEach(s => {
            this.UserData.forEach(u => {
              if (s.id == u.id) {
                this.$refs.multipleTable.toggleRowSelection(u, true)
              }
            })
          })
        })
        this.$set(this.UserData)
      })
    },
    // 获取人员类别树
    getTree(page) {
      if (page) {
        this.newParam.pageBean.page = page
      }
      questionListMenu.majorQuery(this.newParam, res => {
        this.treeData = res.value
      })
    },

    // 关闭新增人员窗口
    handleClose() {
      this.visible = false
      this.treeData = [
        {
          code: '',
          id: '',
          name: '专业装备',
          type: 0,
          children: []
        }
      ]
      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() {
      console.log(this.multipleSelection)
      this.$emit('getUser', this.multipleSelection)
      this.handleClose()
    },
    handleSelectionChange(selection, row) {
      this.multipleSelection = row
      // 清除 所有勾选项
      this.$refs.multipleTable.clearSelection()
      // 当表格数据都没有被勾选的时候 就返回
      // 主要用于将当前勾选的表格状态清除
      if (selection.length == 0) return
      this.$refs.multipleTable.toggleRowSelection(row, true)
    }
  }
}
</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;
}

.el-card {
  height: 96% !important;
}

.el-tree {
  max-height: 400px;
  overflow-y: auto;
  border: none;
}

.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>
