<template>
	<xn-form-container
		title="修改照片"
		:width="800"
		:visible="visible"
		:destroy-on-close="true"
		:body-style="{ 'padding-top': '20px' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="照片" name="imgUrl">
				<x-img-upload @onImgChange="onImgChange"></x-img-upload>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="entranceWheel">
	import XImgUpload from "@/components/XnUpload/XImgUpload.vue";
	import { required } from "@/utils/formRules";
	import washEntranceApi from "@/api/wash/washEntranceApi";
	const visible = ref(false)
	const formRef = ref()
	const emit = defineEmits({ successful: null })
	const submitLoading = ref(false)
	const formData = ref({})
	const formRules = ref({
		imgUrl: [required('请上传照片')]
	})
	const onImgChange = (item) => {
		formData.value.imgUrl = item[0]
	}
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
		submitLoading.value = false
	}

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		formData.value.id = record.id
	}

	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			let data = new FormData()
			data.append('id', formData.value.id)
			data.append('imgUrl', formData.value.imgUrl)
			washEntranceApi.saveEntranceImg(data).then(() => {
				onClose()
				emit('successful')
			}).finally(() => {
				submitLoading.value = false
			})
		})
	}

	// 抛出函数
	defineExpose({
		onOpen
	})


</script>

<style scoped>

</style>
