<template>
    <div class='table-box'>
      <ProTable
        ref='proTable'
        :columns='columns'
        :request-api='getFunctionList'
        :pagination="false"
      >
         <!-- :data='userData' -->
        <!-- 表格 header 按钮 -->
        <template #tableHeader>
          <el-button type='primary' :icon='CirclePlus' @click="openDialog('add')">新增</el-button>
        </template>
        <!-- 表格操作 -->
        <template #operation='scope'>
          <el-button type='primary' link :icon='EditPen' @click="openDialog('edit', scope.row)">编辑</el-button>
          <el-button 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/function/interface'
  import ProTable from '@/components/ProTable/index.vue'
  import {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/function/index.ts'
  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,
      search: { el: 'input' },
    },
    {
      prop: 'code',
      label: '编码',
      minWidth: 100,
    },
    {
    prop: 'type',
    label: '类型',
    width: 100,
    enum: dictStore.getDictData('resourceType'),
    search: { el: 'select', props: { filterable: true } },
    fieldNames: { label: 'label', value: 'code' },
    },
    {
      prop: 'icon',
      label: '图标',
      minWidth: 100,
    },
    {
      prop: 'path',
      label: '路由地址',
      minWidth: 200,
    },
    {
      prop: 'component',
      label: '组件地址',
      minWidth: 200,
    },
    {
      prop: 'sort',
      label: '排序',
      width: 70,
    },

    {
      prop: 'state',
      label: '权限资源状态',
      minWidth: 100,
      enum: dictStore.getDictData('status'),
      fieldNames: { label: 'label', value: 'code' },
      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>