<template>
	<el-button type="warning" plain icon="el-icon-download" @click="download" :loading="loading"></el-button>
</template>

<script setup>
import api from "@/api"
import {ref, getCurrentInstance} from "vue"
import tools from "@/utils/tools"

const {proxy} = getCurrentInstance()
const props = defineProps({
	api: {type: Object},
	params: {
		type: Object, default: () => {
		}
	},
	module: {type: String, default: "demo"},
	name: {type: String, default: "数据导出"},
})

let loading = ref(false)

async function download() {
	const data = {module: props.module, name: props.name, params: props.params}
	let apiUrl = props.api
	if (!apiUrl) {
		apiUrl = api.tools.file.export
	}
	loading.value = true
	const [res, err] = await tools.go(apiUrl(data))
	loading.value = false
	if (err) {
		proxy.$message.error(res.msg)
		return
	}
	proxy.$message.success(res.msg)
}
</script>

<style scoped>

</style>
