<template>
  <div class='table-box'>
    <ProTable
      ref='proTable'
      :columns='columns'
      :pagination="false"
      :toolButton="false"
      :data="tableData"
      :row-key="id"
    >
      <!-- 表格 header 按钮 -->
      <template #tableHeader='scope'>
        <el-button v-auth.testSource="'add'" type='primary' :icon='CirclePlus' @click="openDialog('add')">新增</el-button>
        <el-button v-auth.testSource="'batchDelete'" type='danger' :icon='Delete' 
                   plain :disabled='!scope.isSelected' @click='batchDelete(scope.selectedListIds)'>
          批量删除
        </el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation='scope'>
        <el-button type="primary" link :icon='CopyDocument' @click="copyRow(row)">复制</el-button>
        <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>
  <ParameterPopup :refresh-table='getTableList' ref='parameterPopup' />
</template>
  
  <script setup lang='tsx' name='useRole'>
  import { type TestSource } from '@/api/device/interface/testSource'
  import { useHandleData } from '@/hooks/useHandleData'
  import ProTable from '@/components/ProTable/index.vue'
  import type{  ProTableInstance, ColumnProps } from '@/components/ProTable/interface'
  import { CirclePlus, Delete, EditPen, Share, Download, Upload, View, Refresh,CopyDocument } from '@element-plus/icons-vue'
  import { useDictStore } from '@/stores/modules/dict'
  import ParameterPopup from '@/views/machine/testSource/components/parameterPopup.vue';
  import { onMounted, reactive, ref, watch } from 'vue'
  const parameterPopup = ref()
  const dictStore = useDictStore()
  // ProTable 实例
  const proTable = ref<ProTableInstance>()
  const tableData = ref<any[]>([])
  const props = defineProps<{
  data: TestSource.ResTestSource | null;
}>();
  
  onMounted(() => {
    getTableList();
})

watch(() => props.data, (newData) => {
  if (newData) {
    getTableList();
  }
})

  const getTableList = () => {
    if (props.data) {
      // 处理传递过来的数据
      let newParams = props.data.parameter ? JSON.parse(props.data.parameter) : {};
        // 确保 newParams 是一个数组
      if (!Array.isArray(newParams)) {
        newParams = [newParams];
      }
      const apiData = newParams;
      tableData.value = apiData;
  } 
};


const columns = reactive<ColumnProps<any>[]>([
  { type: 'selection', fixed: 'left', width: 70 },
  { type: 'index', fixed: 'left', width: 70, label: '序号' },
  {
    prop: 'sourceParamType',
    label: '参数类型',
    minWidth: 180,
  },
  {
    prop: 'sourceParamDesc',
    label: '参数描述',
    minWidth: 220,
  },
  {
    prop: 'value',
    label: '值',
    minWidth: 150,
  },
  { prop: 'operation', label: '操作', fixed: 'right', width: 250 },
])
  

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


// 批量删除设备
const batchDelete = async (id: string[]) => {
  
  proTable.value?.clearSelection()
  proTable.value?.getTableList()
}

// 删除设备
const handleDelete = async (params: TestSource.ResTestSource) => {
 console.log(tableData.value )
  tableData.value = tableData.value.filter(item => item.id !== params.id);
  console.log(tableData.value )
}
  
  </script>
  