<!-- menuShield - 菜单屏蔽 -->
<template>
  <div class="app-container">
    <el-container>
      <!-- 企业树 -->
      <park-tree v-if="treeList.length" v-loading="loading" :has-childern-ids="true" :is-user="true" :tree-list="treeList" @changeTree="changePark" />
      <el-main>
        <el-row class="mb-20">
          <span class="list-title">菜单/按钮屏蔽列表</span>
        </el-row>
        <el-row>
          <el-table v-loading="loading" border :data="dataList">
            <el-table-column prop="xiangmumingcheng" label="项目名称" show-overflow-tooltip>
              <template v-slot="scope">
                <span>{{ scope.row.xiangmumingcheng ||'--' }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="e8banben" label="E8版本" show-overflow-tooltip>
              <template v-slot="scope">
                <span>{{ scope.row.e8banben ||'--' }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="zuijingengxinren" label="最近更新人" show-overflow-tooltip>
              <template v-slot="scope">
                <span>{{ scope.row.zuijingengxinren ||'--' }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="zuijingengxinshijian" label="最近更新时间" show-overflow-tooltip>
              <template v-slot="scope">
                <span>{{ scope.row.zuijingengxinshijian ||'--' }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="200" fixed="right">
              <template slot-scope="scope">
                <el-button size="mini" type="text" @click="handleView(scope.row)">查看</el-button>
                <el-button size="mini" type="text" @click="handleEdit(scope.row)">编辑</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-row>
      </el-main>
    </el-container>

    <!-- 查看/编辑弹窗 -->
    <el-dialog class="h290" :visible="show" :title="type==='edit'?'编辑屏蔽菜单及按钮':'查看屏蔽菜单及按钮'" width="540px" :close-on-click-modal="false" @close="close">
      <el-form :model="searchForm" @keyup.enter.native="handleSearch">
        <el-form-item>
          <el-input v-model.trim="searchForm.caidan" placeholder="请输入关键字进行过滤" />
        </el-form-item>
      </el-form>
      <el-tree
        ref="tree"
        node-key="id"
        show-checkbox
        :data="treeData"
        :default-expanded-keys="['0']"
        :props="{ label: 'name', children: 'children' }"
        @check="getCheckedNodes"
      />
      <template #footer>
        <el-button @click="close">取消</el-button>
        <el-button :loading="loading" type="primary" @click="submit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import lodash from 'lodash'
import { getEntProjectItemTree, getAllprivilegelist } from '@/apiV2/menuManage'

export default {
  components: {
    parkTree: () => import('@/components/parkTree/parkTree.vue')
  },
  data() {
    return {
      loading: false,
      treeList: [],
      dataList: [],
      show: false,
      type: 'edit',
      treeData: [],
      searchForm: {
        caidan: ''
      }
    }
  },
  async created() {
    // 调取左侧树接口
    const leftTree = await getEntProjectItemTree()
    const obj = lodash.cloneDeep(leftTree.data)
    this.treeList = this.filterTree(obj, [])
    getAllprivilegelist().then(res => {
      this.treeData = lodash.cloneDeep(res.data)
    })
  },
  methods: {
    // 过滤留下企业和项目
    filterTree(tree, arr) {
      // debugger
      if (!tree.length) return []
      for (const item of tree) {
        // 循环数组，然后过滤数据
        // 若该数据 type 不是企业或项目，跳出本次循环
        if (item.type == 'person' || item.type == 'park') continue
        // 若满足条件，用新的node代替，并清空children，用递归手动赋予children
        const node = { ...item, children: [] }
        // 添加到新数组中
        arr.push(node)
        // 若有 children，递归
        if (item.children && item.children.length) {
          this.filterTree(item.children, node.children)
        }
      }
      return arr
    },
    // 点击左侧树
    changePark(e) {
      console.log('e', e)
    },
    // 查看按钮
    handleView() {
      this.type = 'view'
      this.show = true
    },
    // 编辑按钮
    handleEdit() {
      this.type = 'edit'
      this.show = true
    },
    // 编辑提交
    submit() {

    },
    // 弹窗关闭
    close() {
      this.show = false
    },
    // 弹窗里搜索
    handleSearch() {

    },
    // 弹窗复选框事件
    getCheckedNodes() {
      const arr1 = this.$refs.tree.getCheckedNodes(false, true)
      const arr2 = []
      arr1.forEach(ele => {
        if (ele.id != 0) {
          arr2.push(ele.id)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
