<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
	<div>
		<el-dialog :title="title" :close-on-click-modal="false" draggable width="1000px" height="500px" @close="close"
			append-to-body v-model="visible">
			<el-form :model="inputForm" ref="inputForm" :class="method === 'view' ? 'readonly' : ''"
				:disabled="status === 'audit' || status === 'taskFormDetail'" label-width="135px"
				@submit.native.prevent>
				<el-row :gutter="0">
				</el-row>
				<el-row :gutter="0">
					<el-col :span="24">
						<el-form-item label="附件上传：">
							<el-upload ref="upload" :on-remove="handleRemove" action="" :limit="1" :auto-upload="false"
								:on-change="beforeUploadDetail" :show-file-list="true">
								<el-button type="primary">点击上传Excel文件</el-button>
							</el-upload>
						</el-form-item>
					</el-col>
				</el-row>

			</el-form>


			<template #footer>
				<span class="dialog-footer">
					<el-button type="warning" @click="downloadTpl()" plain>下载模板</el-button>
					<el-button @click="close()" icon="el-icon-circle-close">关闭</el-button>
					<el-button v-if="method !== 'view'" type="primary" icon="el-icon-circle-check"
						@click="doSubmit()">确定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script>

import FinanceInvoiceService from '@/api/finance/invoice/FinanceInvoiceService'
const financeInvoiceService = new FinanceInvoiceService()
export default {
	data() {
		return {
			visible: false,
			title: '',
			inputForm: {
				file: []
			}
		}
	},
	created() {
	},
	components: {
	},
	methods: {
		handleRemove() {
			this.inputForm.file.pop()
			console.log(this.inputForm.file);
		},
		init() {
			this.visible = true
			this.title = '收款发票导入'
			this.inputForm = {
				file: []
			}
			this.$forceUpdate()
		},

		beforeUploadDetail(file) {
			this.inputForm.file.push(file)
		},

		// 下载模板
		downloadTpl() {
			const loading = this.$loading({
				lock: true,
				text: '模板下载中，请稍后',
				spinner: 'el-icon-loading',
				background: 'rgba(255, 255, 255, 0.3)'
			});
			financeInvoiceService.exportInvoiceTemplate().then((res) => {
				// 将二进制流文件写入excel表，以下为重要步骤
				this.$utils.downloadExcel(res, '收款发票批量导入模板' + ".xlsx")
				loading.close();
			}).catch(function (err) {
				loading.close();
				if (err.response) {
					console.log(err.response)
				}
			})
		},
		doSubmit() {
			if (this.inputForm.file.length == 0) {
				this.$message.warning('未上传文件，无法提交导入')
				return
			}
			const loading = this.$loading({
				lock: true,
				text: '导入中，请稍后',
				spinner: 'el-icon-loading',
				background: 'rgba(255, 255, 255, 0.3)'
			});
			const formBody = new FormData()
			formBody.append('file', this.inputForm.file[0].raw)

			financeInvoiceService.importProjectRecords(formBody).then((data) => {
				loading.close();

				console.log("后端返回数据：", data); // 调试看有没有返回
				console.log("excelFileName：", data?.excelFileName);

				// ==========================================
				// 关键：只要有文件名，就强制触发父页面事件
				// ==========================================
				if (data && data.excelFileName) {
					this.$emit('import-error', data);
					this.$message.warning("导入存在异常，请下载查看");
					this.close(); // ✅ 加这一行，子弹窗自动关闭，父页面弹框更清晰
					return;
				}

				// 正常成功
				this.$message.success('导入成功');
				this.close();
				this.$emit("refreshDataList");

			}).catch(err => {
				loading.close();
				this.$message.error('导入失败：服务异常');
			})
		},
		close() {
			this.visible = false
			this.$refs.inputForm.resetFields()
			this.$refs.upload.clearFiles();
			this.inputForm = {
				file: []
			}
		}

	}
}
</script>

<style scoped>
/deep/ .el-input-number .el-input__inner {
	text-align: left;
}

.el-row {
	display: flex;
	align-items: center;
	/* 垂直居中 */
}
</style>
