<template>
	<xn-form-container
		:title="formData.id ? '起卸调度' : '起卸调度'"
		:width="900"
		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="loadNumber">
				<a-input v-model:value="formData.loadNumber" placeholder="请输入预约单号" allow-clear disabled/>
			</a-form-item>
			<a-form-item label="起卸点位：" name="loadPointId">
				<a-select v-model:value="formData.loadPointId" placeholder="请选择装卸点位"
						  :options="loadPointIdList" @change="onChangePoint"
				> </a-select>
			</a-form-item>
			<a-form-item label="起卸时间：" name="loadTimeId" >
				<a-select v-model:value="formData.loadTimeId" placeholder="请选择装卸时间"
						  :options="loadTimeIdList" @change="onChangeLoadTime"
				> </a-select>
			</a-form-item>
<!--			<a-form-item label="可约次数：" name="availableNumber" v-show="numberFlag">
				<a-input v-model:value="formData.availableNumber" placeholder="请输入可约次数" allow-clear disabled/>
			</a-form-item>
			<a-form-item label="已约次数：" name="alreadyNumber" v-show="numberFlag">
				<a-input v-model:value="formData.alreadyNumber" placeholder="请输入已约次数" allow-clear disabled/>
			</a-form-item>-->
		</a-form>

		<s-table
			ref="tableRef"
			:columns="columns"
			:data="loadData"
			bordered
			:row-key="(record) => record.id"
		>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a-button type="link" size="small" v-if="hasPerm('bizLoadReplace')" @click="replaceRef.onOpen(record)">更换</a-button>
					</a-space>
					<a-space>
						<a-button type="link" danger size="small" v-if="hasPerm('bizOrderDispatchDelete')" @click="deleteConfig(record)">删除</a-button>
					</a-space>
				</template>
			</template>
		</s-table>

		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>

		<Replace ref="replaceRef" @successful="tableRef.refresh(true)" />
	</xn-form-container>
</template>

<script setup name="bizOrderForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import tool from '@/utils/tool'
	import goodsConfApi from '@/api/biz/goodsConfApi'
	import bizSaleOrderApi from "@/api/biz/bizSaleOrderApi";
	import bizLoadPointApi from "@/api/biz/bizLoadPointApi";
	import bizLoadTimeApi from "@/api/biz/bizLoadTimeApi"
	import bizOrderLoadApi from "@/api/biz/bizOrderLoadApi";
	import {ExclamationCircleOutlined} from '@ant-design/icons-vue';
	import {Modal} from 'ant-design-vue';
	import {createVNode} from 'vue';
	import Replace from './replaceload.vue'
	import bizLoadDispatchApi from "@/api/biz/bizLoadDispatchApi";


	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const tableRef = ref()
	const replaceRef = ref()

	const numberFlag = ref(false)
	// 表单数据
	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 supplierIdList = ref()
	const loadPointIdList = ref()
	const loadTimeIdList = ref()

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

		//装货点位查询
		bizLoadPointApi.getList({loadType:'1'}).then((res)=>{
			loadPointIdList.value = res.map((item)=>{
				return{
					value:item.id,
					label:item.loadPoint
				}
			})
		})

		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 onChangePoint =(value)=>{
		formData.value.loadTimeId = ''
		loadTimeIdList.value = ''
		formData.value.availableNumber = ''
		formData.value.alreadyNumber = ''
		bizLoadTimeApi.searchNow({pointId:formData.value.loadPointId}).then((res)=>{
			loadTimeIdList.value = res.map((item)=>{
				return{
					value:item.id,
					label:item.beginTime+'~'+item.endTime
				}
			})
		})
	}

	const onChangeLoadTime = (value) => {
		bizLoadTimeApi.bizLoadTimeDetail({id:value}).then((res)=>{
			formData.value.availableNumber = res.availableNumber
			formData.value.alreadyNumber = res.alreadyNumber
			//numberFlag.value = true
		})
	}

	const loadData = (parameter) => {
		return bizLoadDispatchApi.getPageList({appointId:formData.value.id,loadType:'1'}).then((data) => {
			return data
		})
	}

	const columns = [
		{
			title: '装卸点位',
			dataIndex: 'loadPoint',
			align:'center'
		},
		{
			title: '开始时间',
			dataIndex: 'beginTime',
			align:'center'
		},
		{
			title: '结束时间',
			dataIndex: 'endTime',
			align:'center'
		},
		/*{
			title: '可约次数',
			dataIndex: 'availableNumber',
			align:'center'
		},
		{
			title: '已约次数',
			dataIndex: 'alreadyNumber',
			align:'center'
		},*/
	]
	// 操作栏通过权限判断是否显示
	columns.push({
		title: '操作',
		dataIndex: 'action',
		align: 'center',
		width: 150
	})




	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
		loadTimeIdList.value = ''
		numberFlag.value = false
		emit('successful')

	}
	// 默认要校验的
	const formRules = {
		//orderNumber: [required('请输入订单编号')],
		//orderName: [required('请输入订单名称')],
		//customerId:[required('请选择客户信息')],
		loadPointId:[required('请选择装卸点位')],
		loadTimeId: [required('请选择装卸时间')],
	}


	// 删除
	const deleteConfig = (record) => {

		Modal.confirm({
			title: '确定删除该数据吗？',
			icon: createVNode(ExclamationCircleOutlined),
			content: '',
			onOk() {
				submitLoading.value = true
				let params = [
					{
						id: record.id
					}
				]

				bizLoadDispatchApi
					.bizLoadDispatchDelete(params)
					.then(() => {
						tableRef.value.refresh(true)
					})
					.finally(() => {
						submitLoading.value = false
					})
			},
			onCancel() {}
		})
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				formDataParam.appointId = formDataParam.id
				formDataParam.id = ''
				bizLoadDispatchApi
					.bizLoadDispatchSubmitLoad(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
					})
					.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>
