<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      title="选择单位"
      :visible.sync="showTroubleshooting"
      width="60%"
      :close-on-press-escape="false"
      :before-close="handleClose"
      top="5vh"
    >
      <template class="materialInformation">
        <div class="common-page-shadow left-tree">
          <el-input
            v-model="filterText"
            placeholder="请输入企业名称"
          ></el-input>
          <el-tree
            accordion
            :load="treeNode"
            node-key="id"
            default-expand-all
            :filter-node-method="filterNode"
            ref="tree"
            lazy
            :highlight-current="true"
            :props="defaultProps"
            @node-click="handleNodeClick"
          ></el-tree>
        </div>
        <div class="common-page-shadow">
          <div class="common-search-container" ref="contentPanel">
              <el-form v-model="queryParam" :inline="true">
                  <el-form-item label="名称">
                    <el-input
                      size="small"
                      v-model="querys[0].value"
                      clearable
                      placeholder="请输入名称"
                    ></el-input>
                  </el-form-item>
                  <el-form-item>
                    <el-button
                      size="small"
                      type="danger"
                      icon="el-icon-search"
                      @click="handleSearch"
                      >搜索</el-button
                    >
                    <el-button
                      size="small"
                      icon="el-icon-refresh"
                      @click="handleReset"
                      >重置</el-button
                    >
                  </el-form-item>
              </el-form>
          </div>
          <el-table
            ref="multipleSelection"
            v-loading="loading"
            border
            size="mini"
            :data="tableData"
            height="400px"
            :row-key="rowKey"
            :style="{width: '100%', height: '500px'}"
            :max-height="tableMaxHeight"
            :cell-style="{'text-align': 'center'}"
            :header-cell-style="{'text-align': 'center'}"
            @selection-change="handleSelectionChange"
            @select="tableSelect"
          >
            <el-table-column
              type="selection"
              width="55"
              :reserve-selection="true"
            />
            <el-table-column type="index" width="55" label="序号" />
            <el-table-column
              prop="name"
              min-width="10%"
              label="名称"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="code"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="编码 "
              resizable
            >
            </el-table-column>
            <!-- <el-table-column
            prop="pathName"
            show-overflow-tooltip
            min-width="10%"
            align="center"
            label="路径"
            resizable
          >
          </el-table-column> -->
          </el-table>
          <div style="float:right;margin-top:10px">
            <el-pagination
              ref="paginationPanel"
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="queryParam.pageBean.page"
              :page-sizes="[20, 30, 40, 50]"
              :page-size="queryParam.pageBean.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="queryParam.pageBean.total"
            >
            </el-pagination>
          </div>
        </div>
      </template>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleSave">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {TableMixin} from '@/mixins/tableMixin'
import basicData from '@/api/basicData'
export default {
  name: 'CmgtSaftyDangerCheckplanFormDialogTroubleshooting', // 排查计划明细
  props: {
    useOrgList: Array
  },
  data() {
    return {
      showTroubleshooting: false, // 控制弹层显示与隐藏
      multipleSelection: [],
      selectedValue: [],
      treeData: [],
      filterText: '',
      defaultProps: {
        id: 'code',
        label: 'name',
        isLeaf: 'leaf'
      },
      querys: [
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'name_',
          relation: 'AND',
          value: ''
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'code_',
          relation: 'AND',
          value: ''
        },
        {
          group: 'main',
          operation: 'NOT_EQUAL',
          parentGroup: '',
          property: 'id',
          relation: 'AND',
          value: ''
        }
      ],
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0
        }
      },
      currentUser:{}
    }
  },
  mixins: [TableMixin],
  created() {
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUserDetail'))
    this.querys[2].value = this.currentUser.org[0].orgId
    this.loadData()
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  methods: {
    rowKey(row) {
      return row.id
    },
    //搜索
    handleSearch() {
      this.loadData()
    },
    //重置
    handleReset() {
      this.querys[0].value = ''
      this.querys[1].value = ''
      this.querys[2].value = this.currentUser.org[0].orgId
      this.loadData()
    },
    //显示弹框
    add() {
      this.showTroubleshooting = true
      this.$nextTick(function() {
        if (this.useOrgList.length != 0) {
          this.useOrgList.forEach(item => {
            if (this.tableData.some(data => data.id === item.id)) {
              this.$refs.multipleSelection.toggleRowSelection(item, true) //这样就可以了
            } else {
              this.$refs.multipleSelection.toggleRowSelection(item, true) //这样就可以了
            }
          })
        }
      })
      this.loadData()
    },
    //筛选结点
    filterNode(value, data, node) {
      if (!value) return true
      return this.findSearKey(node, value)
    },
    //递归搜索父级是否包含关键字
    findSearKey(node, key) {
      if (node.label.indexOf(key) !== -1) {
        return true
      } else {
        if (node.parent.parent == null) {
          return false
        } else {
          return this.findSearKey(node.parent, key)
        }
      }
    },
    treeNode(node, resolve) {
      if (node.level === 0) {
        return resolve([{name: '行政维度'}])
      }
      if (node.level > 1) {
        return resolve([])
      } else {
        let params = {
          querys: [],
          pageBean: {
            pageSize: -1,
            page: -1
          }
        }
        this.$http
          .post('${portal}/api/org/v1/orgs/getOrgPage', params)
          .then(res => {
            console.log(res, 'pppppppppp')
            let data = res.data.rows
            // data.forEach(item => {
            //   item['isLeaf'] = false
            //   return
            // })
            resolve(data)
          })
      }
    },
    handleNodeClick(data, node) {
      console.log(data, 'data')
      this.querys[1].value = data.code
      this.loadData()
    },
    // 获取list
    loadData() {
      // this.loading = true
      this.tableData = []
      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])
      }
      if (this.querys[2].value) {
                this.queryParam.querys.push(this.querys[2])
            }
      basicData.getOrgPage(this.queryParam).then(res => {
        this.queryParam.pageBean.total = res.total
        this.tableData = res.rows
        this.$set(this.tableData)
      })
    },
    tableSelect(rows, row) {
      let selected = rows.length && rows.indexOf(row) !== -1
      if (!selected) {
        this.selectedValue = rows
      } else {
        this.selectedValue = []
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 点击关闭按钮
    handleClose() {
      this.$refs.multipleSelection.clearSelection()
      this.showTroubleshooting = false
    },
    // 保存按钮
    handleSave() {
      this.multipleSelection.forEach(item=>{
        delete item.isParent
      })
      let list = []
      list = this.multipleSelection
      list = Array.from(new Set(list.map(JSON.stringify)), JSON.parse)
      this.$emit('addDetail', list)
      this.handleClose()
    },
    handleCurrentChange(page) {
      this.queryParam.pageBean = {...this.queryParam.pageBean, page}
      this.loadData()
    },
    handleSizeChange(pageSize) {
      this.queryParam.pageBean = {...this.queryParam.pageBean, pageSize}
      this.loadData()
    }
  }
}
</script>

<style scoped lang="scss">
.dialog-footer {
  width: 100%;
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
.left-tree {
  width: 20% !important;
  border: 1px solid #ccc;
  border-radius: 6px;
  overflow: auto;
}
/deep/.el-dialog__body {
  display: flex;
}
.common-page-shadow {
  width: 75%;
  margin-left: 15px;
}
.common-search-container {
  padding: 0px;
  padding-left: 10px;
  box-shadow: 0 0 0;
  display: block;
}
/deep/.el-tree {
  height: 580px;
}
/deep/.el-dialog__body {
  height: 100%;
}

</style>
