<template>
   <div class='table-box'>
    <ProTable
      ref='proTable'
      :columns='columns'
      :request-api='getPqErrSysList'
    >
      <!-- 表格 header 按钮 -->
      <template #tableHeader='scope'>
        <el-button type='primary' :icon='CirclePlus' @click="openDialog('add')">新增</el-button>
        <el-button v-auth.device="'delete'" type='danger' :icon='Delete' plain :disabled='!scope.isSelected'
                   @click='batchDelete(scope.selectedListIds)'>
        删除
        </el-button>
      </template>
       <!-- 表格操作 -->
       <template #operation='scope'>
          <el-button v-auth.device="'view'" type='primary' link :icon='View' @click="openDialog('view', scope.row)">查看</el-button>
          <el-button v-auth.device="'edit'" type='primary' link :icon='EditPen' @click="openDialog('edit', scope.row)">编辑</el-button>
          <el-button v-auth.device="'delete'" type='primary' link :icon='Delete' @click='handleDelete(scope.row)'>删除</el-button>
        </template>
    </ProTable>
  </div>

  <ErrorSystemPopup :refresh-table='proTable?.getTableList' ref='errorSystemPopup' />
  <ErrorStandardPopup :refresh-table='proTable?.getTableList' ref='errorStandardPopup' />
</template>

<script setup lang="ts" name='useProTable'>
import ProTable from '@/components/ProTable/index.vue'
import type { ColumnProps, ProTableInstance } from '@/components/ProTable/interface'
import { CirclePlus, Delete,EditPen,View} from '@element-plus/icons-vue'
import { reactive,ref } from 'vue'
import { useHandleData } from '@/hooks/useHandleData'
import ErrorSystemPopup from '@/views/machine/errorSystem/components/errorSystemPopup.vue'
import ErrorStandardPopup from '@/views/machine/errorSystem/components/errorStandardPopup.vue'
import type { ErrorSystem } from '@/api/device/interface/error'
import { useDictStore } from '@/stores/modules/dict'
import { getPqErrSysList, deletePqErrSys} from '@/api/device/error/index'
const dictStore = useDictStore()
// ProTable 实例
const proTable = ref<ProTableInstance>()
const errorSystemPopup = ref()
const errorStandardPopup = ref()
// 表格配置项
const columns = ref<ColumnProps<ErrorSystem.ErrorSystemList>[]>([
  { type: 'selection', fixed: 'left', width: 70 },
  { type: 'index', fixed: 'left', width: 70, label: '序号' },
  {
    prop: 'name',
    label: '名称',
  },
  {
    prop: 'standardName',
    label: '标准号',
  },
  {
    prop: 'standardTime',
    label: '标准推行年份',
    width: 200,
    search: { el: 'input' },
  },
  {
    prop: 'devLevel',
    label: '适用设备等级',
    width: 120,
    enum: dictStore.getDictData('Dev_Level'),
    search: { el: 'select', props: { filterable: true } },
    fieldNames: { label: 'name', value: 'id' },
  },
  { prop: 'operation', label: '操作', fixed: 'right' ,width: 250,},
])

// 打开 drawer(新增、编辑)
const openDialog = (titleType: string, row: Partial<ErrorSystem.ErrorSystemList> = {}) => {
  if(titleType == 'view'){
    errorStandardPopup.value?.open(row.name, row)
  }else{
    errorSystemPopup.value?.open(titleType, row)
  }

 
}


// 批量删除设备
const batchDelete = async (id: string[]) => {
  await useHandleData(deletePqErrSys, id, '删除所选误差体系')
  proTable.value?.clearSelection()
  proTable.value?.getTableList()
}

// 删除设备
const handleDelete = async (params: ErrorSystem.ErrorSystemList) => {
  await useHandleData(deletePqErrSys, [params.id], `删除【${params.name}】误差体系`)
  proTable.value?.getTableList()
}

</script>

<style scoped>

</style>