<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <menu-search @search="reload" />
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :default-expand-all="false"
        :need-page="false"
        :parse-data="parseData"
        cache-key="systemMenuTable"
        @done="onDone"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit()"
          >
            添加
          </el-button>
          <el-button class="ele-btn-icon" size="small" @click="expandAll">
            展开全部
          </el-button>
          <el-button class="ele-btn-icon" size="small" @click="foldAll">
            折叠全部
          </el-button>
          <!-- <div class="el-button--primary">122222</div> -->
        </template>
        <!-- 标题列 -->
        <template v-slot:name="{ row }">
          <i :class="row.icon"></i> {{ row.name }}
        </template>
        <!-- 类型列 -->
        <template v-slot:type="{ row }">
          <el-tag
            type="primary"
            size="mini"
            :disable-transitions="true"
            v-if="row.type == 1"
          >
            菜单
          </el-tag>
          <el-tag
            type="warning"
            size="mini"
            :disable-transitions="true"
            v-if="row.type == 2"
          >
            按钮
          </el-tag>
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-plus"
            @click="openEdit(null, row.id)"
          >
            添加
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
          >
            修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除吗？"
            @confirm="remove(row)"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <menu-edit
      :data="current"
      :parent-id="parentId"
      :menu-list="menuList"
      :visible.sync="showEdit"
      @done="reload"
    />
  </div>
</template>

<script>
import MenuSearch from './components/menu-search.vue';
import MenuEdit from './components/menu-edit.vue';
import { listMenus, deleteMenu } from '@/api/system/menu';

export default {
  name: 'SystemMenu',
  components: { MenuSearch, MenuEdit },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          columnKey: 'index',
          type: 'index',
          width: 45,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          prop: 'name',
          label: '菜单名称',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'name'
        },
        {
          prop: 'url',
          label: '路由地址',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          prop: 'sort',
          label: '排序',
          align: 'center',
          showOverflowTooltip: true,
          width: 60
        },
        {
          prop: 'type',
          label: '类型',
          align: 'center',
          showOverflowTooltip: true,
          width: 60,
          slot: 'type'
        },
        {
          prop: 'createTime',
          label: '创建时间',
          showOverflowTooltip: true,
          minWidth: 110,
          formatter: (_row, _column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 190,
          align: 'center',
          resizable: false,
          slot: 'action',
          showOverflowTooltip: true
        }
      ],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 全部菜单数据
      menuList: [],
      // 上级菜单id
      parentId: null
    };
  },
  methods: {
    /* 表格数据源 */
    datasource({ where }) {
      return listMenus({ ...where });
    },
    /* 数据转为树形结构 */
    parseData(data) {
      // data.map(item=>{
      //    item.id = parseInt(item.id)
      // })
      return this.$util.toTreeData({
        data: data,
        idField: 'id',
        parentIdField: 'parentId'
      });
    },
    /* 表格渲染完成回调 */
    onDone({ data }) {
      //if (!this.menuList.length) {
      this.menuList = data;
      //}
    },
    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload({ where: where });
    },
    /* 显示编辑 */
    openEdit(row, parentId) {
      this.current = row;
      this.parentId = parentId;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      if (row.children?.length) {
        this.$message.error('请先删除子节点');
        return;
      }
      const loading = this.$loading({ lock: true });
      deleteMenu([row.id])
        .then((res) => {
          loading.close();
          this.$message.success('菜单删除成功');
          this.reload();
        })
        .catch((e) => {
          loading.close();
          // this.$message.error(e.message);
        });
    },
    /* 展开全部 */
    expandAll() {
      this.$refs.table.toggleRowExpansionAll(true);
    },
    /* 折叠全部 */
    foldAll() {
      this.$refs.table.toggleRowExpansionAll(false);
    },
    /* 判断是否是外链 */
    isUrl(url) {
      return !!(
        url &&
        (url.startsWith('http://') ||
          url.startsWith('https://') ||
          url.startsWith('//'))
      );
    },
    /* 判断是否是目录 */
    isDirectory(d) {
      return !!d.children?.length && !d.component;
    }
  }
};
</script>
