import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { GeneralDataReception, Query } from '@/api/webApi'
import { IdType } from '../type'
const tableHeader = ref<any>([])
const tableData = ref<any>([])
const msgId = 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, submitID } = returnData
      tableHeader.value = columnSet
      tableData.value = listValues
      msgId.value = submitID
      return {
        tableHeader,
        tableData,
        msgId,
      }
    } else {
      ElMessage.error(message)
      return {
        tableHeader,
        tableData,
        msgId,
      }
    }
  } catch (err: any) {
    return {
      tableHeader,
      tableData,
      msgId,
    }
  }
}

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

export default { getTable, dataChange }
