<template>
	<xn-form-container
		:title="'添加提货配置'"
		: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-row :gutter="24">
				<a-col :span="24">
					<a-form-item label="提货时间段：" name="confStartEndTime">
						<a-range-picker v-model:value="formData.confStartEndTime" value-format="YYYY-MM-DD HH:mm" show-time style="width: 100%" />
					</a-form-item>
				</a-col>
			</a-row>

			<!-- 批量添加货物 -->
			<a-form-item label="货物信息" name="goodsJson">
				<a-row :gutter="24" class="form-row">
					<a-col :span="12" class="form-row-con"> 货物 </a-col>
					<a-col :span="8" class="form-row-con"> 最大提货重量(KG) </a-col>
					<a-col :span="4" class="form-row-con">
						<a-button type="primary" @click="addDomains()" size="small">
							<PlusOutlined />
							增加
						</a-button>
					</a-col>
				</a-row>
				<div :key="goodsInfo" v-for="(goodsInfo, index) in formData.goodsJson">
					<a-row :gutter="24">
						<a-col :span="12">
							<a-form-item
								:name="['goodsJson', index, 'goodsId']"
								:rules="{ required: true, message: '请选择货物' }"
							>
								<a-select
									v-model:value="goodsInfo.goodsId"
									:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
									:options="goodsList"
									class="xn-wd"
									placeholder="请选择货物"
								/>
							</a-form-item>
						</a-col>
						<a-col :span="8">
							<a-form-item
								:name="['goodsJson', index, 'confWeight']"
								:rules="{ required: true, message: '请输入最大提货重量' }"
							>
								<a-input v-model:value="goodsInfo.confWeight" placeholder="请输入重量" allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="4" class="xn-mt4">
							<a-button size="small" type="primary" danger ghost @click="delDomains(index)">移除</a-button>
						</a-col>
					</a-row>
				</div>
			</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="goodsConfForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import goodsApi from '@/api/biz/goodsApi'
	import goodsConfApi from '@/api/biz/goodsConfApi'

	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const goodsList = ref([])

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

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

		// 获取数据库中的所有表
		goodsApi.getList().then((data) => {
			goodsList.value = data.map((item) => {
				return {
					value: item['id'],
					label: item['goodsName']+" - "+item['goodsModel']
				}
			})
		}).finally(() => {
			submitLoading.value = false
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		confStartEndTime: [required('请选择提货时间段')],
		goodsJson: [required('请选择货物')],
	}

	// 附属职位信息增行
	const addDomains = () => {
		if (formData.value.goodsJson === null) {
			formData.value.goodsJson = []
		}
		formData.value.goodsJson.push({
			goodsId: undefined,
			confWeight: undefined
		})
	}
	// 删减行
	const delDomains = (index) => {
		formData.value.goodsJson.splice(index, 1)
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				// 因为不切断，我下面转换数据格式，影响上面表单会报错
				let formDatas = JSON.parse(JSON.stringify(formData.value))
				// 判断货品信息：至少选择一个
				if (formDatas.goodsJson && formDatas.goodsJson.length > 0) {
					formDatas.goodsJson = JSON.stringify(formDatas.goodsJson)

					const formDataParam = cloneDeep(formData.value)
					submitLoading.value = true

					formDatas.confStartTime = formDataParam.confStartEndTime[0]+":00"
					formDatas.confEndTime = formDataParam.confStartEndTime[1]+":00"
					goodsConfApi
						.goodsConfSubmitForm(formDatas, formDataParam.id)
						.then(() => {
							onClose()
							emit('successful')
						})
						.finally(() => {
							submitLoading.value = false
						})
				} else {
					// 不允许提交  提示信息

				}
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>

<style scoped lang="less">
	.form-row {
		background-color: var(--item-hover-bg);
		margin-left: 0 !important;
		margin-bottom: 10px;
	}
	.form-row-con {
		padding-bottom: 5px;
		padding-top: 5px;
		padding-left: 15px;
	}
</style>
