<template>
	<xn-form-container
		title="归还物资"
		:width="800"
		:visible="visible"
		:destroy-on-close="true"
		:body-style="{ 'padding-top': '0px' }"
		@close="onClose"
	>
		<div style="padding: 20px 0">
			<a-form ref="giveRef" :model="formData" :rules="formRules" layout="vertical">
				<a-form-item label="归还时间" name="returnTime">
					<a-date-picker v-model:value="formData.returnTime" :show-time="{ defaultValue: dayjs('00:00:00', 'HH:mm:ss') }" value-format="YYYY-MM-DD HH:mm:ss"  format="YYYY-MM-DD HH:mm:ss" />
				</a-form-item>
				<a-form-item label="照片" name="returnImgUrl">
					<x-img-upload @onImgChange="onImgChange"></x-img-upload>
				</a-form-item>
				<a-form-item label="备注" name="remarks">
					<a-textarea v-model:value="formData.remarks" placeholder="请填写备注" :rows="4" />
				</a-form-item>
			</a-form>
		</div>
		<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="wareGive">
	import dayjs from "dayjs"
	import bizWareApi from "@/api/biz/bizWareApi"
	import XImgUpload from "@/components/XnUpload/XImgUpload.vue";
	import { required } from '@/utils/formRules'
	import { cloneDeep } from "lodash-es"

	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const giveRef = ref()
	// 表单数据
	const formData = ref({})
	const treeData = ref([])
	const submitLoading = ref(false)

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


	// 关闭抽屉
	const onClose = () => {
		giveRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}

	const formRules = {
		returnTime: [required('请选择归还时间')],
		returnImgUrl: [required('请上传归还照片')]
	}

	const onImgChange = (item) => {
		if (item.length > 0) {
			formData.value.returnImgUrl = item[0]
		} else {
			formData.value.returnImgUrl = undefined
		}
	}

	// 验证并提交数据
	const onSubmit = () => {
		giveRef.value.validate().then(() => {
			let data = new FormData()
			data.append('id', formData.value.id)
			data.append('returnTime', formData.value.returnTime)
			data.append('returnImgUrl', formData.value.returnImgUrl)
			data.append('remarks', formData.value.remarks)
			bizWareApi.revertWare(data).then(() => {
				onClose()
				emit('successful')
			})
		})
	}

	// 抛出函数
	defineExpose({
		onOpen
	})
</script>

<style scoped>

</style>
