<template>
	<xn-form-container
		:title="formData.id ? '编辑订单管理' : '增加订单管理'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" :wrapper-col="wrapperCol" :label-col="labelCol">
			<a-form-item label="订单编号：" name="orderNumber">
				<a-input v-model:value="formData.orderNumber" placeholder="请输入订单编号" allow-clear />
			</a-form-item>
			<a-form-item label="订单名称：" name="orderName">
				<a-input v-model:value="formData.orderName" placeholder="请输入订单名称" allow-clear />
			</a-form-item>
			<a-form-item label="货品信息：" name="goodId">
				<a-select v-model:value="formData.goodId" placeholder="请选择货品信息"
						  :options="goodIdList"
				> </a-select>
			</a-form-item>
			<a-form-item label="订单类型：" name="orderType">
				<a-select v-model:value="formData.orderType" placeholder="请选择订单类型"
						  :options="orderTypeList"
				> </a-select>
			</a-form-item>
			<a-form-item label="订单重量：" name="orderWeight">
				<a-input-number v-model:value="formData.orderWeight" style="width:90%"  :precision="0" :min="1" :max="999999"  placeholder="请输入订单重量" allow-clear /><span style="margin-left:10px;">吨</span>
			</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="bizOrderForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizOrderApi from '@/api/biz/bizOrderApi'
	import tool from '@/utils/tool'
	import bizGoodsApi from '@/api/biz/bizGoodsApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)

	//设置表单样式
	const labelCol = ref({ span: 5})
	const wrapperCol = ref({ span: 16})

	//订单类型
	const orderTypeList = tool.dictList('order_type')
	const goodIdList = ref()

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		bizGoodsApi.getList().then((res)=>{
			goodIdList.value = res.map((item)=>{
				return{
					value:item.id,
					label:item.model? (item.name+'-'+item.model):item.name
				}
			})
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		orderNumber: [required('请输入订单编号')],
		orderName: [required('请输入订单名称')],
		goodId: [required('请选择货品信息')],
		orderWeight: [required('请输入订单重量')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				bizOrderApi
					.bizOrderSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
