<template>
	<a-drawer
		:title="'客户信息详情'"
		:width="900"
		:visible="visible"
		:destroy-on-close="true"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-descriptions :column="1" size="middle" bordered class="mb-2">
				<a-descriptions-item label="企业客户">{{ formData.depotGrainName }}</a-descriptions-item>
				<a-descriptions-item label="贷款合同号">{{ formData.contractCode }}</a-descriptions-item>
				<a-descriptions-item label="位置">{{ formData.addressCity }}&nbsp{{ formData.addressCounty }}</a-descriptions-item>
				<a-descriptions-item label="库容(万吨)">{{ formData.depotSize }}</a-descriptions-item>
				<a-descriptions-item label="日烘干能力(吨)">{{ formData.dayDrySize }}</a-descriptions-item>
				<a-descriptions-item label="当前库存数量(万吨)">{{ formData.depotCount }}</a-descriptions-item>
				<a-descriptions-item label="贷款额度(元)">{{ formData.loanLimit }}</a-descriptions-item>
				<a-descriptions-item label="贷款金额(元)">{{ formData.loanSum }}</a-descriptions-item>
				<a-descriptions-item label="贷款余额(元)">{{ formData.loanBalance }}</a-descriptions-item>
				<a-descriptions-item label="贷款利息(元)">{{ formData.loanInterest }}</a-descriptions-item>
				<a-descriptions-item label="贷款到期日">{{ formData.loanEndDate }}</a-descriptions-item>
				<a-descriptions-item label="已质押库存数(万吨)">{{ formData.mortgageCount }}</a-descriptions-item>
				<a-descriptions-item label="新增入库(吨)">{{ formData.tonInCount }}</a-descriptions-item>
				<a-descriptions-item label="新增入库(辆)">{{ formData.truckInCount }}</a-descriptions-item>
				<a-descriptions-item label="贷款未审批(吨)">{{ formData.tonInCheckCount }}</a-descriptions-item>
				<a-descriptions-item label="还款未出库(吨)">{{ formData.tonOutFinishCount }}</a-descriptions-item>
			</a-descriptions>
			<br> 粮食种类：
			<a-table
				ref="table"
				size="small"
				:columns="columns"
				:data-source="formData.priceChildren"
				:expand-row-by-click="true"
				bordered
			></a-table>
			<br> 文件：
			<a-table
				ref="table"
				size="small"
				:columns="fileColumns"
				:data-source="fileData"
				:expand-row-by-click="true"
				bordered
			></a-table>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
		</template>
	</a-drawer>
</template>

<script setup name="depotGrainInfoForm">
import tool from '@/utils/tool'
import {cloneDeep} from 'lodash-es'
import {required} from '@/utils/formRules'
import depotGrainInfoApi from '@/api/biz/depotGrainInfoApi'
import dictOptionsApi from "@/api/biz/dictOptionsApi";
// 抽屉状态
const visible = ref(false)
const emit = defineEmits({successful: null})
const formRef = ref()
// 定义地区元素
const treeData = ref([])
// 表单数据
const formData = ref({})
const submitLoading = ref(false)
const depotIdOptions = ref([])
const addressCityOptions = tool.dictList('黑龙江')
const addressCountyOptions = ref([])
const columns = [
	{
		title: '粮食种类',
		dataIndex: 'grainGroup'
	},
	{
		title: '干粮价格(元/吨)',
		dataIndex: 'dryPrice'
	},
	{
		title: '质押价格(元/吨)',
		dataIndex: 'loanPrice'
	},
	{
		title: '平均质押价格(元/吨)',
		dataIndex: 'loanPriceAvg'
	},
	{
		title: '质押率(%)',
		dataIndex: 'loanRatio'
	},
	{
		title: '库存数量(万吨)',
		dataIndex: 'loanTonW'
	},
	{
		title: '潮干比',
		dataIndex: 'dampRatio'
	},
	{
		title: '标准干粮水份(%)',
		dataIndex: 'canonRatio'
	},
]
const fileData = ref([])
const fileColumns = [
	{
		title: '文件名称',
		dataIndex: 'name',
		ellipsis: true,
	},
	{
		title: '文件大小',
		dataIndex: 'sizeInfo',
		ellipsis: true,
		width: '180px'
	},
]

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
		if (!formData.value.depotCount)
			formData.value.depotCount = 0
		if (!formData.value.mortgageCount)
			formData.value.mortgageCount = 0
		if (!formData.value.loanLimit)
			formData.value.loanLimit = 0
		if (formData.value.addressCity)
			addressCountyOptions.value = tool.dictList(formData.value.addressCity)
		depotGrainInfoApi.depotGrainInfoFileLst({"id": formData.value.id}).then((data) => {
			fileData.value = data
		})
	} else {
		dictOptionsApi.options(Object.assign('', {'dictValue': '粮库'})).then((data) => {
			depotIdOptions.value = data
		})
	}
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	addressCountyOptions.value = []
	visible.value = false
}
// 城市选择
const addressCityAuto = () => {
	formData.value.addressCounty = ''
	if (formData.value.addressCity)
		addressCountyOptions.value = tool.dictList(formData.value.addressCity)
}
// 增行
const addDomains = () => {
	if (!formData.value.priceChildren) {
		formData.value.priceChildren = []
	}
	formData.value.priceChildren.push({
		grainGroup: undefined,
		avgPrice: undefined,
		repayPrice: undefined,
		pledgeRatio: undefined,
		dampRatio: undefined
	})
}
// 减行
const delDomains = (index) => {
	formData.value.priceChildren.splice(index, 1)
}
// 默认要校验的
const formRules = {
	contractCode: [required('请输入')],
	addressCity: [required('请输入')],
	addressCounty: [required('请输入')],
	addressTown: [required('请输入')],
	depotSize: [required('请输入')],
	dayDrySize: [required('请输入')],
	loanEndDate: [required('请输入')],
}
// 验证并提交数据
const onSubmit = () => {
	formRef.value
		.validate()
		.then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			depotGrainInfoApi
				.depotGrainInfoSubmitForm(formDataParam, !formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
}
// 抛出函数
defineExpose({
	onOpen
})
const grainGroupOptions = tool.dictList('粮食种类')
</script>
