<!-- 专业装备 -->
<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-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"
            :row-key="binRowKey"
            border
            height="400"
            size="medium"
            stripe
            highlight-current-row
            @current-change="manualSelection"
          >
            <el-table-column
              label="序号"
              width="50"
              type="index"
            ></el-table-column>
            <el-table-column
              label="年度"
              prop="year"
              show-overflow-tooltip
            ></el-table-column>
            <el-table-column
              label="训练课目"
              show-overflow-tooltip
              prop="subjectName"
            ></el-table-column>
            <el-table-column
              label="训练场地"
              prop="spaceName"
              :formatter="({spaceName}) => spaceName || '-'"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column label="训练日期" prop="subjectName">
              <template v-slot="{row}">
                {{ row.startTime + '-' + row.endTime }}
              </template>
            </el-table-column>
            <el-table-column
              label="训练时长"
              show-overflow-tooltip
              prop="hour"
            ></el-table-column>
            <el-table-column
              label="教练员"
              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">
        <el-header class="select-header">
          <span>已选择</span>
        </el-header>
        <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 {dictionaryEscape, getDictionaryByCode} from '@/utils/dictionary'
import * as coachApi from '@/api/examination/jlyList.js'
import * as examSpaceData from '@/api/examDataMenu/examSafeguardData/examSpaceData.js'

import {TableMixin} from '@/mixins/tableMixin'
import * as api from '@/api/examDataMenu/examManagerData/examPlan.js'

export default {
  mixins: [TableMixin],
  name: 'SelectOrganize',

  props: ['userTypeId'],
  data() {
    return {
      queryParam: {
        pageBean: {pageSize: 20, page: 1, total: 0},

        sorter: [{direction: 'DESC', property: 'createTime'}]
      },
      querys: [
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'org_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: ''
        },
        {operation: 'EQUAL', property: 'planType', value: '1'}
      ],

      search: '',

      visible: false,
      UserData: [],
      selectUser: [],
      selectUserProps: {
        label: 'subjectName'
      },
      nameArr: [],
      multipleSelection: null,

      userlength: null,
      userInfoVos: [],
      currentKey: null,

      filterText: '',
      expandAll: false
    }
  },
  created() {},

  mounted() {},
  methods: {
    dictionaryEscape,

    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, orgId) {
      this.querys[0].value = orgId
      this.selectUser = []
      this.visible = true
      if (val.length > 0) {
        this.userInfoVos = val
        this.selectUser = JSON.parse(JSON.stringify(val))
        this.userlength = val.length
      }
      this.loadData(1)
      this.multipleSelection = null
    },

    // 获取列表
    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.querys[1].value) {
        this.queryParam.querys.push(this.querys[1])
      }
      this.queryParam.querys.push(this.querys[3])
      let res = await api.tableData(this.queryParam)

      this.UserData = res.rows

      this.$nextTick(() => {
        this.selectUser.forEach(s => {
          console.log(1)

          this.UserData.forEach(u => {
            if (s.id == u.id) {
              this.$refs.multipleTable.toggleRowSelection(u)
            }
          })
        })
      })
      console.log(111, this.selectUser)

      this.total = res.total
      this.$set(this.UserData)
    },

    // 显示
    handleShow(val) {
      this.visible = true
      this.selectUser = []
      this.multipleSelection = null

      this.loadData(1)
    },

    // 关闭新增人员窗口
    handleClose() {
      this.visible = false
      this.treeData = []
      this.UserData = []
      this.selectUser = []
    },

    // 中间列表
    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
    },

    manualSelection(row) {
      console.log(row)
      //先判断当前操作flag
      //true勾选还是false取消

      this.$refs.multipleTable.setCurrentRow(row)
      this.selectUser = this.selectUser.filter(item => {
        return item.id !== row.id
      })

      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.subjectName}</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%;
}

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

.select-header {
  border-bottom: 1px solid #ebeef5;
}

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