<template>
  <div class="app-container">
    <!--筛选条件-->
    <div class="search-div">
      <el-form ref="selectForm" :inline="true" :model="listQuery" class="form-container">
        <el-form-item class="selectForm-container-item" prop="resourceName">
          <el-input v-model.trim="listQuery.resourceName" placeholder="菜单名称" clearable/>
        </el-form-item>
        <el-form-item class="selectForm-container-item" prop="resourceUrl">
          <el-input v-model="listQuery.resourceUrl" placeholder="菜单路径" clearable/>
        </el-form-item>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button v-if="hasPerm('/resource/add')" class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="add">新增</el-button>
      </el-form>
    </div>
    <!--查询结果Table显示-->
    <tree-table v-loading="listLoading" :data="list" :columns="columns" rowkey="id" class="treetable" border stripe>
      <el-table-column v-if="showOperate" label="操作" width="110">
        <template slot-scope="scope">
          <el-button v-if="hasPerm('/resource/update')" type="warning" size="mini" @click="edit(scope.row)">修改</el-button>
          <el-button v-if="hasPerm('/resource/delete')" type="danger" size="mini" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </tree-table>

    <!--编辑资源的对话框-->
    <edit-resource v-show="dialogFormVisible" ref="editResource" @watchChild="fetchData"/>
  </div>
</template>

<script>

import treeTable from '@/components/TreeTable'
import editResource from '@/views/system/resource/editResource'
import { getResourceList, delResource } from '@/api/system/resource'
import { toTreeList } from '@/utils/structure'

export default {
  name: 'ListResource',
  components: { treeTable, editResource },
  data() {
    return {
      listQuery: {
        resourceName: '', // 资源名称
        resourceUrl: '' // 资源路径
      }, // 查询条件
      columns: [
        {
          text: '资源名称',
          value: 'name',
          type: 'expand'
        },
        {
          text: '资源编号',
          value: 'code'
        },
        {
          text: '资源父编号',
          value: 'pcode'
        },
        {
          text: '请求地址',
          value: 'url'
        },
        {
          text: '排序',
          value: 'num',
          width: 50
        },
        {
          text: '层级',
          value: 'levels',
          width: 50
        },
        {
          text: '资源类型',
          value: 'resourceTypeName',
          width: 80
        },
        {
          text: '状态',
          value: 'status',
          width: 70
        }
      ], // 动态加载的表头
      list: [], // 资源数据列表
      listLoading: true, // 列表加载动画是否显示
      dialogFormVisible: false, // 对话框是否可见
      dialogStatus: '' // 对话框状态
    }
  },
  computed: {
    'showOperate': function() { // 判断是否显示操作列，如果编辑和删除的权限都没有则不显示操作列
      return this.hasPerm('/resource/update') || this.hasPerm('/resource/delete')
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    add() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$refs.editResource.initDialog(this.dialogStatus, this.dialogFormVisible)
    },
    edit(row) {
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$refs.editResource.initDialog(this.dialogStatus, this.dialogFormVisible, row)
    },
    del(row) {
      this.$confirm(
        '确定要删除' + row.name + '吗？',
        '确认删除',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delResource(row.id).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      })
    },
    search() {
      this.fetchData()
    },
    fetchData() {
      console.log('fetchData')
      this.listLoading = true
      getResourceList(this.listQuery).then(response => {
        this.list = toTreeList(response.data.list, '0', false)
        this.listLoading = false
      })
    },
    // 在嵌套列表list中删除指定元素
    deleteItem(list, des) {
      list.forEach((value, index) => {
        if (value.id === des.id) {
          console.log('findit')
          list.splice(index, 1)
        } else {
          if (value.children && value.children.length > 0) {
            this.deleteItem(value.children, des)
          }
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .treetable{
    margin-bottom: 50px;
  }
</style>
