<template>
    <app-drawer :title="t('common.alert')" v-model:show="show" :loading="loading">
        <div class="g-text-message">{{ t('investor.manage.user.export.tips') }}</div>
        <template #footer>
            <el-button @click="onCancel">{{ t('operation.cancel') }}</el-button>
            <el-button type="primary" @click="onSubmit">{{ t('operation.confirm') }}</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { ElMessage } from 'element-plus'
import { base64ToBlob, downloadFile } from '@/filters'
import { investorExport } from '@/services/api/investor'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import { i18n } from '@/stores'

const { global: { t } } = i18n
const show = shallowRef(true)
const loading = shallowRef(false)

const onCancel = () => {
    show.value = false
}

const onSubmit = () => {
    loading.value = true
    investorExport().then((res) => {
        const blob = base64ToBlob(res.data) // 接收文件流
        const fileName = new Date().getTime() + '.xlsx'
        downloadFile(blob, fileName)
    }).catch((err) => {
        ElMessage.error(err)
    }).finally(() => {
        onCancel()
    })
}
</script>