<template>
    <div class='table-box' ref='popupBaseView'>
      <ProTable
        ref='proTable'
        :columns='columns'
        :request-api='getTableList'
      >
        <!-- :requestApi="getRoleList" -->
        <!-- 表格 header 按钮 -->
        <template #tableHeader='scope'>
          <el-button v-auth.devType="'add'" type='primary' :icon='CirclePlus' @click="openDialog('add')">新增</el-button>
          <el-button v-auth.devType="'delete'" type='danger' :icon='Delete' plain :disabled='!scope.isSelected'
                     @click='batchDelete(scope.selectedListIds)'>
            删除
          </el-button>
        </template>
        <!-- 表格操作 -->
        <template #operation='scope'>
          <el-button v-auth.devType="'edit'" type='primary' link :icon='EditPen' :model-value='false'
                     @click="openDialog('edit', scope.row)">编辑
          </el-button>
          <el-button v-auth.devType="'delete'" type='primary' link :icon='Delete' @click='handleDelete(scope.row)'>删除
          </el-button>
        </template>
  
      </ProTable>
    </div>
    <IcdPopup :refresh-table='proTable?.getTableList' ref='icdPopup' />
  </template>
  
  <script setup lang='tsx' name='useRole'>
  import TimeControl from '@/components/TimeControl/index.vue'
  import { type ICD } from '@/api/device/interface/icd'
  import { useHandleData } from '@/hooks/useHandleData'
  import { useDownload } from '@/hooks/useDownload'
  import ProTable from '@/components/ProTable/index.vue'
  import { type ProTableInstance, type ColumnProps } from '@/components/ProTable/interface'
  import { CirclePlus, Delete, EditPen, Download, Upload } from '@element-plus/icons-vue'
  import { useDictStore } from '@/stores/modules/dict'
  import {
    getICDList,
    deleteICD,
  } from '@/api/device/icd/index.ts'
  import { reactive, ref } from 'vue'
  import { useModeStore, useAppSceneStore } from '@/stores/modules/mode'
  
  defineOptions({
    name: 'devType',
  })
  const modeStore = useModeStore()
  const dictStore = useDictStore()
  // ProTable 实例
  const proTable = ref<ProTableInstance>()
  const icdPopup = ref()

  
  const getTableList = async (params: any) => {
    let newParams = JSON.parse(JSON.stringify(params))
    return getICDList(newParams)
  }
  
  
  // 表格配置项
  const columns = reactive<ColumnProps<ICD.ResICD>[]>([
    { type: 'selection', fixed: 'left', width: 70 },
    { type: 'index', fixed: 'left', width: 70, label: '序号' },
    {
      prop: 'name',
      label: '名称',
      search: { el: 'input' },
    },
    {
      prop: 'path',
      label: '存储地址',
  
    },
    {
      prop: 'createTime',
      label: '创建时间',
      render: scope => {
        if (scope.row.createTime) {
        const date = new Date(scope.row.createTime);
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const seconds = String(date.getSeconds()).padStart(2, '0');
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
      }
      return '';
      }
    },
    { prop: 'operation', label: '操作', fixed: 'right', width: 200 },
  ])
  
  
  // 打开 drawer(新增、编辑)
  const openDialog = (titleType: string, row: Partial<ICD.ResICD> = {}) => {
    icdPopup.value?.open(titleType, row)
  }
  
  
  // 批量删除设备
  const batchDelete = async (id: string[]) => {
    await useHandleData(deleteICD, id, '删除所选icd')
    proTable.value?.clearSelection()
    proTable.value?.getTableList()
  }
  
  // 删除设备
  const handleDelete = async (params: ICD.ResICD) => {
    await useHandleData(deleteICD,  [params.id] , `删除【${params.name}】icd`)
    proTable.value?.getTableList()
    
  }
  
  </script>
    