<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="saleOrderNumber">
				<a-input v-model:value="formData.saleOrderNumber" placeholder="请输入销售订单编号" allow-clear />
			</a-form-item>
			<a-form-item label="订单名称：" name="saleOrderName">
				<a-input v-model:value="formData.saleOrderName" placeholder="请输入销售订单名称" allow-clear />
			</a-form-item>
			<a-form-item label="客户信息：" name="customerId">
				<a-select v-model:value="formData.customerId" placeholder="请选择客户信息"
						  :options="customerIdList"
				> </a-select>
			</a-form-item>
			<a-form-item label="订单类型：" name="saleOrderType">
				<a-radio-group button-style="solid" v-model:value="formData.saleOrderType">
					<a-radio-button value="1">
						常规订单
					</a-radio-button>
					<a-radio-button value="2">
						服务订单
					</a-radio-button>
				</a-radio-group>
			</a-form-item>
			<a-form-item label="货品信息：" name="saleGoodsCode">
<!--				<a-input v-model:value="formData.saleGoodsName" placeholder="请输入货品名称" allow-clear />-->
				<a-select v-model:value="formData.saleGoodsCode" placeholder="请选择货品信息"
						  :options="saleGoodsCodeList"
				> </a-select>
			</a-form-item>
			<a-form-item label="订单重量：" name="saleOrderWeight">
				<a-input-number v-model:value="formData.saleOrderWeight" 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="bizSaleOrderForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizSaleOrderApi from '@/api/biz/bizSaleOrderApi'
	import customerApi from '@/api/biz/customerApi'
	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 customerIdList = ref()
	const saleGoodsCodeList = ref()

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}else{
			formData.value.saleOrderType = '1'
		}

		//查询客户信息
		customerApi.getList().then((res)=>{
			customerIdList.value = res.map((item)=>{
				return{
					value:item.id,
					label:item.name
				}
			})
		})
		//查询货品信息
		bizGoodsApi.getList().then((res)=>{
			saleGoodsCodeList.value = res.map((item)=>{
				return{
					value:item.goodsCode,
					label:item.goodsName+'-'+item.goodsCode
				}
			})
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		saleOrderNumber: [required('请输入销售订单编号')],
		saleOrderName: [required('请输入销售订单名称')],
		saleOrderType: [required('请选择订单类型')],
		saleGoodsCode: [required('请选择货品信息')],
		saleOrderWeight: [required('请输入销售订单重量')],
		customerId: [required('请选择客户信息')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				bizSaleOrderApi
					.bizSaleOrderSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
