<template>
    <a-card :bordered="false">
		<a-page-header
            :title="formData.id ? '编辑二线出岛业务记账管理' : '新增二线出岛业务记账管理'"
			@back="onClose"
		>
			<template #extra>
				<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
				<a-popconfirm title="请确认是否保存？" @confirm="onSubmit" v-if="!isView">
                    <a-button type="primary" :loading="submitLoading">保存</a-button>
                </a-popconfirm>
			</template>
		</a-page-header>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
            <a-row :gutter="16">
                <a-col :span="12">
                    <a-form-item label="企业编码：" name="companyCode">
                        <a-input v-model:value="formData.companyCode" :disabled="isView" placeholder="请输入企业编码" allow-clear />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="企业名称：" name="companyName">
                        <a-input v-model:value="formData.companyName" :disabled="isView" placeholder="请输入企业名称" allow-clear />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="账册类型：" name="accountType">
                        <a-select v-model:value="formData.accountType" :disabled="isView" placeholder="请选择账册类型" :options="accountTypeOptions" show-search allow-clear optionFilterProp="label"/>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="账册编号：" name="accountCode">
                        <a-input v-model:value="formData.accountCode" :disabled="isView" placeholder="请输入账册编号" allow-clear />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="商品编号：" name="commodityCode">
                        <a-input v-model:value="formData.commodityCode" :disabled="isView" placeholder="请输入商品编号" allow-clear />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="商品名称：" name="commodityName">
                        <a-input v-model:value="formData.commodityName" :disabled="isView" placeholder="请输入商品名称" allow-clear />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="业务类型：" name="businessType">
                        <a-select v-model:value="formData.businessType" :disabled="isView" placeholder="请选择业务类型" :options="businessTypeOptions" show-search allow-clear optionFilterProp="label"/>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="记账数量：" name="accountNumber">
                        <a-input v-model:value="formData.accountNumber" :disabled="isView" placeholder="请输入记账数量" allow-clear />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="法定计量单位：" name="measurementUnit">
                        <a-select v-model:value="formData.measurementUnit" :disabled="isView" placeholder="请选择法定计量单位" :options="measurementUnitOptions" show-search allow-clear optionFilterProp="label"/>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="二线出岛申请单号：" name="secondlineOutislandNumber">
                        <a-input v-model:value="formData.secondlineOutislandNumber" :disabled="isView" placeholder="请输入二线出岛申请单号" allow-clear />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="出库单号：" name="warehouseNumber">
                        <a-input v-model:value="formData.warehouseNumber" :disabled="isView" placeholder="请输入出库单号" allow-clear />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="出库日期：" name="warehouseTime">
                        <a-date-picker v-model:value="formData.warehouseTime" :disabled="isView" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择出库日期" style="width: 100%" allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="记账日期：" name="accountTime">
                        <a-date-picker v-model:value="formData.accountTime" :disabled="isView" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择记账日期" style="width: 100%" allow-clear/>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="备注：" name="remark">
                        <a-input v-model:value="formData.remark" :disabled="isView" placeholder="请输入备注" allow-clear />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="文件路径：" name="fileUrl">
                        <a-input v-model:value="formData.fileUrl" :disabled="isView" placeholder="请输入文件路径" allow-clear />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="制单人：" name="creator">
                        <a-input v-model:value="formData.creator" :disabled="isView" placeholder="请输入制单人" allow-clear />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="规格：" name="specs">
                        <a-input v-model:value="formData.specs" :disabled="isView" placeholder="请输入规格" allow-clear />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="单价：" name="unitPrice">
                        <a-input v-model:value="formData.unitPrice" :disabled="isView" placeholder="请输入单价" allow-clear />
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
	</a-card>
</template>

<script setup name="qySecondlineWarehouseDetail">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import qySecondlineWarehouseApi from '@/api/yqyc/qySecondlineWarehouseApi'
	// 抽屉状态
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const isView = ref(false)
	const accountTypeOptions = ref([])
	const businessTypeOptions = ref([])
	const measurementUnitOptions = ref([])

	// 打开抽屉
	const onOpen = (record, view = false) => {
	    isView.value = view
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		accountTypeOptions.value = tool.dictList('account_type')
		businessTypeOptions.value = tool.dictList('bussiness_type')
		measurementUnitOptions.value = tool.dictList('measurement unit')
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		isView.value = true
		emit('onClose')
	}
	// 默认要校验的
	const formRules = {
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				qySecondlineWarehouseApi
					.qySecondlineWarehouseSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
