<template>
	<xn-form-container
		:title="formData.id ? '编辑运费结算' : '增加运费结算'"
		:width="width"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" :label-col="labelCol" :wrapper-col="wrapperCol">
			<a-form-item label="结算类型：" name="settleType" v-show="isShow">
				<a-input v-model:value="formData.settleType" placeholder="请输入结算类型" allow-clear />
			</a-form-item>
			<a-form-item label="结算金额：" name="settleAccount" v-show="isShow">
				<a-input v-model:value="formData.settleAccount" placeholder="请输入结算金额" allow-clear />
			</a-form-item>
			<a-tooltip title="点击添加物流订单">
				<a-button size="small" @click="itemRef.showModal('checkbox', 'orderItem')"> 选择物流订单 </a-button>
			</a-tooltip>
			<table class="table">
				<tr>
					<td>订单编号</td>
					<td>客户名称</td>
					<td>货品名称</td>
					<td>货品编码</td>
					<td>供应商名称</td>
					<td>订单重量</td>
					<td>实际托运重量</td>
					<td>运费单价</td>
					<td>操作</td>
				</tr>
				<tr v-for="(order, index) in formData.orderList">
					<td>
						<a-tooltip :title="order.orderNumber">
							{{ order.orderNumber }}
						</a-tooltip>
					</td>
					<td>
						<a-tooltip :title="order.customerName">
							{{ order.customerName }}
						</a-tooltip>
					</td>
					<td>
						<a-tooltip :title="order.goodsName">
							{{ order.goodsName }}
						</a-tooltip>
					</td>
					<td>
						<a-tooltip :title="order.goodsCode">
							{{ order.goodsCode }}
						</a-tooltip>
					</td>
					<td>
						<a-tooltip :title="order.supplierName">
							{{ order.supplierName }}
						</a-tooltip>
					</td>
					<td>
						<a-tooltip :title="order.orderWeight">
							{{ order.orderWeight }}
						</a-tooltip>
					</td>
					<td>
						<a-tooltip :title="order.orderWeight">
							{{ order.orderType == '1'? order.shippingWeight:order.netWeight }}
						</a-tooltip>
					</td>
					<td>
						<a-tooltip :title="order.freightPrice">
							{{ order.freightPrice }}
						</a-tooltip>
					</td>
					<td>
						<a-button
							danger
							size="small"
							type="link"
							@click="deleteCount(index)"

						>
							<delete-outlined />
						</a-button>
					</td>
				</tr>
				<tr v-if="formData.orderList.length>0">
					<th colspan="1">合计</th>
					<td colspan="8">
						{{amountTotal}}
					</td>
				</tr>
			</table>
		</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>

		<OrderItem ref="itemRef" @orderItemCallBack="orderItemCallBack" />
	</xn-form-container>
</template>

<script setup name="bizSettleForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import { message, Modal } from 'ant-design-vue'
	import { ExclamationCircleOutlined } from '@ant-design/icons-vue'
	import { createVNode } from 'vue'
	import bizSettleApi from '@/api/biz/bizSettleApi'
	import OrderItem from './orderItem.vue'

	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const itemRef = ref()
	const amountTotal = ref()
	const isShow = ref(false)
	// 表单数据
	const formData = ref({orderList:[]})
	const submitLoading = ref(false)
	const width = ref('calc(70%)')
	const labelCol = ref({ span: 3 })
	const wrapperCol = ref({ span: 21 })

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			//let recordData = cloneDeep(record)
			//formData.value = Object.assign({}, recordData)
			bizSettleApi.detailOrder({id:record.id}).then((res)=>{
				formData.value = res
				amountTotal.value = 0
				formData.value.orderList.forEach((item) => {
					amountTotal.value = amountTotal.value + (item.orderType=='1' ? item.freightPrice*item.shippingWeight : item.freightPrice*item.netWeight)
				})
				formData.value.settleAccount = amountTotal.value
			})
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {orderList:[]}
		open.value = false
	}

	//删除明细
	const deleteCount = (index) =>{
		formData.value.orderList.splice(index, 1)
		amountTotal.value = 0
		formData.value.orderList.forEach((item) => {
			amountTotal.value = amountTotal.value + (item.orderType=='1' ? item.freightPrice*item.shippingWeight : item.freightPrice*item.netWeight)
		})
		formData.value.settleAccount = amountTotal.value
	}

	// 默认要校验的
	const formRules = {
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				bizSettleApi
					.bizSettleSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}

	const orderItemCallBack = (value) => {
		if (value && value.length > 0) {
			console.log('---this.value--->' + JSON.stringify(value))
			if (formData.value.orderList && formData.value.orderList.length > 0) {
				let map = new Map()
				const array = ref([])
				formData.value.orderList = formData.value.orderList.concat(value)
				formData.value.orderList.forEach((item) => {
					if (!map.has(item.id)) {
						map.set(item.id, item)
						array.value = array.value.concat(item)
					}
				})
				formData.value.orderList = array.value
			} else {
				formData.value.orderList = value
			}
			amountTotal.value = 0
			formData.value.orderList.forEach((item) => {
				amountTotal.value = amountTotal.value + (item.orderType=='1' ? item.freightPrice*item.shippingWeight : item.freightPrice*item.netWeight)
			})

			formData.value.settleType = '1'
			formData.value.settleAccount = amountTotal.value
		}
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>

<style scoped>
table {
	margin-top: 10px;
	text-align: center;
	width: 100%;
}

table td {
	border: 1px solid #f0f2f5;
	padding: 10px 0;
}
tr:hover {
	background-color: #f5f5f5;
}
</style>
