<template>
  <div class="bbsl-common-page">
    <div class="bbsl-table-search">
      <el-form v-model="queryParam" :inline="true">
        <el-form-item label="资料分类名称：">
          <el-input
            v-model="querys[0].value"
            clearable
            maxlength="128"
            placeholder="请输入课目名称"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="handleSearch"
            >搜索
          </el-button>
          <el-button icon="el-icon-refresh" @click="clearQuerys"
            >重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row :gutter="10" class="fullheight">
      <el-col :span="6" class="fullheight">
        <div class="common-tree-container">
          <div class="container-head">
            <el-input
              v-model="filterText"
              clearable
              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="getTreen"></i>
          </div>
          <el-tree
            v-if="treeData.length"
            ref="tree"
            :data="treeData"
            :expand-on-click-node="expandAll"
            :filter-node-method="filterNode"
            :props="defaultProps"
            highlight-current
            node-key="id"
            @node-click="handleNodeClick"
          >
            <!-- :render-content="renderContent" -->
          </el-tree>
        </div>
      </el-col>

      <el-col :span="18" class="fullheight">
        <div ref="contentPanel" class="bbsl-table-content">
          <div ref="btnPanel" class="bbsl-btn-content">
            <el-button icon="el-icon-plus" type="success" @click="handleAdd"
              >新增
            </el-button>
            <el-popconfirm title="确定删除选中数据吗？" @confirm="handleDelete">
              <el-button
                slot="reference"
                :disabled="!multipleSelection.length"
                icon="el-icon-delete"
                style="margin-left: 10px"
                type="danger"
                >删除
              </el-button>
            </el-popconfirm>
          </div>
          <el-table
            ref="multipleSelection"
            v-loading="loading"
            :cell-style="{'text-align': 'center'}"
            :data="tableData"
            :header-cell-style="{'text-align': 'center'}"
            :max-height="tableMaxHeight"
            :style="{width: '100%'}"
            size="mini"
            @row-dblclick="handleDbClick"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column
              fixed
              label="序号"
              type="index"
              width="55"
            ></el-table-column>
            <el-table-column
              label="资料分类编码"
              min-width="200"
              prop="code"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              label="资料分类名称"
              min-width="200"
              prop="name"
              show-overflow-tooltip
            >
            </el-table-column>
            <el-table-column
              label="创建时间"
              min-width="200"
              prop="createTime"
              show-overflow-tooltip
            >
            </el-table-column>
            <!--   <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button size="mini" @click="Empower(scope.$index, scope.row)">授权</el-button>
                            </template>
                        </el-table-column> -->
          </el-table>
          <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
            class="bbsl-pagination"
            layout="prev, pager, next,total, sizes, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </el-col>
    </el-row>

    <zlflMenuDialog ref="formModal" @reload="reload"></zlflMenuDialog>
  </div>
</template>

<script>
import zlflMenu from '@/api/examination/zlflMenu.js'
import {TableMixin} from '@/mixins/tableMixin'
import zlflMenuDialog from './components/zlflMenuDialog.vue'

export default {
  name: 'equSysMenu',
  components: {
    zlflMenuDialog
  },
  mixins: [TableMixin],
  data() {
    return {
      url: {
        delete: '/exam/materialType/type/'
      },
      search: '',
      tableData: [],
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      parentId: null, //上级id
      querys: [
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'name',
          relation: 'AND',
          value: ''
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'parentId',
          relation: 'AND',
          value: ''
        }
      ],
      filterText: '',
      expandAll: false
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.getTreen()
  },
  mounted() {},
  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) {
      // console.log(data);
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    handleAdd() {
      this.$refs.formModal.add(this.parentId)
    },
    // 授权按钮
    Empower(index, item) {
      this.$refs.powerModal.add(item)
    },
    reload() {
      this.treeData = []
      this.$set(this.treeData)
      this.getTreen()
      this.loadData()
    },
    renderContent(h, {node, data, store}) {
      return (
        <span
          class="custom-tree-node"
          on-mouseover={() => {
            this.show = true
          }}
          on-mouseout={() => {
            this.show = false
          }}
        >
          <span>{data.name}</span>
          <span>
            <el-popover placement="bottom" width="55" trigger="hover">
              <el-button
                style="width:100%;border:0;margin:0;color:#606266;"
                size="mini"
                type="text"
                icon="el-icon-plus"
                on-click={() => this.append(data, 'son')}
              >
                新增
              </el-button>
              <el-button
                v-show={data.id !== '0'}
                style="width:100%;border:0;margin:0;color:#606266;"
                size="mini"
                type="text"
                icon="el-icon-edit"
                on-click={() => this.upDateinfo(data, '')}
              >
                修改
              </el-button>
              <el-button
                v-show={data.id !== '0'}
                style="width:100%;border:0;margin:0;color:#606266;"
                size="mini"
                icon="el-icon-delete"
                type="text"
                on-click={() => this.remove(data)}
              >
                删除
              </el-button>
              <i
                slot="reference"
                class="el-icon-more-outline"
                style="font-size: 15px;"
              ></i>
            </el-popover>
          </span>
        </span>
      )
    },
    append(data, type) {
      if (type != 'father') {
        this.$refs.formModal.add(data)
      } else {
        this.$refs.formModal.add(data)
      }
    },
    upDateinfo(data, type) {
      this.$refs.formModal.edit(data)
    },
    // 删除装备系统类型
    remove(item) {
      this.$confirm('确定删除选中数据？', '提示', {
        distinguishCancelAndClose: true,
        confirmButtonText: '确 定',
        cancelButtonText: '取 消',
        type: 'warning'
      })
        .then(() => {
          zlflMenu.deleteOneEqeSys(item.id).then(res => {
            if (res.data.state) {
              this.$notify({
                type: 'success',
                message: '删除成功',
                duration: 2000
              })
              this.getTreen()
              this.loadData(1)
            }
          })
        })
        .catch(action => {
          this.$notify({
            type: 'info',
            message: action === 'cancel' ? '已取消删除' : '停留在当前页面'
          })
        })
    },
    // 重置
    clearQuerys() {
      this.querys[0].value = ''
      this.loadData(1)
    },
    // 获取list
    loadData(page) {
      if (page) {
        this.queryParam.pageBean.page = page
      }
      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])
      }
      zlflMenu.getMaterial(this.queryParam, res => {
        this.total = res.total
        this.tableData = res.rows
        this.$set(this.tableData)
        this.loading = false
      })
    },
    // 获取树
    getTreen() {
      zlflMenu.getMaterTree({id: '0'}, res => {
        this.treeData = res.value
        this.$set(this.treeData)
      })
    },
    searchName() {
      let querys = {
        name: ''
      }
      querys.name = this.search
      zlflMenu.getMaterTree(querys, res => {
        this.treeData = res.value
        this.$set(this.treeData)
      })
    },
    // 点击树查看数据
    handleNodeClick(data) {
      this.parentId = data.id
      if (data.id == '0') {
        this.querys[1].value = ''
        this.loadData(1)
      } else {
        this.querys[1].value = this.parentId
        this.loadData(1)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.type-tag {
  margin: 0 2px;
}

.common-tree-container {
  height: calc(100% - 70px);
  margin-top: 0.52083vw;
  padding: 1.04167vw 1.04167vw;
  background-color: #fff;
  position: relative;
  box-sizing: border-box;
  overflow-y: auto;
  border-radius: 20px;
  box-shadow: 0px 0px 0.625vw 0 rgba(92, 102, 83, 0.2);
}

.title {
  width: 100%;
  margin-bottom: 10px;

  i {
    color: #1289fe;
    font-size: 25px;
  }

  span {
    font-size: 25px;
  }
}

::v-deep .el-tree {
  height: 90%;
  overflow-y: auto;
  border: none;
}

::v-deep .el-tree-node__content {
  padding: 5px 0;
}

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