<template xmlns:v-slot="http://www.w3.org/1999/XSL/Transform">
	<div>
		<el-dialog title="批量下载附件" :close-on-click-modal="false" draggable width="600px" height="500px" @close="close"
			append-to-body v-model="visible" center align-center>
			<el-form :model="inputForm" ref="inputForm" v-loading="loading" :rules="rules" label-width="100px"
				@submit.native.prevent>
				<el-form-item label="下载附件" prop="attachmentType">
					<el-select size="default" v-model="inputForm.attachmentType" placeholder="请选择下载附件类型"
						style="width: 100%;">
						<el-option v-for="item in $dictUtils.getDictList('attachment_type')" :key="item.value"
							:label="item.label" :value="item.value">
						</el-option>
					</el-select> </el-form-item>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="close()" icon="el-icon-circle-close">关闭</el-button>
					<el-button type="primary" icon="el-icon-circle-check" @click="doSubmit()">确定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script>
import enrollmentRegistrationService from '@/api/human/enrollment/EnrollmentRegistrationService'
export default {
	data() {
		return {
			disabled: false,
			title: '',
			method: '',
			visible: false,
			loading: false,
			ids: "",
			inputForm: {
				attachmentType: ""
			},
			keyWatch: '',
			rules: {
				attachmentType: [
					{ required: true, message: '请选择下载附件类型', trigger: 'blur' }
				],

			}
		}
	},
	computed: {
		name() {
			return this.$store.state.user.name
		},
		userId() {
			return this.$store.state.user.id
		}
	},
	watch: {
		'loading': {
			handler(newVal) {
				this.$emit('changeLoading', newVal)
			}
		}
	},
	methods: {
		// 表单提交
		doSubmit() {
			this.$refs['inputForm'].validate((valid) => {
				if (valid) {
					this.loading = true
					enrollmentRegistrationService.downloadAttachmentById({
						ids: this.ids,
						attachmentType: this.inputForm.attachmentType
					}).then(res => {
						if (res.type == "application/json") {
							const reader = new FileReader()
							reader.readAsText(res, 'utf-8')
							reader.onload = () => {
								const result = JSON.parse(reader.result)
								this.$message.error(result.msg)
								this.close()
								this.loading = false
							}
							return
						}
						let fileName = this.$dictUtils.getDictLabel("attachment_type", this.inputForm.attachmentType,
							'用户信息')
						this.$utils.downloadWord(res, fileName + '.zip')
						this.$message.success("操作成功")
						this.close()
						this.loading = false
					}).catch(err => {
						this.loading = false
					})
				}
			})
		},
		close() {
			this.$refs.inputForm.resetFields()
			this.ids = ""
			this.visible = false
		},
	}
}
</script>


<style lang="less">
.fileUpload {
	.el-form-item__content {
		width: 100%;
	}
}
</style>
