<template>
	<a-drawer
		:title="'出入库异常处理'"
		:width="600"
		:visible="visible"
		:destroy-on-close="true"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="异常信息：" name="alarmMsg">
				<a-textarea v-model:value="formData.alarmMsg" :auto-size="{ minRows: 5, maxRows: 10 }" disabled/>
			</a-form-item>
			<a-form-item label="处理状态：" name="saveState">
				<a-radio-group v-model:value="formData.saveState" :options="[{label:'驳回离场',value:'驳回离场'},{label:'允许通行',value:'允许通行'}]"/>
			</a-form-item>
			<a-form-item v-if="formData.saveState === '驳回离场' && formData.flowType === '8'">
				<div style="color: red">※ 驳回离场时，需要引导车辆装卸货并恢复到一次过磅时的重量，然后立即出场，此次交易数据将作废。请全程跟踪处理。</div>
			</a-form-item>
			<a-form-item label="处理信息：" name="saveMsg">
				<a-textarea v-model:value="formData.saveMsg" placeholder="请输入处理信息" :auto-size="{ minRows: 5, maxRows: 10 }"/>
			</a-form-item>
			<a-form-item label="处理图片：" name="savePicUrl">
				<a-upload
					v-model:file-list="formData.savePicUrls"
					class="avatar-uploader"
					list-type="picture-card"
					:show-upload-list="false"
					:custom-request="customRequest"
					accept="image/png, image/jpeg, image/jpg"
				>
					<img v-if="imageUrl" :src="imageUrl" alt="avatar" style="max-height: 100px; max-width: 100px"/>
					<div v-else>
						<plus-outlined/>
						<div class="ant-upload-text">上传</div>
					</div>
				</a-upload>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button style="margin-right: 8px" type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</a-drawer>
</template>

<script setup name="depotWorkAlarmForm">
import tool from '@/utils/tool'
import {cloneDeep} from 'lodash-es'
import {required} from '@/utils/formRules'
import depotWorkAlarmApi from '@/api/biz/depotWorkAlarmApi'
import dictOptionsApi from "@/api/biz/dictOptionsApi";
import depotSamplingApi from "@/api/biz/depotSamplingApi";
import fileApi from "@/api/dev/fileApi";
// 抽屉状态
const visible = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)
const imageUrl = ref('')

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
	}
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	imageUrl.value = ''
	visible.value = false
}
// 默认要校验的
const formRules = {
	depotId: [required('请输入项目编码')],
	depotBizType: [required('请输入出入库:入库/出库')],
}
// 验证并提交数据
const onSubmit = () => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			depotWorkAlarmApi
				.depotWorkAlarmProcessTo(formDataParam)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
}
const customRequest = (data) => {
	formData.value.sitePicUrls = ref([])
	getBase64(data.file)
		.then((res) => {
			imageUrl.value = res
			formData.value.sitePicUrls.push(res)
			//保存图片
			const fileData = new FormData()
			fileData.append('file', data.file)
			fileApi
				.fileUploadLocalReturnUrl(fileData)
				.then((result) => {
					formData.value.savePicUrl = result.toString().substring(result.toString().length - 19)
				})
		})
		.catch((err) => {
		})
}
// 文件转base64，用于显示图片
const getBase64 = (file) => {
	return new Promise((resolve, reject) => {
		// FileReader类就是专门用来读文件的
		const reader = new FileReader()
		reader.readAsDataURL(file)
		// 成功和失败返回对应的信息，reader.result一个base64，可以直接使用
		reader.onload = () => resolve(reader.result)
		// 失败返回失败的信息
		reader.onerror = (error) => reject(error)
	})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
