<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">
			<template v-if="formData.id">
				<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>
				<template v-if="formData.alarmFlag === '是'">
					<a-form-item label="处理信息：" name="saveMsg">
						<a-textarea v-model:value="formData.saveMsg" :auto-size="{ minRows: 3, maxRows: 10 }" disabled/>
					</a-form-item>
					<a-form-item label="处理时间：" name="saveTime">
						<a-input v-model:value="formData.saveTime" disabled/>
					</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"
							disabled
						>
							<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="processPass">
						<a-radio-group v-model:value="formData.processPass" placeholder="请选择" :options="[{label:'未通过',value:'未通过'},{label:'已通过',value:'已通过'}]"/>
					</a-form-item>
				</template>
			</template>
			<template v-else>
				<a-form-item label="企业客户：" name="depotId">
					<a-select v-model:value="formData.depotId" placeholder="请选择企业客户" :options="depotIdOptions"/>
				</a-form-item>
				<a-form-item label="监管类型：" name="siteType">
					<a-select v-model:value="formData.siteType" placeholder="请选择监管类型" :options="siteTypeOptions"/>
				</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"
					>
						<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>
				<template v-if="formData.siteType === '固定监管员在岗情况'">
					<a-form-item label="是否在岗：" name="alarmFlag">
						<a-radio-group v-model:value="formData.alarmFlag" placeholder="请选择是否异常" :options="[{label:'不在岗',value:'是'},{label:'在岗',value:'否'}]"/>
					</a-form-item>
				</template>
				<template v-else>
					<a-form-item label="是否异常：" name="alarmFlag">
						<a-radio-group v-model:value="formData.alarmFlag" placeholder="请选择是否异常" :options="alarmFlagOptions"/>
					</a-form-item>
					<a-form-item label="监管信息：" name="siteMsg">
						<a-textarea v-model:value="formData.siteMsg" placeholder="请输入监管信息" :auto-size="{ minRows: 8, maxRows: 20 }" allow-clear/>
					</a-form-item>
				</template>
			</template>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button v-if="formData.saveState === '处理中'" type="primary" @click="onSubmit" :loading="submitLoading">已处理</a-button>
			<a-button v-else type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</a-drawer>
</template>

<script setup name="depotFlyerSiteLogForm">
import tool from '@/utils/tool'
import {cloneDeep} from 'lodash-es'
import {required} from '@/utils/formRules'
import depotFlyerSiteLogApi from '@/api/biz/depotFlyerSiteLogApi'
import dictOptionsApi from "@/api/biz/dictOptionsApi";
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 depotIdOptions = ref([])
const siteTypeOptions = ref([])
const alarmFlagOptions = ref([])
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.alarmFlag === '是') {
			if (!formData.value.saveState || formData.value.saveState === '待处理') {
				formData.value.saveState = '处理中'
			} else if (formData.value.saveState === '处理中') {
				formData.value.saveState = '已处理'
			}
		}
		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]
		}
	} else {
		formData.value.alarmFlag = '否'
		dictOptionsApi.options(Object.assign('', {'dictValue': '粮库'})).then((data) => {
			depotIdOptions.value = data
		})
	}
	siteTypeOptions.value = tool.dictList('飞行监管类型')
	alarmFlagOptions.value = tool.dictList('是否判断')
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	imageUrl.value = ''
	imageUrlTwo.value = ''
	visible.value = false
}
// 默认要校验的
const formRules = {
	depotId: [required('请选择')],
	siteType: [required('请输入')],
	alarmFlag: [required('请输入')],
	processPass: [required('请选择')],
}
// 验证并提交数据
const onSubmit = () => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			if (formDataParam.siteType === '固定监管员在岗情况')
				formDataParam.siteMsg = formDataParam.alarmFlag === '是' ? '不在岗' : '在岗'
			depotFlyerSiteLogApi
				.depotFlyerSiteLogSubmitForm(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>
