<template>
	<a-drawer
		:title="'人工补单'"
		:width="800"
		:visible="visible"
		:destroy-on-close="true"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-card title="预约信息 (请选择车牌号码)" :bordered="false">
				<a-row :gutter="24">
					<a-col :span="8">
						<a-form-item label="企业客户：" name="depotGrainName">
							<a-input v-model:value="formData.depotGrainName" disabled/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="出入库：" name="depotBizType">
							<a-input v-model:value="formData.depotBizType" placeholder="" disabled/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="粮食种类：" name="grainGroup">
							<a-input v-model:value="formData.grainGroup" placeholder="" disabled/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="车牌号码：" name="licensePlate">
							<a-select v-model:value="formData.licensePlate" placeholder="" @change="putLicensePlate" allow-clear
									  :options="licensePlateOptions"/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="后车牌：" name="licensePlateBack">
							<a-input v-model:value="formData.licensePlateBack" placeholder="" disabled allow-clear/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="粮食类型：" name="grainType">
							<a-input v-model:value="formData.grainType" placeholder="" disabled/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="司机姓名：" name="driverName">
							<a-input v-model:value="formData.driverName" placeholder="" allow-clear disabled/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="司机电话：" name="driverMobile">
							<a-input v-model:value="formData.driverMobile" placeholder="" allow-clear disabled/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="预约编号：" name="truckBookId">
							<a-input v-model:value="formData.truckBookId" placeholder="" disabled/>
						</a-form-item>
					</a-col>
				</a-row>
			</a-card>
			<a-card title="过磅信息" :bordered="false">
				<a-row :gutter="24">
					<a-col :span="8">
						<a-form-item label="车牌颜色：" name="plateColor">
							<a-select v-model:value="formData.plateColor" placeholder="请输入车牌颜色" allow-clear :options="plateColorOptions"/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="毛重(公斤)：" name="grossWeight">
							<a-input-number v-model:value="formData.grossWeight" placeholder="请输入毛重" style="width: 100%" @change="putNetWeight"
											allow-clear/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="过毛时间：" name="grossTime">
							<a-date-picker v-model:value="formData.grossTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择过毛时间"
										   style="width: 100%"/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="库位：" name="shedName">
							<a-select v-model:value="formData.shedName" placeholder="请选择库位" :options="depotShedOptions"/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="皮重(公斤)：" name="tareWeight">
							<a-input-number v-model:value="formData.tareWeight" placeholder="请输入皮重" style="width:100%" @change="putNetWeight" allow-clear/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="过皮时间：" name="tareTime">
							<a-date-picker v-model:value="formData.tareTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择过皮时间"
										   style="width: 100%"/>
						</a-form-item>
					</a-col>
					<a-col :span="8" v-if="formData.depotBizType === '出库'">
						<a-form-item label="价格(元/吨)：" name="unitPrice">
							<a-input-number v-model:value="formData.unitPrice" placeholder="请输入价格" :min="0.01" style="width:100%"
											disabled/>
						</a-form-item>
					</a-col>
					<a-col :span="8" v-else>
						<a-form-item label="价格(元/吨)：" name="unitPrice">
							<a-input-number v-model:value="formData.unitPrice" placeholder="请输入价格" :min="0.01" style="width:100%" @change="putTotalPrice"
											allow-clear/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="净重(公斤)：" name="netWeight">
							<a-input v-model:value="formData.netWeight" placeholder="请输入净重" allow-clear disabled/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="总价(元)：" name="totalPrice">
							<a-input v-model:value="formData.totalPrice" allow-clear disabled/>
						</a-form-item>
					</a-col>
				</a-row>
			</a-card>
			<a-card title="扦样信息" v-if="formData.depotBizType === '入库'" :bordered="false">
				<a-row :gutter="24">
					<a-col :span="8">
						<a-form-item label="水份(%)：" name="samplingShuiFen">
							<a-input-number v-model:value="formData.samplingShuiFen" placeholder="请输入水份" :min="1" style="width:100%" allow-clear/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="容重：" name="samplingRongZhong">
							<a-input v-model:value="formData.samplingRongZhong" placeholder="请输入容重" allow-clear/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="不完善率：" name="samplingQueShao">
							<a-input v-model:value="formData.samplingQueShao" placeholder="请输入不完善率" allow-clear/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="霉变：" name="samplingMeiBian">
							<a-input v-model:value="formData.samplingMeiBian" placeholder="请输入霉变" allow-clear/>
						</a-form-item>
					</a-col>
					<a-col :span="8">
						<a-form-item label="杂质：" name="samplingZaZhi">
							<a-input v-model:value="formData.samplingZaZhi" placeholder="请输入杂质" allow-clear/>
						</a-form-item>
					</a-col>
				</a-row>
			</a-card>
		</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>
	</a-drawer>
</template>

<script setup name="depotTruckWeighDoubleForm">
import tool from '@/utils/tool'
import {cloneDeep} from 'lodash-es'
import {required, rules} from '@/utils/formRules'
import depotTruckWeighPlugApi from '@/api/biz/depotTruckWeighPlugApi'
import dictOptionsApi from "@/api/biz/dictOptionsApi";
import depotTruckBookApi from '@/api/biz/depotTruckBookApi'
import {message} from "ant-design-vue";
import {create, all} from 'mathjs'
import depotGrainApi from "@/api/biz/depotGrainApi";
// 抽屉状态
const visible = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)
let grossPlateNames = ref([])
const depotShedOptions = ref([])
const licensePlateOptions = ref([])
let depotTruckBookData = []
const mathjs = create(all, {
	number: "BigNumber",
	precision: 20,
})
const add = (num1, num2) => +mathjs.format(mathjs.add(num1, num2), {
	precision: 16,
})
// multiply
const multiply = (num1, num2) => +mathjs.format(mathjs.multiply(num1, num2), {
	precision: 16,
})

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
		formData.value.unitPrice = formData.value.samplingPrice
	} else {
		depotTruckBookApi.depotTruckBookPage({size: -1, bizFlowName: '补单选择'}).then((data) => {
			depotTruckBookData = data.records
			licensePlateOptions.value = depotTruckBookData.map((item) => {
				return {
					value: item['truckBookId'],
					label: item['licensePlate']
				}
			})
		})
	}
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	depotTruckBookData = []
	visible.value = false
}
// 默认要校验的
const formRules = {
	depotGrainName: [required('请输入')],
	depotBizType: [required('请输入')],
	grainGroup: [required('请输入')],
	driverName: [required('请输入')],
	driverMobile: [required('请输入')],
	grainType: [required('请输入')],
	licensePlate: [required('请输入')],
	plateColor: [required('请输入')],
	grossWeight: [required('请输入'), rules.number],
	tareWeight: [required('请输入'), rules.number],
	netWeight: [required('请输入'), rules.number],
	grossTime: [required('请输入')],
	tareTime: [required('请输入')],
	unitPrice: [required('请输入'), rules.amountCheck],
	totalPrice: [required('请输入'), rules.amountCheck],
	depotShed: [required('请输入')],
}
// 预约选择
const putLicensePlate = () => {
	depotTruckBookData.map((item) => {
		if (formData.value.licensePlate === item['truckBookId']) {
			formData.value = Object.assign({}, item)
			formData.value.unitPrice = formData.value.samplingPrice
			putTotalPrice()
			dictOptionsApi.options({'dictValue': '库位', depotId: formData.value.depotId}).then((data) => {
				depotShedOptions.value = data
			})
			return
		}
	})
}
// 计算净重
const putNetWeight = () => {
	if (formData.value.grossWeight && (formData.value.tareWeight || formData.value.tareWeight === 0)) {
		formData.value.netWeight = mathjs.subtract(formData.value.grossWeight, formData.value.tareWeight)
		putTotalPrice()
	}
}
// 计算总价
const putTotalPrice = () => {
	if (formData.value.netWeight && formData.value.unitPrice)
		//formData.value.totalPrice = (formData.value.unitPrice * 10000 * formData.value.netWeight / 10000000).toFixed(2)
		formData.value.totalPrice = multiply(multiply(formData.value.unitPrice, formData.value.netWeight), 0.001).toFixed(2)
}
// 验证并提交数据
const onSubmit = () => {
	if (formData.value.grossWeight === 0 || formData.value.netWeight < 1) {
		message.warning('毛重和净重必须大于0公斤')
		return false
	}
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			depotTruckWeighPlugApi
				.depotTruckWeighPlugSubmitForm(formDataParam, true)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
}
// 抛出函数
defineExpose({
	onOpen
})
const plateColorOptions = tool.dictList('车牌颜色')
</script>
