<template>
    <div class='table-box'>
      <ProTable
        ref='proTable'
        :columns='columns'
        :request-api='getFunctionList'
        :pagination="false"
      >
         <!-- :data='userData' -->
        <!-- 表格 header 按钮 -->
        <template #tableHeader>
          <el-button v-auth.resource="'add'" type='primary' :icon='CirclePlus' @click="openDialog('add')">新增</el-button>
        </template>
        <!-- 表格操作 -->
        <template #operation='scope'>
          <el-button v-auth.testScript="'edit'" type='primary' link :icon='EditPen' @click="openDialog('edit', scope.row)">编辑</el-button>
          <el-button v-auth.resource="'delete'" type='primary' link :icon='Delete' @click='handleDelete(scope.row)'>删除</el-button>
          </template>
      </ProTable>

       
    </div>
    <ResourcePopup :refresh-table='proTable?.getTableList' ref='resourcePopup' />

  </template>
  <script setup lang='tsx' name='useProTable'>
  import { ref ,reactive} from 'vue'
  import { useHandleData } from '@/hooks/useHandleData'
    import type { Function } from "@/api/user/interface/function"
  import ProTable from '@/components/ProTable/index.vue'
  import {Back, CirclePlus, Delete, EditPen,HomeFilled} from '@element-plus/icons-vue'
  import type { ColumnProps, ProTableInstance } from '@/components/ProTable/interface'
  import { useDictStore } from '@/stores/modules/dict'
  import ResourcePopup from './components/resourcePopup.vue'
  import {deleteFunction,getFunctionList} from '@/api/user/function/index'
  import * as Icons from '@element-plus/icons-vue'
  defineOptions({
      name: 'resource'
  })
  const dictStore = useDictStore()
  const resourcePopup = ref()
  // ProTable 实例
  const proTable = ref<ProTableInstance>()

  // 表格配置项
  const columns = reactive<ColumnProps<Function.ResFunction>[]>([
    { type: 'index', fixed: 'left', width: 70, label: '序号' },
    {
      prop: 'name',
      label: '名称',
      minWidth: 150,
      align:'left',
      headerAlign: 'center',
      search: { el: 'input' },
    },
    {
      prop: 'code',
      label: '编码',
      minWidth: 120,
    },
    {
    prop: 'type',
    label: '类型',
    width: 100,
    render: (scope) => {
    const typeMap: { [key: number]: { label: string; type: string } } = {
      0: { label: '菜单', type: 'primary' },
      1: { label: '按钮', type: 'success' },
      2: { label: '公共资源', type: 'info' },
      3: { label: '服务间调用资源', type: 'warning' },
    };
    const typeInfo = typeMap[scope.row.type] || { label: '未知', type: 'danger' };
    return (
      <el-tag type={typeInfo.type}>{typeInfo.label}</el-tag>
    );
  },
},
    {
      prop: 'icon',
      label: '图标',
      minWidth: 80,
      render: scope => {
      const customIcons: { [key: string]: any } = Icons
      const iconKey = scope.row.icon; // 
      if (!iconKey || !customIcons[iconKey]) {
      // 如果 iconKey 为空或未定义，或者 customIcons 中找不到对应的图标，返回一个空的 <span> 标签
      return <span></span>;
    }
      const icon = customIcons[iconKey]; // 如果找不到图标，使用默认图标
      return (
        <el-button  icon={icon} />
      );
    },
    },
    {
      prop: 'path',
      label: '路由地址',
      minWidth: 200,
    },
    {
      prop: 'component',
      label: '组件地址',
      minWidth: 200,
    },
    {
      prop: 'sort',
      label: '排序',
      width: 70,
    },

    {
      prop: 'state',
      label: '状态',
      minWidth: 70,
      render: scope => {
        return (
         
          <el-tag type={scope.row.state ? 'success' : 'danger'} > {scope.row.state ? '正常' : '禁用'} </el-tag>
        )
      },
    },
    { prop: 'operation', label: '操作', fixed: 'right',width: 200 },
  ])

 

// 打开 drawer(新增、编辑)
const openDialog = (titleType: string, row: Partial<Function.ResFunction> = {}) => {
  resourcePopup.value?.open(titleType, row)
 
}

  // 删除菜单信息
  const handleDelete = async (params: Function.ResFunction) => {
     await useHandleData(deleteFunction,  params , `删除【${params.name}】菜单`)
     proTable.value?.getTableList()
  }

 

  // 批量删除菜单信息
  const batchDelete = async (id: string[]) => {
    await useHandleData(deleteFunction, { id }, '删除所选菜单信息')
    proTable.value?.clearSelection()
    proTable.value?.getTableList()
  }


  </script>