<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="title">
                <el-input
                  v-model="querys[2].value"
                  placeholder="请输入关键字搜索"
                  @keyup.enter.native="searchName"
                >
                  <i
                    slot="suffix"
                    class="el-input__icon el-icon-search"
                    style="color:#CCCCCC;cursor:pointer;"
                    @click="searchName()"
                  ></i>
                </el-input>
              </div>
              <el-tree
                v-if="treeData.length"
                :data="treeData"
                :props="defaultProps"
                default-expand-all
                highlight-current
                @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:50%"
                @keyup.enter.native="searchEnterFun"
              >
              </el-input>
              <el-button
                icon="el-icon-search"
                size="small"
                style="margin-left: 20px"
                type="primary"
                @click="searchEnterFun()"
                >查询
              </el-button>
            </div>
          </el-header>
          <el-main style="padding:0">
            <el-table
              ref="multipleTable"
              :data="orgData"
              :row-key="binRowKey"
              border
              height="490"
              size="medium"
              stripe
              @select="manualSelection"
            >
              <el-table-column
                align="center"
                type="selection"
                width="45"
              ></el-table-column>
              <el-table-column
                align="center"
                label="序号"
                type="index"
                width="50"
              ></el-table-column>
              <el-table-column
                label="名称"
                prop="accessControlName"
              ></el-table-column>
              <el-table-column label="编码" prop="code"></el-table-column>
              <el-table-column label="路径" prop="pathName"></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="170px">
          <el-header class="select-header">
            <span>已选择</span>
          </el-header>
          <div class="select-tree-div">
            <el-tree
              ref="selectTree"
              :data="selectOrgs"
              :props="selectOrgProps"
              :render-content="renderContent"
              node-key="accessControlId"
            ></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 givePower from '@/api/examination/givePower.js'

export default {
  mixins: [TableMixin],
  name: 'SelectOrganize',
  data() {
    return {
      querys: [
        {
          group: 'main',
          operation: 'RIGHT_LIKE',
          parentGroup: '',
          property: 'path',
          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: ''
        }
      ],
      search: '',
      treeData: [{code: '', id: '0', name: '行政维度', type: 0, children: []}],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      visible: false,
      orgData: [],
      selectOrgs: [],
      selectOrgProps: {
        label: 'accessControlName'
      },
      nameArr: [],
      multipleSelection: []
    }
  },
  created() {},
  mounted() {},
  methods: {
    searchName() {
      this.getTree(1)
    },
    // 获取列表
    loadData(page) {
      if (page) {
        this.queryParam.pageBean.page = page
      }
      this.orgData = []
      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])
      }
      givePower.getOrgsList(this.queryParam, res => {
        this.total = res.total
        res.rows.forEach(u => {
          this.orgData.push({
            accessControlName: u.name,
            accessControlId: u.id,
            code: u.code,
            pathName: u.pathName
          })
        })
        this.selectOrgs.forEach(s => {
          this.orgData.forEach(u => {
            if (s.accessControlId == u.accessControlId) {
              this.$refs.multipleTable.toggleRowSelection(u, true)
            }
          })
        })
        this.$set(this.orgData)
      })
    },
    // 获取树
    getTree(page) {
      if (page) {
        this.queryParam.pageBean.page = page
      }
      this.queryParam.querys = []
      if (this.querys[2].value) {
        this.queryParam.querys.push(this.querys[2])
      }
      givePower.getOrgsList(this.queryParam, res => {
        this.treeData[0].children = res.rows
      })
    },
    // 显示
    handleShow(val) {
      this.selectOrgs = []
      this.visible = true
      if (val) {
        this.userInfoVos = val
        this.selectOrgs = JSON.parse(JSON.stringify(val))
        this.userlength = val.length
      }
      this.loadData(1)
      this.getTree(1)
    },
    // 关闭新增人员窗口
    handleClose() {
      this.visible = false
      this.treeData = [
        {code: '', id: '0', name: '行政维度', type: 0, children: []}
      ]
      this.orgData = []
      this.selectOrgs = []
    },
    // 树
    handleNodeClick(item) {
      console.log(item)
      this.querys[0].value = item.path
      this.loadData(1)
    },
    // 中间列表
    searchEnterFun() {
      this.loadData(1)
    },
    // 重置
    reset() {
      this.querys[0].value = ''
      this.querys[1].value = ''
      this.loadData(1)
    },
    dialogConfirm() {
      this.$emit('getOrgs', this.selectOrgs)
      this.handleClose()
    },
    binRowKey(row) {
      return row.accessControlId
    },
    selectable(row) {
      let type = true
      this.userInfoVos.forEach((u, ui) => {
        if (ui <= this.userlength) {
          if (row.accessControlId == u.accessControlId) {
            type = false
          }
        }
      })
      return type
      // return true
    },
    manualSelection(selection, row) {
      console.log(row)
      //先判断当前操作flag
      //true勾选还是false取消
      let flag = selection.some(item => {
        if (row.accessControlId == item.accessControlId) return true
      })
      if (flag) {
        this.selectOrgs.push(row)
      } else {
        this.selectOrgs.forEach((s, si) => {
          if (s.accessControlId == row.accessControlId) {
            this.selectOrgs.splice(si, 1)
          }
        })
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map(item => {
        return item
      })
      this.selectOrgs = this.multipleSelection
      this.selectOrgs.forEach(item => {
        this.nameArr.push(item.name)
      })
    },
    renderContent(h, {node, data, store}) {
      return (
        <span
          class="custom-tree-node"
          on-mouseover={() => {
            this.show = true
          }}
          on-mouseout={() => {
            this.show = false
          }}
        >
          <span>{data.accessControlName}</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.selectOrgs.forEach((u, ui) => {
        if (u.accessControlId == val.accessControlId) {
          this.selectOrgs.splice(ui, 1)
        }
      })
      this.userInfoVos = JSON.parse(JSON.stringify(this.selectOrgs))
      this.userlength = this.selectOrgs.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;
}

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

.el-tree {
  border: none;
}

.select-aside {
  border-left: 1px solid #eee;
}

.select-header {
  border-bottom: 1px solid #ebeef5;
}
</style>
