<template>
    <div class='table-box' ref='popupBaseView'>
      <ProTable
          ref='proTable'
          :columns='columns'
          :request-api='getDictTreeByName'
          :pagination="false"
          
      >
        <template #tableHeader>
          <el-button v-auth.dictTree="'add'" type='primary' :icon='CirclePlus' @click="openDialog('add')">新增</el-button>
        </template>
  
        <template #operation='scope'>
          <el-button v-auth.dictTree="'add'" type='primary' link :icon='CirclePlus' @click="openDialog('add',scope.row)">新增</el-button>
          <el-button v-auth.dictTree="'edit'" type='primary' link :icon='EditPen' @click="openDialog('edit', scope.row)">编辑</el-button>
          <el-button v-auth.dictTree="'delete'" type='primary' link :icon='Delete' @click='handleDelete(scope.row)'>删除</el-button>
        </template>
      </ProTable>
    </div>
    <TreePopup :refresh-table='proTable?.getTableList' ref='treePopup'/>
  </template>
  
  <script setup lang='tsx' name='dict'>
  import {CirclePlus, Delete, EditPen} from '@element-plus/icons-vue'
  import {type Dict} from '@/api/system/dictionary/interface'
  import type { ProTableInstance, ColumnProps } from '@/components/ProTable/interface'
  import TreePopup from '@/views/system/dictionary/dictTree/components/treePopup.vue'
  import {useDictStore} from '@/stores/modules/dict'
  import {useHandleData} from '@/hooks/useHandleData'
  import {
    getDictTreeByName,
    deleteDictTree,
  } from '@/api/system/dictionary/dictTree'
  import { reactive, ref } from 'vue'
  defineOptions({
      name: 'dictTree'
  })
  const dictStore = useDictStore()

  const proTable = ref<ProTableInstance>()
  const treePopup = ref()
 
  const columns = reactive<ColumnProps<Dict.ResDictTree>[]>([
    { type: 'index', fixed: 'left', width: 70, label: '序号' },
    {
      prop: 'name',
      label: '字典名称',
      align:'left',
      headerAlign: 'center',
      search: { el: 'input' },
    },
    {
      prop: 'code',
      label: '编码',
    },
    {
      prop: 'remark',
      label: '描述',
    },
    {
      prop: 'sort',
      label: '排序',
      width:70,

    },
    {
      prop: 'state',
      label: '状态',
      minWidth:30,
      isShow:false,
      render: scope => {
        return (
           <el-tag type={scope.row.state === 0 ? 'success' : (scope.row.state === 1 ? 'warning' : 'danger')}>
                        {scope.row.state === 0 ? '正常' : (scope.row.state === 1 ? '停用' : '删除')}
            </el-tag>
           
        )
      },
    },
    {
      prop: 'operation',
      label: '操作',
      fixed: 'right',
      width:250,

    },
  ])

  // 打开 drawer(新增、编辑)
  const openDialog = (titleType: string, row: Partial<Dict.ResDictTree> = {}) => {
    treePopup.value?.open(titleType, row)
  }
  
  
  // 删除字典类型
  const handleDelete = async (params: Dict.ResDictTree) => {
    await useHandleData(deleteDictTree, params, `删除【${params.name}】树形字典类型`)
    proTable.value?.getTableList()
  }
  

  
  </script>
  