<!-- 岗位弹框 -->
<template>
  <div>
    <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="select-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" 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: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
              height="400"
              size="medium"
              @selection-change="manualSelection"
            >
              <el-table-column
                align="center"
                :reserve-selection="true"
                type="selection"
                width="45"
              >
              </el-table-column>
              <el-table-column
                :index="
                  index =>
                    index +
                    1 +
                    (queryParam.pageBean.page - 1) *
                      queryParam.pageBean.pageSize
                "
                label="序号"
                align="center"
                type="index"
                width="100"
              ></el-table-column>
              <el-table-column
                label="岗位名称"
                prop="positionName"
              ></el-table-column>
              <el-table-column
                label="岗位编号"
                prop="positionCode"
              ></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="handleSizeChanges"
            @current-change="handleCurrentChanges"
          ></el-pagination>
        </el-container>
        <el-aside class="select-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">
              <el-tree
                ref="selectTree"
                :data="selectUser"
                :props="selectUserProps"
                :render-content="renderContent"
                node-key="majorName"
              >
              </el-tree>
            </div>
          </el-card>
        </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 subMenu from '@/api/examination/subMenu.js'
import {treeDataByDem, refineRequestBody} from '@/api/systemInterface.js'
import {set} from 'lodash'

export default {
  mixins: [TableMixin],
  name: 'SelectOrganize',
  data() {
    return {
      queryParam: {
        pageBean: {
          pageSize: 10,
          page: 1
        }
      },
      // newParam: {

      //   querys: [
      //     {
      //       group: 'main',
      //       operation: 'NOT_IN',
      //       parentGroup: '',
      //       property: 'o.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: ''
      //     }
      //   ]
      // },
      querys: [
        {
          property: 'ORG_ID_',
          value: '',
          group: 'orgTree',
          relation: 'AND',
          operation: 'IN'
        },
        {
          group: 'main',
          operation: 'RIGHT_LIKE',
          parentGroup: '',
          property: 'name',
          relation: 'AND',
          value: ''
        }
      ],
      search: '',
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      visible: false,
      UserData: [],
      selectUser: [],
      selectUserProps: {
        label: 'positionName'
      },
      nameArr: [],
      multipleSelection: [],
      // 专业id
      userTypeId: null,
      filterText: '',
      currentKey: null
    }
  },
  created() {},
  mounted() {},
  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
    }
  },
  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
    },
    async getTree() {
      this.treeData = [
        {
          code: '',
          id: '',
          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)
      }
    },
    handleNodeClick(data) {
      this.querys[0].value = data.id

      this.loadData(1)
    },
    handleSizeChanges(val) {
      this.queryParam.pageBean.page = 1
      this.queryParam.pageBean.pageSize = val
      this.loadData()
    },
    handleCurrentChanges(val) {
      this.queryParam.pageBean.page = val

      this.loadData()
    },
    add(row) {
      this.userTypeId = ''
      this.selectUser = []
      this.visible = true

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

      this.getTree()
    },

    // 获取列表
    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])
      }

      subMenu.getOrgPostPage(this.queryParam, res => {
        res.rows.forEach(r => {
          this.UserData.push({
            orgId: r.orgId,
            positionId: r.id,
            positionName: r.name,
            positionCode: r.groupCode
          })
        })

        this.total = res.total
        this.$set(this.UserData)
      })
    },
    //获取专业装备下的岗位
    getSelected() {
      subMenu.findByBindPos(this.userTypeId, res => {
        if (res) {
          this.querys[0].value = ''
          res.forEach((r, ri) => {
            this.selectUser.push(r)
            if (ri == 0) {
              this.querys[0].value = r.positionId
            } else {
              this.querys[0].value += ',' + r.positionId
            }
          })
          this.loadData(1)
        }
      })
    },
    // 关闭新增人员窗口
    handleClose() {
      this.$refs.multipleTable.clearSelection()
      this.userTypeId = []
      this.visible = false
      this.treeData = [
        {
          code: '',
          id: '',
          name: '单位信息',
          type: 0,
          children: []
        }
      ]
      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()
    },
    manualSelection(selection) {
      this.selectUser = selection
    },

    binRowKey(row) {
      return row.positionId
    },
    renderContent(h, {node, data, store}) {
      return (
        <span
          class="custom-tree-node"
          on-mouseover={() => {
            this.show = true
          }}
          on-mouseout={() => {
            this.show = false
          }}
        >
          <span>{data.positionName}</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.positionId == val.positionId) {
          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: 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: 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>
