<template>
  <div>
    <el-dialog
      :before-close="handleClose"
      :visible="visible"
      append-to-body
      custom-class="bbsl-location-dialog"
      title="关联岗位"
      top="5vh"
      width="75%"
    >
      <el-container>
        <el-aside class="select-aside" width="200px">
          <el-input v-model="filterText" placeholder="输入关键字进行过滤">
          </el-input>

          <el-tree
            ref="tree"
            :current-node-key="currentKey"
            :data="treeData"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            :props="defaultProps"
            default-expand-all
            highlight-current
            node-key="id"
            @node-click="handleNodeClick"
          >
          </el-tree>
        </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="loadData(1)"
              >
              </el-input>
              <el-button
                icon="el-icon-search"
                size="small"
                style="margin-left: 20px"
                type="primary"
                @click="loadData(1)"
                >查询
              </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
              @select="manualSelection"
            >
              <el-table-column
                align="center"
                type="selection"
                width="45"
              ></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, 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="positionId"
            >
            </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 userTypeMenu from '@/api/examination/userTypeMenu.js'

export default {
  mixins: [TableMixin],
  name: 'SelectOrganize',
  data() {
    return {
      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'
      },
      userInfoVos: [],
      nameArr: [],
      multipleSelection: [],
      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: {
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    add(val) {
      this.querys[0].value = ''
      if (val.length > 0) {
        this.userInfoVos = val
        this.userlength = this.userInfoVos.length
        this.selectUser = JSON.parse(JSON.stringify(val))
      } else {
        this.selectUser = []
      }
      this.visible = true
      this.$nextTick(() => {
        this.$refs.multipleTable.clearSelection()
      })
      this.multipleSelection = []
      // this.loadData(1)
      this.getTree()
    },
    searchName() {
      this.getTree(1)
    },

    getTree() {
      this.currentKey = null
      userTypeMenu.getOrgsList({}, res => {
        this.treeData = res
        this.currentKey = res[0].id
        this.querys[0].value = this.currentKey
        this.parentId = this.currentKey
        this.$set(this.querys[1])
        this.$set(this.treeData)
        this.loadData(1)
      })
    },
    handleNodeClick(data) {
      this.querys[0].value = data.id
      this.loadData(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])
      }
      userTypeMenu.getOrgPostPage(this.queryParam, res => {
        res.rows.forEach(item => {
          this.UserData.push({
            // orgId: item.orgId,
            state: true,
            positionCode: item.groupCode,
            positionId: item.id,
            positionName: item.name
          })
        })
        this.selectUser.forEach(s => {
          this.UserData.forEach(u => {
            if (s.positionId == u.positionId) {
              this.$refs.multipleTable.toggleRowSelection(u, true)
            }
          })
        })
        this.total = res.total
        this.$set(this.UserData)
      })
    },

    // 显示
    handleShow(val) {
      this.visible = true
      this.selectUser = []
      this.multipleSelection = []
      this.loadData(1)
      this.getTree(1)
    },
    // 关闭新增人员窗口
    handleClose() {
      this.visible = false
      this.treeData = [
        {
          code: '',
          id: '0',
          name: '行政维度',
          type: 0,
          children: []
        }
      ]
      this.UserData = []
      this.selectUser = []
    },
    binRowKey(row) {
      return row.positionId
    },
    selectable(row) {
      let type = true
      this.userInfoVos.forEach((u, ui) => {
        if (ui <= this.userlength) {
          if (row.positionId == u.positionId) {
            type = false
          }
        }
      })
      return type
      // return true
    },
    manualSelection(selection, row) {
      //先判断当前操作flag
      //true勾选还是false取消
      let flag = selection.some(item => {
        if (item.positionId === row.positionId) return true
      })
      if (flag) {
        this.selectUser.push(row)
      } else {
        this.selectUser.forEach((s, si) => {
          if (s.positionId == row.positionId) {
            this.selectUser.splice(si, 1)
          }
        })
      }
    },
    // 重置
    reset() {
      this.querys[0].value = ''
      this.querys[1].value = ''
      this.loadData(1)
    },
    dialogConfirm() {
      this.$emit('getUser', this.selectUser)
      this.handleClose()
    },
    handleSelectionChange(val) {
      return
      this.multipleSelection = val.map(item => {
        return item
      })
      this.selectUser = this.multipleSelection
      this.selectUser.forEach(item => {
        this.nameArr.push(item.fullname)
      })
    },
    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.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;

  .select-header {
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ebeef5;
  }
}

::v-deep .el-table__header-wrapper .el-checkbox {
  //找到表头那一行，然后把里面的复选框隐藏掉
  display: none;
}
</style>
