<template>
	<a-drawer
		:title="formData.id ? '编辑飞行监管异常日志' : '增加飞行监管异常日志'"
		: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="siteType">
				<a-input v-model:value="formData.siteType" disabled/>
			</a-form-item>
			<a-form-item label="监管图片：" name="sitePicUrl">
				<a-upload
					v-model:file-list="formData.sitePicUrls"
					class="avatar-uploader"
					list-type="picture-card"
					:show-upload-list="false"
					:custom-request="customRequest"
					accept="image/png, image/jpeg, image/jpg"
					disabled
				>
					<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-item label="监管信息：" name="siteMsg">
				<a-textarea v-model:value="formData.siteMsg" placeholder="请输入监管信息" :auto-size="{ minRows: 3, 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 label="处理图片：" name="savePicUrl">
				<a-upload
					v-model:file-list="formData.savePicUrls"
					class="avatar-uploader"
					list-type="picture-card"
					:show-upload-list="false"
					:custom-request="customRequestTwo"
					accept="image/png, image/jpeg, image/jpg"
				>
					<img v-if="imageUrlTwo" :src="imageUrlTwo" 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-item label="处理信息：" name="saveMsg">
				<a-textarea v-model:value="formData.saveMsg" placeholder="请输入处理信息" :auto-size="{ minRows: 3, maxRows: 10 }"/>
			</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>
	</a-drawer>
</template>

<script setup name="depotFlyerSiteLogWarnForm">
import tool from '@/utils/tool'
import {cloneDeep} from 'lodash-es'
import {required, rules} from '@/utils/formRules'
import depotFlyerSiteLogWarnApi from '@/api/biz/depotFlyerSiteLogWarnApi'
import sysConfig from "@/config";
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 imageUrlTwo = ref('')

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
		if (formData.value.sitePicUrl) {
			// 让其回显
			imageUrl.value = sysConfig.API_URL + formData.value.sitePicUrl
			formData.value.sitePicUrls = [imageUrl.value]
		}
		if (formData.value.savePicUrl) {
			// 让其回显
			imageUrlTwo.value = sysConfig.API_URL + formData.value.savePicUrl
			formData.value.savePicUrls = [imageUrlTwo.value]
		}
	}
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	imageUrl.value = ''
	imageUrlTwo.value = ''
	visible.value = false
}
// 默认要校验的
const formRules = {
	saveState: [required('请选择')],
}
// 验证并提交数据
const onSubmit = () => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			depotFlyerSiteLogWarnApi
				.depotFlyerSiteLogWarnSubmitForm(formDataParam, !formDataParam.id)
				.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.sitePicUrl = result.toString().substring(result.toString().length - 19)
				})
		})
		.catch((err) => {
		})
}
const customRequestTwo = (data) => {
	formData.value.savePicUrls = ref([])
	getBase64(data.file)
		.then((res) => {
			imageUrlTwo.value = res
			formData.value.savePicUrls.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>
