<!-- 专业装备 -->
<template>
  <el-dialog
    :before-close="handleClose"
    :close-on-click-modal="false"
    :visible="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="querys[1].value"
              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>
            <!-- <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"
            :row-key="binRowKey"
            border
            height="400"
            size="medium"
            stripe
            @current-change="manualSelection"
            highlight-current-row
          >
            <el-table-column
              label="序号"
              align="center"
              type="index"
              :index="
                index =>
                  index +
                  1 +
                  (newParam.pageBean.page - 1) * newParam.pageBean.pageSize
              "
              width="100"
            ></el-table-column>
            <el-table-column
              label="课目信息"
              align="center"
              prop="name"
              show-overflow-tooltip
            ></el-table-column>
            <el-table-column
              align="center"
              label="训练类别"
              prop="trainTypeName"
              show-overflow-tooltip
            ></el-table-column>
            <el-table-column
              label="教练员"
              align="center"
              show-overflow-tooltip
              prop="coachName"
            >
            </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">
        <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="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>
</template>

<script>
import {TableMixin} from '@/mixins/tableMixin'
import dictUtils from '@/components/dict/DictSelectUtils.js'
import questionListMenu from '@/api/examination/questionListMenu.js' // 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: 'LIKE',
          parentGroup: '',
          property: 'po.full_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: ''
          }
        ]
      },
      total: 0,
      search: '',
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      visible: false,
      UserData: [],
      selectUser: [],
      selectUserProps: {
        label: 'name'
      },
      nameArr: [],
      multipleSelection: null,
      params: {
        majorIds: [],
        subjectIds: []
      },
      userlength: null,
      userInfoVos: [],
      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() {
    this.$refs.multipleTable.setCurrentRow(this.selectUser)
  },
  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) {
      console.log(val)
      this.selectUser = []
      this.visible = true
      if (val) {
        this.userInfoVos = val
        this.selectUser = JSON.parse(JSON.stringify(val))
        this.userlength = val.length
      }
      console.log(this.selectUser)
      this.multipleSelection = null
      this.getTree(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])
      }
      questionListMenu.findByMajorIds(this.queryParam, res => {
        this.UserData = res.rows
        this.total = res.total

        this.UserData.forEach(u => {
          this.trainTypeList.forEach(e => {
            console.log(u, e)
            if (u.trainType == e.value) {
              u.trainTypeName = e.name
            }
          })
        })
        this.$nextTick(() => {
          this.selectUser.forEach(s => {
            console.log(1)

            this.UserData.forEach(u => {
              if (s.id == u.id) {
                this.$refs.multipleTable.toggleRowSelection(u)
              }
            })
          })
        })

        this.$set(this.UserData)
      })
    },
    // 获取人员类别树
    getTree(page) {
      if (page) {
        this.newParam.pageBean.page = page
      }
      questionListMenu.majorQuery(this.newParam, res => {
        if (res.value.length > 0) {
          this.treeData = res.value
          this.currentKey = this.treeData[0].id
          this.querys[0].value = this.currentKey
          console.log(this.currentKey)
          this.loadData(1)
        }
      })
    },
    // 显示
    handleShow(val) {
      this.visible = true
      this.selectUser = []
      this.multipleSelection = null
      this.getTree(1)
      this.loadData(1)
    },

    // 关闭新增人员窗口
    handleClose() {
      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.id
    },
    selectable(row) {
      let type = true
      this.userInfoVos.forEach((u, ui) => {
        if (ui <= this.userlength) {
          if (row.id == u.id) {
            type = false
          }
        }
      })
      return type
      // return true
    },
    manualSelection(row) {
      console.log(111, row)
      //先判断当前操作flag
      //true勾选还是false取消
      this.$refs.multipleTable.setCurrentRow(row)
      this.selectUser = []

      this.selectUser.push(row)
    },
    handleSelectionChange(val) {
      this.selectUser = val
      // this.multipleSelection = row;
      // // 清除 所有勾选项
      // this.$refs.multipleSelection.clearSelection()
      // // 当表格数据都没有被勾选的时候 就返回
      // // 主要用于将当前勾选的表格状态清除
      // if (selection.length == 0) return
      // this.$refs.multipleSelection.toggleRowSelection(row, true);
    },
    renderContent(h, {node, data, store}) {
      return (
        <span
          class="custom-tree-node"
          on-mouseover={() => {
            this.show = true
          }}
          on-mouseout={() => {
            this.show = false
          }}
        >
          <span>{data.name}</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.id == val.id) {
          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%;
}
::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: 400px;
  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: 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>
