<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 disabled/>
			</a-form-item>
			<a-form-item label="订单名称：" name="orderName">
				<a-input v-model:value="formData.orderName" placeholder="请输入订单名称" allow-clear disabled/>
			</a-form-item>
			<a-form-item label="货品名称：" name="goodsName">
				<a-input v-model:value="formData.goodsName" placeholder="请输入货品名称" allow-clear disabled/>
			</a-form-item>
			<a-form-item label="客户信息：" name="customerId">
				<a-select v-model:value="formData.customerId" placeholder="请选择客户信息"
						  :options="customerIdList" @change="onChangeCustomer"
				> </a-select>
			</a-form-item>
			<a-form-item label="销售订单信息：" name="saleOrderInfo">
				<a-select v-model:value="formData.saleOrderInfo" placeholder="请选择订单信息"
						  :options="saleOrderInfoList" @change="onChangeSaleOrder"
				> </a-select>
			</a-form-item>
			<a-form-item label="提货时间：" name="deliveryTimeId" >
				<a-select v-model:value="formData.deliveryTimeId" placeholder="请选择提货时间"
						  :options="deliveryTimeIdList"
				> </a-select>
			</a-form-item>
			<a-form-item label="订单类型：" name="orderType" style="display:none">
				<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="2" :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'
	import customerApi from '@/api/biz/customerApi'
	import goodsConfApi from '@/api/biz/goodsConfApi'
	import bizSaleOrderApi from "@/api/biz/bizSaleOrderApi";
	// 抽屉状态
	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 customerIdList = ref()
	const saleOrderInfoList = ref()
	const deliveryTimeIdList = ref()

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		//查询客户信息
		customerApi.getList().then((res)=>{
			customerIdList.value = res.map((item)=>{
				return{
					value:item.id,
					label:item.name
				}
			})
		})

		/*if(formData.value.customerId){
			customerApi.getOrderByCustomerId({id:formData.value.customerId}).then((res)=>{
				saleOrderInfoList.value = res.orderList.map((item)=>{
					return{
						value:item.orderName+'-'+item.orderNumber+'-'+item.goodsName+'-'+item.orderWeight+'-'+item.orderType,
						label:item.orderName
					}
				})
			})
		}*/
		if(formData.value.customerId){
			bizSaleOrderApi.queryByCustomerId({id:formData.value.customerId,flag:formData.value.id?"edit":"add"}).then((res)=>{
				saleOrderInfoList.value = res.map((item)=>{
					return{
						value:item.id,
						label:item.saleOrderName
					}
				})
			})
		}

		if(formData.value.saleOrderInfo){
			//根据货品名称和重量查询提货时间段
			goodsConfApi.getList({goodsName:formData.value.saleOrderInfo.split('-')[2],needWeight:formData.value.saleOrderInfo.split('-')[3]}).then((res)=>{
				deliveryTimeIdList.value = res.map((item)=>{
					return{
						value:item.id,
						label:item.confStartTime+'~'+item.confEndTime
					}
				})
			})
		}
	}

	//客户点击时间
	const onChangeCustomer =(value)=>{
		formData.value.saleOrderInfo = ''
		formData.value.deliveryTimeId = ''
		bizSaleOrderApi.queryByCustomerId({id:formData.value.customerId,flag:formData.value.id?"edit":"add"}).then((res)=>{
			saleOrderInfoList.value = res.map((item)=>{
				return{
					value:item.id,
					label:item.saleOrderName
				}
			})
		})
	}

	//订单点击时间
	const onChangeSaleOrder = (value) =>{
		bizSaleOrderApi.detailById({id:value}).then((res)=>{
			formData.value.orderName = res.saleOrderName
			formData.value.orderNumber= res.saleOrderNumber
			formData.value.goodsName = res.saleGoodsName
			formData.value.orderWeight = res.saleOrderWeight
			formData.value.orderType = res.saleOrderType
		})
		formData.value.deliveryTimeId = ''
		//根据货品名称和重量查询提货时间段
		goodsConfApi.getList({goodsName:value.split('-')[2],needWeight:value.split('-')[3]}).then((res)=>{
			deliveryTimeIdList.value = res.map((item)=>{
				return{
					value:item.id,
					label:item.confStartTime+'~'+item.confEndTime
				}
			})
		})
	}

	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
		goodIdList.value = {}
		customerIdList.value = {}
		saleOrderInfoList.value = {}
		deliveryTimeIdList.value = {}
	}
	// 默认要校验的
	const formRules = {
		//orderNumber: [required('请输入订单编号')],
		//orderName: [required('请输入订单名称')],
		customerId:[required('请选择客户信息')],
		saleOrderInfo:[required('请选择订单信息')],
		deliveryTimeId: [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>
