<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="customerId">
				<a-select v-model:value="formData.customerId" placeholder="请选择客户信息"
						  :options="customerIdList"
				> </a-select>
			</a-form-item>
			<a-form-item label="货品信息：" name="goodsId">
				<a-select v-model:value="formData.goodsId" placeholder="请选择货品信息"
						  :options="goodsIdList"
				> </a-select>
			</a-form-item>
			<a-form-item label="订单数量：" name="orderWeight">
				<a-input-number v-model:value="formData.orderWeight" style="width:90%"  :precision="2" :min="0.01" :max="9999999"  placeholder="请输入订单数量" @blur="getTotalAmount" allow-clear/><span style="margin-left:10px;">吨</span>
			</a-form-item>
			<a-form-item label="单价：" name="orderPrice">
				<a-input-number v-model:value="formData.orderPrice" style="width:90%"  :precision="2" :min="0.01" :max="999999"  placeholder="请输入单价" @blur="getTotalAmount" allow-clear/><span style="margin-left:10px;">元/吨</span>
			</a-form-item>
			<a-form-item label="订单总额：" name="orderAmount">
				<a-input-number v-model:value="formData.orderAmount" style="width:90%"  :precision="2" :min="0.01" :max="9999999"  placeholder="请输入订单总额" allow-clear/><span style="margin-left:10px;">元</span>
			</a-form-item>
			<a-form-item label="船运供应商：" name="supplierId">
				<a-select v-model:value="formData.supplierId" placeholder="请选择船运供应商"
						  :options="supplierIds"
				> </a-select>
			</a-form-item>
			<a-form-item label="运费单价：" name="freightPrice">
				<a-input-number v-model:value="formData.freightPrice" style="width:90%"  :precision="2" :min="0.01" :max="999999"  placeholder="请输入运费单价" allow-clear/><span style="margin-left:10px;">元/吨</span>
			</a-form-item>
			<a-form-item label="支付方式：" name="payType">
				<a-radio-group button-style="solid" v-model:value="formData.payType">
					<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="orderType">
				<a-radio-group button-style="solid" v-model:value="formData.orderType">
					<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="supplierIdList">
				<a-select v-model:value="formData.supplierIdList" placeholder="请选择汽运供应商"
						  :options="supplierIdLists" mode="tags"
				> </a-select>
			</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="bizLoadAppointForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizLoadAppointApi from '@/api/biz/bizLoadAppointApi'
	import bizServiceCustomerApi from '@/api/biz/bizServiceCustomerApi'
	import bizGoodsApi from "@/api/biz/bizGoodsApi";
	import bizSupplierApi from "@/api/biz/bizSupplierApi";

	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const customerIdList = ref()
	const goodsIdList = ref()
	const supplierIds = ref()
	const supplierIdLists = ref()

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

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

		//查询服务客户信息
		bizServiceCustomerApi.getList().then((res)=>{
			customerIdList.value = res.map((item)=>{
				return{
					value:item.id,
					label:item.name
				}
			})
		})

		//查询货品信息
		bizGoodsApi.getList().then((res)=>{
			goodsIdList.value = res.map((item)=>{
				return{
					value:item.id,
					label:item.goodsName+'-'+item.goodsCode
				}
			})
		})

		//查询船运供货商信息
		bizSupplierApi.getList({transportType:'2'}).then((res)=>{
			supplierIds.value = res.map((item)=>{
				return{
					value:item.id,
					label:item.supplierName
				}
			})
		})

		//查询汽运供货商信息
		bizSupplierApi.getList({transportType:'1'}).then((res)=>{
			supplierIdLists.value = res.map((item)=>{
				return{
					value:item.id,
					label:item.supplierName
				}
			})
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		customerId:[required('请选择客户信息')],
		goodsId:[required('请选择货品信息')],
		orderWeight:[required('请输入订单数量')],
		orderPrice:[required('请输入单价')],
		orderAmount:[required('请输入订单总额')],
		supplierId:[required('请选择船运供应商')],
		freightPrice:[required('请输入运费单价')],
		payType:[required('请选择支付方式')],
		orderType:[required('请选择订单类型')],
		supplierIdList:[required('请选择汽运供应商')],
	}

	//获取订单总额
	const getTotalAmount = () => {
		console.log("gfdlkgjkfl")
		if(formData.value.orderWeight!=null && formData.value.orderWeight!='' &&
			formData.value.orderPrice!=null && formData.value.orderPrice!=''){
			formData.value.orderAmount = (formData.value.orderWeight * formData.value.orderPrice).toFixed(2)
		}
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				bizLoadAppointApi
					.bizLoadAppointSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
