<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="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="orderType" style="display:none">
				<a-select v-model:value="formData.orderType" placeholder="请选择订单类型"
						  :options="orderTypeList"
				> </a-select>
			</a-form-item>-->
			<a-form-item label="订单类型：" name="orderType" >
				<a-radio-group button-style="solid" v-model:value="formData.orderType" disabled>
					<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="deliveryTimeId" >
				<a-select v-model:value="formData.deliveryTimeId" placeholder="请选择提货时间"
						  :options="deliveryTimeIdList"
				> </a-select>
			</a-form-item>
			<a-form-item label="供货商：" name="supplierIdList">
				<a-select v-model:value="formData.supplierIdList" placeholder="请选择供货商信息"
						  :options="supplierIds" mode="tags"
				> </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="goodsName">
				<a-input v-model:value="formData.goodsName" placeholder="请输入货品名称" allow-clear disabled/>
			</a-form-item>
			<a-form-item label="货品编码：" name="goodsCode">
				<a-input v-model:value="formData.goodsCode" placeholder="请输入货品编码" allow-clear disabled/>
			</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 disabled/><span style="margin-left:10px;">吨</span>
			</a-form-item>
			<a-form-item label="单价：" name="price">
				<a-input v-model:value="formData.price" style="width:90%" placeholder="请输入单价" allow-clear disabled/><span style="margin-left:10px;">元/吨</span>
			</a-form-item>
			<a-form-item label="业务员：" name="businessName">
				<a-input v-model:value="formData.businessName" placeholder="请输入业务员" allow-clear disabled/>
			</a-form-item>
			<a-form-item label="送货地址：" name="shipAddress">
				<a-input v-model:value="formData.shipAddress" placeholder="请输入送货地址" allow-clear disabled/>
			</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";
	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 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 supplierIds = 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
				}
			})
		})

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

		/*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.queryByErp({id:formData.value.customerId,flag:formData.value.id?"edit":"add"}).then((res)=>{
				saleOrderInfoList.value = res.map((item)=>{
					return{
						value:item.id,
						label:item.saleOrderNumber
					}
				})
			})
		}

		if(formData.value.saleOrderInfo){
			bizSaleOrderApi.detailById({id:formData.value.saleOrderInfo}).then((res)=>{
				//根据货品名称和重量查询提货时间段
				goodsConfApi.getList({goodsName:res.saleGoodsName,goodsCode:res.goodsCode,needWeight:res.saleOrderWeight}).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 = ''
		saleOrderInfoList.value = ''
		bizSaleOrderApi.queryByErp({id:formData.value.customerId,flag:formData.value.id?"edit":"add"}).then((res)=>{
			saleOrderInfoList.value = res.map((item)=>{
				return{
					value:item.id,
					label:item.saleOrderNumber
				}
			})
		})
	}

	//订单点击时间
	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.goodsCode = res.saleGoodsCode
			formData.value.orderWeight = res.saleOrderWeight
			formData.value.orderType = res.saleOrderType
			formData.value.shipAddress = res.shipAddress
			formData.value.price = res.salePrice
			formData.value.businessName  = res.businessName
			formData.value.deliveryTimeId = ''
			//根据货品名称和重量查询提货时间段
			console.log("goodsName:"+formData.value.goodsName)
			console.log("needWeight:"+formData.value.orderWeight)
			goodsConfApi.getList({goodsName:formData.value.goodsName,goodsCode:formData.value.goodsCode,needWeight:formData.value.orderWeight}).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('请输入订单重量')],
		orderType: [required('请选择订单类型')],
		supplierIdList: [required('请选择供货商信息')],
		freightPrice: [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>

<style>
/* 修改禁用状态下的颜色 */
.ant-radio-button-wrapper-disabled {
	color: black !important; /* 设置文字颜色 */
	background-color: #f0f0f0 !important; /* 设置背景颜色 */
}

/* 修改禁用状态下选中项的颜色 */
.ant-radio-button-wrapper-checked.ant-radio-button-wrapper-disabled {
	color: white !important; /* 设置文字颜色 */
	background-color: blue !important; /* 设置选中项的背景颜色 */
}
</style>
