import { listApi, deleteApi, importApi, updateApi } from '@/apis/dataManage/dataImport.ts'
import { ref } from 'vue'

export const useMain = () => {
  // 列表loading
  const listLoading = ref(false)

  // 获取列表
  const getList = async () => {
    if (listLoading.value) return false
    listLoading.value = true
    const res = await listApi().catch(err => {
      return err
    })
    listLoading.value = false
    return res
  }

  const importLoading = ref(false)

  // 导入
  const importFile = async (data: Record<string, any>) => {
    if (importLoading.value) return false
    importLoading.value = true
    const res = await importApi(data).catch(err => {
      return err
    })
    importLoading.value = false
    return res
  }


  const deleteLoading = ref(false)

  // 删除
  const onDelete = async (data: Record<string, any>) => {
    if (deleteLoading.value) return false
    deleteLoading.value = true
    const res = await deleteApi(data).catch(err => {
      return err
    })
    deleteLoading.value = false
    return res
  }


  const updateLoading = ref(false)

  // 更新
  const onUpdate = async (data: Record<string, any>) => {
    if (updateLoading.value) return false
    updateLoading.value = true
    const res = await updateApi(data).catch(err => {
      return err
    })
    updateLoading.value = false
    return res
  }

  return {
    listRequest: {
      request: getList,
      loading: listLoading
    },
    importRequest: {
      request: importFile,
      loading: importLoading
    },
    deleteRequest: {
      request: onDelete,
      loading: deleteLoading
    },
    updateRequest: {
      request: onUpdate,
      loading: updateLoading
    }
  }
}
