import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { GeneralDataReception, Query } from '@/api/dataIntegration'
import { IdType } from '../type'
const tableHeader = ref<any>([])
const tableData = ref<any>([])
const getTable = async (id: IdType, dataContent) => {
  try {
    const { code, returnData, message } = await Query({
      id: id,
      needPage: 1,
      dataContent: [...dataContent],
    })
    if (code == 0 && returnData) {
      const { columnSet, listValues } = returnData
      tableHeader.value = columnSet
      tableData.value = listValues
      return {
        tableHeader,
        tableData,
      }
    } else {
      ElMessage.error(message)
      return {
        tableHeader,
        tableData,
      }
    }
  } catch (err: any) {
    return {
      tableHeader,
      tableData,
    }
  }
}

const dataChange = async (id: IdType, event: IdType, data) => {
  const obj = Object.assign(data, { event })
  const { code, message } = await GeneralDataReception({
    serviceId: id,
    dataContent: JSON.stringify(obj),
  })
  return {
    code,
    message,
  }
}

export default { getTable, dataChange }
