<template>
  <div class="menu-management-container">
    <el-row :gutter="20">
      <!--      <el-col :lg="4" :md="8" :sm="24" :xl="4" :xs="24">-->
      <!--        <el-card class="menu-left" shadow="hover">-->
      <!--          <el-tree-->
      <!--            :data="data"-->
      <!--            :default-expanded-keys="['root']"-->
      <!--            node-key="id"-->
      <!--            :props="defaultProps"-->
      <!--            @node-click="handleNodeClick" />-->
      <!--        </el-card>-->
      <!--      </el-col>-->
      <!--      <el-col :lg="20" :md="16" :sm="24" :xl="20" :xs="24">-->
      <el-col>
        <el-card shadow="hover">
          <vab-query-form>
            <vab-query-form-top-panel :span="12">
              <el-button
                icon="el-icon-plus"
                type="primary"
                @click="handleEdit('')">
                添加
              </el-button>
            </vab-query-form-top-panel>
          </vab-query-form>
          <el-table
            v-loading="listLoading"
            border
            :data="list"
            default-expand-all
            :height="$noPagingTableHeight(1)"
            row-key="id"
            :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
            <el-table-column
              label="菜单名称"
              prop="menuName"
              show-overflow-tooltip />
            <!--            <el-table-column label="路径" prop="path" show-overflow-tooltip />-->
            <el-table-column
              label="组件路径"
              prop="component"
              show-overflow-tooltip />
            <el-table-column
              label="权限标识"
              prop="perms"
              show-overflow-tooltip />
            <el-table-column label="排序" prop="sort" width="60" />
            <el-table-column
              label="图标"
              prop="icon"
              show-overflow-tooltip
              width="60">
              <template #default="{ row }">
                <vab-icon v-if="row.icon" :icon="row.icon" />
              </template>
            </el-table-column>
            <el-table-column label="菜单状态" prop="status" width="80">
              <template #default="{ row }">
                {{ row.status === '10' ? '正常' : '停用' }}
              </template>
            </el-table-column>
            <el-table-column label="显示状态" prop="visible" width="80">
              <template #default="{ row }">
                {{ row.status === '10' ? '显示' : '隐藏' }}
              </template>
            </el-table-column>
            <el-table-column label="缓存" show-overflow-tooltip width="60">
              <template #default="{ row }">
                {{ row.isCache ? '是' : '否' }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="创建时间" prop="createdTime">
              <template #default="scope">
                <span>{{ parseTime(scope.row.createdTime) }}</span>
              </template>
            </el-table-column>
            <!--            <el-table-column label="重定向" show-overflow-tooltip>-->
            <!--              <template #default="{ row }">-->
            <!--                {{ row.redirect ? row.redirect : '无' }}-->
            <!--              </template>-->
            <!--            </el-table-column>-->
            <!--            <el-table-column label="是否隐藏" show-overflow-tooltip>-->
            <!--              <template #default="{ row }">-->
            <!--                {{ row.meta.hidden ? '是' : '否' }}-->
            <!--              </template>-->
            <!--            </el-table-column>-->
            <!--            <el-table-column-->
            <!--              label="是否隐藏当前节点"-->
            <!--              show-overflow-tooltip-->
            <!--              width="100">-->
            <!--              <template #default="{ row }">-->
            <!--                {{ row.meta.levelHidden ? '是' : '否' }}-->
            <!--              </template>-->
            <!--            </el-table-column>-->
            <!--            <el-table-column label="是否固定" show-overflow-tooltip>-->
            <!--              <template #default="{ row }">-->
            <!--                {{ row.meta && row.meta.noClosable ? '是' : '否' }}-->
            <!--              </template>-->
            <!--            </el-table-column>-->
            <!--            <el-table-column label="badge" show-overflow-tooltip>-->
            <!--              <template #default="{ row }">-->
            <!--                <el-tag-->
            <!--                  v-if="row.meta && row.meta.badge"-->
            <!--                  effect="dark"-->
            <!--                  type="danger">-->
            <!--                  {{ row.meta.badge }}-->
            <!--                </el-tag>-->
            <!--              </template>-->
            <!--            </el-table-column>-->
            <!--            <el-table-column label="dot" show-overflow-tooltip>-->
            <!--              <template #default="{ row }">-->
            <!--                {{ row.meta && row.meta.dot ? '是' : '否' }}-->
            <!--              </template>-->
            <!--            </el-table-column>-->
            <el-table-column
              fixed="right"
              label="操作"
              show-overflow-tooltip
              width="85">
              <template #default="{ row }">
                <el-button type="text" @click="handleEdit(row)">编辑</el-button>
                <el-button type="text" @click="handleDelete(row)">
                  删除
                </el-button>
              </template>
            </el-table-column>
            <template #empty>
              <el-image
                class="vab-data-empty"
                :src="require('@/assets/empty_images/data_empty.png')" />
            </template>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import menuApi from '@/api/system/menu'
  import Edit from './components/MenuEdit'

  export default {
    name: 'Menu',
    components: { Edit },
    data() {
      return {
        data: [],
        defaultProps: {
          children: 'children',
          label: 'label',
        },
        list: [],
        listLoading: true,
      }
    },
    async created() {
      // const {
      //   data: { list },
      // } = await menuApi.getTree()
      // this.data = list
      await this.fetchData()
    },
    methods: {
      handleEdit(row) {
        if (row && row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          const { msg } = await menuApi.doDelete({ id: row.id })
          this.$baseMessage(msg, 'success', 'vab-hey-message-success')
          await this.fetchData()
        })
      },
      async fetchData(role) {
        this.listLoading = true
        const { data: data } = await menuApi.getList({ role })
        this.list = this.translateDataToTree(data)
        this.listLoading = false
      },
      handleNodeClick({ role }) {
        this.fetchData(role)
      },
    },
  }
</script>

<style lang="scss" scoped>
  $base: '.menu-management';
  #{$base}-container {
    padding: 0 !important;
    background: $base-color-background !important;
  }

  .menu-left {
    height: calc(100vh - 213px);
  }
</style>
