<template>
	<a-drawer
		:title="'增加入库放款单'"
		:width="600"
		:visible="visible"
		:destroy-on-close="true"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="企业客户：" name="depotGrainName">
				<a-input v-model:value="formData.depotGrainName" disabled/>
			</a-form-item>
			<a-form-item label="贷款合同编号：" name="contractCode">
				<a-input v-model:value="formData.contractCode" placeholder="请输入贷款合同编号" allow-clear disabled/>
			</a-form-item>
			<a-row :gutter="24">
				<a-col :span="24">
					<a-form-item label="入库总金额(元)：" name="orderAmount">
						<a-input v-model:value="formData.orderAmount" placeholder="请输入订单总金额" allow-clear disabled/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="入库实际价格(元/吨)：" name="orderPrice">
						<a-input v-model:value="formData.orderPrice" placeholder="请输入订单价格" allow-clear disabled/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="入库价格(元/吨)：" name="orderDryPrice">
						<a-input v-model:value="formData.orderDryPrice" placeholder="入库价格" allow-clear disabled/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="入库实际重量(吨)：" name="orderWeight">
						<a-input v-model:value="formData.orderWeight" placeholder="请输入入库实际重量" allow-clear disabled/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="入库重量(吨)：" name="orderDryWeight">
						<a-input v-model:value="formData.orderDryWeight" placeholder="请输入入库重量" allow-clear disabled/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="质押率(%）：" name="loanRatio">
						<a-input-number v-model:value="formData.loanRatio" :min="0.01" style="width:100%" allow-clear disabled/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="平均质押价格(元/吨, 仅参考)：" name="loanPriceAvg">
						<a-input-number v-model:value="formData.loanPriceAvg" :min="0.01" style="width:100%" disabled/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="质押价格(元/吨)：" name="loanPrice">
						<a-input-number v-model:value="formData.loanPrice" @change="loanAmountAuto" :min="0.01" style="width:100%" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="放款金额(元)：" name="loanAmount">
						<a-input-number v-model:value="formData.loanAmount" placeholder="请输入放款金额" :min="0.01" style="width:100%" allow-clear disabled/>
					</a-form-item>
				</a-col>
			</a-row>
			<p>
				<a-button type="primary" @click="fileFormRef.onOpen(Object.assign(formData,{'bizName':'质押凭证', id:formData.IdWorker}))">
					<template #icon>
						<plus-outlined/>
					</template>
					上传文件
				</a-button>
			</p>
			<p>※ 放款金额 = 质押价格 * 入库实际重量 * 质押率</p>
			<p>※ 放款流程 ：申请放款单 → 银行审批</p>
		</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>
	<FileForm ref="fileFormRef" @successful="table.refresh(true)"/>
</template>

<script setup name="depotLoanGrantForm">
import tool from '@/utils/tool'
import {cloneDeep} from 'lodash-es'
import {required, rules} from '@/utils/formRules'
import depotLoanGrantApi from '@/api/biz/depotLoanGrantApi'
import depotGrainInfoApi from '@/api/biz/depotGrainInfoApi'
import FileForm from '../depotFile/formLeft.vue'
// 抽屉状态
const visible = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
const fileFormRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)

// 打开抽屉
const onOpen = (record) => {
	if (record) {
		visible.value = true
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
		depotGrainInfoApi.depotGrainInfoDetail({"id": recordData.depotId}).then((data) => {
			formData.value.depotGrainName = data.depotGrainName
			formData.value.contractCode = data.contractCode
			data.priceChildren.map((m) => {
				if (m.grainGroup === formData.value.grainGroup) {
					formData.value = Object.assign(formData.value, m)
				}
			})
			loanAmountAuto()
		})
		//申请id
		depotLoanGrantApi.depotLoanGrantDetail({"id": "IdWorker"}).then((data) => {
			formData.value.IdWorker = data.id
		})
		console.log(formData.value)
	}
}
// 计算放款金额
const loanAmountAuto = () => {
	formData.value.loanAmount = (formData.value.orderWeight * 10000 * formData.value.loanPrice * formData.value.loanRatio / 1000000).toFixed(2)
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	visible.value = false
}
// 默认要校验的
const formRules = {
	depotGrainName: [required('请输入')],
	contractCode: [required('请输入')],
	loanAmount: [required('请输入'), rules.amountCheck],
	orderPrice: [required('请输入'), rules.amountCheck],
	orderWeight: [required('请输入')],
	orderAmount: [required('请输入'), rules.amountCheck],
	loanPrice: [required('请输入'), rules.amountCheck],
	loanRatio: [required('请输入')],
}
// 验证并提交数据
const onSubmit = () => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			formDataParam.id = formDataParam.IdWorker
			depotLoanGrantApi
				.depotLoanGrantSubmitForm(formDataParam, true)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
