<template>
    <app-drawer :title="t('common.alert')" v-model:show="show" :loading="loading">
        <div class="g-text-message">确定导出表格数据？</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, PropType } from 'vue'
import { ElMessage } from 'element-plus'
import { base64ToBlob, downloadFile } from '@/filters'
import { positionExport } from '@/services/api/order'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import { i18n } from '@/stores'

const props = defineProps({
    queryParams: {
        type: Object as PropType<Model.ContractSheetReq>,
        required: true
    }
})

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

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

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