<!--
  @description: 菜单管理
  @author: change by sunxinman
  @routerPath: /systemManagement/menuManagement
  @date:  2020/11/04
-->
<template>
  <div>
    <table-crud
      ref="tableCrud"
      :search-form="searchForm"
      :menu="true"
      search-label-width="80px"
      :page-flag="false"
      @on-load="getList"
    >
      <template v-slot:header>
        菜单管理列表
      </template>
      <template v-slot:search>
        <el-form-item label="菜单名称">
          <el-input
            v-model.trim="searchForm.functionname"
            style="width:220px"
            placeholder="请输入菜单名称"
            @keyup.enter.native="search"
          />
        </el-form-item>
      </template>
      <template v-slot:menuLeft>
        <el-button type="primary" plain @click="editMenu('','add')">
          新 增
        </el-button>
        <el-button @click="handleExpand()">
          全部{{ isExpand?'收起':'展开' }}
        </el-button>
      </template>
      <template v-slot:GXTable>
        <el-table
          ref="smallTable"
          :data="allList.filter(data => !searchForm.functionname || data.functionname.toLowerCase().includes(searchForm.functionname.toLowerCase()))"
          row-key="id"
          :default-expand-all="isExpand"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          size="small"
        >
          <el-table-column label="名称">
            <template slot-scope="scope">
              <span>{{ scope.row.functionname }}</span>
            </template>
          </el-table-column>
          <el-table-column label="前端地址">
            <template slot-scope="scope">
              <span>{{ scope.row.functionUrl }}</span>
            </template>
          </el-table-column>
          <el-table-column label="icon图标">
            <template slot-scope="scope">
              <span class="functionIconStyle_icon">
                <i :class="'iconfont '+ scope.row.functionIconStyle" />
              </span>
              <span>{{ scope.row.functionIconStyle }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="是否可用">
            <template slot-scope="scope">
              <span>{{ scope.row.functionType==1?'是':'否' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="180"
            align="center"
          >
            <template slot-scope="scope">
              <!-- functionLevel===0 只有第一级时展示 -->
              <el-link
                v-if="scope.row.functionLevel===0"
                type="primary"
                @click="editMenu(scope.row,'rowAdd')"
              >
                [新增子级]
              </el-link>
              <el-link
                type="primary"
                @click="editMenu(scope.row,'edit')"
              >
                [编辑]
              </el-link>
              <el-link
                type="primary"
                @click="deleteMenu(scope.row,scope.$index)"
              >
                [删除]
              </el-link>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </table-crud>
    <!--  新增/编辑菜单弹框 -->
    <menuAddDialog ref="menuAdd" :call-back="getList" :menu-list="allList" />
  </div>
</template>

<script>
/* api */
import { fetchMenuList, deleteMenu } from '@/api/rolemanagment/menu'
/* 组件 */
import menuAddDialog from './menuAdd-dialog'// 新增/编辑菜单弹框
export default {
  name: 'MenuList',
  components: {
    menuAddDialog// 新增/编辑菜单弹框
  },

  data() {
    return {
      isExpand: false, // 是否展开树 false 否 true 是
      allList: [], // 全部列表数据
      // 搜索参数
      searchForm: {
        functionname: '' // 菜单名称
      }
    }
  },
  methods: {
    // 是否展开table(展开与折叠切换)
    handleExpand() {
      this.isExpand = !this.isExpand
      this.$nextTick(() => {
        this.forArr(this.allList, this.isExpand)
      })
    },
    // 遍历
    forArr(arr, isExpand) {
      this.isExpand = isExpand
      arr.forEach(i => {
        // toggleRowExpansion(i, isExpand)用于多选表格，切换某一行的选中状态，如果使用了第二个参数，则是设置这一行选中与否（selected 为 true 则选中）
        this.$refs.smallTable.toggleRowExpansion(i, isExpand)
        if (i.children) {
          this.forArr(i.children, isExpand)
        }
      })
    },
    // 列表数据展示
    getList(params) {
      fetchMenuList(params).then(response => {
        this.allList = response.data.res
        this.forArr(this.allList, false)
      })
    },
    // 新增菜单/编辑菜单
    editMenu(row, type) {
      this.$refs.menuAdd.open({
        type: type, // add新增 edit 编辑 rowAdd 列表内的新增
        row: row // 菜单信息
      })
    },
    // 删除事件
    deleteMenu(row, index) {
      this.$confirm('确认要删除菜单吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        deleteMenu(row.id).then(response => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getList()
        })
      })
    }
  }
}
</script>
<style lang="less" scoped>
  @import '~@assets/style/common/public.less';
.functionIconStyle_icon{
  color: @hyacinthine;
  margin-right: 10px;
}
.scrollbar {
  height: 80vh;
  margin-top: 10px;
}

.filter-container {
  margin: 30px 20px !important;
}
</style>
