<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form mb-4">
			<a-row :gutter="24">
				<a-col :span="4">
					<a-form-item label="企业客户" name="depotId">
						<a-select v-model:value="searchFormState.depotId" placeholder="请选择企业客户" :options="depotOptions"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="贷款合同号" name="contractCode">
						<a-input v-model:value="searchFormState.contractCode" placeholder="请输入贷款合同号"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="位置（市）" name="addressCity">
						<a-input v-model:value="searchFormState.addressCity" placeholder="请输入位置（市）"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="位置（县）" name="addressCounty">
						<a-input v-model:value="searchFormState.addressCounty" placeholder="请输入位置（县）"/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="() => searchFormRef.resetFields()">重置</a-button>
					<!--					<a @click="toggleAdvanced" style="margin-left: 8px">
										{{ advanced ? '收起' : '展开' }}
										<component :is="advanced ? 'up-outlined' : 'down-outlined'"/>
									</a>-->
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('depotGrainInfoAdd')">
						<template #icon>
							<plus-outlined/>
						</template>
						新增
					</a-button>
					<a-button type="primary" @click="exportTable()"><export-outlined/>导出</a-button>
					<!-- <a-button danger @click="deleteBatchDepotGrainInfo()" v-if="hasPerm('depotGrainInfoBatchDelete')">删除</a-button>-->
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'addressCity'">
					<p> {{ record.addressCity }}</p>
					<p> {{ record.addressCounty }}</p>
					<!--					<p> {{ record.addressTown }}</p>-->
				</template>
				<template v-if="column.dataIndex === 'depotSize'">
					<p>库容(万吨)：{{ record.depotSize }}</p>
					<p>日烘干(吨)：{{ record.dayDrySize }}</p>
					<p>库存(万吨)：{{ record.depotCount }}</p>
					<p>已质押(万吨)：{{ record.mortgageCount }}</p>
				</template>
				<template v-if="column.dataIndex === 'loanSum'">
					<p>贷款金额(元)：{{ record.loanSum }}</p>
					<p>贷款余额(元)：{{ record.loanBalance }}</p>
					<p>贷款额度(元)：{{ record.loanLimit }}</p>
					<p>贷款到期日：{{ record.loanEndDate }}</p>
				</template>
				<template v-if="column.dataIndex === 'tonInCount'">
					<p>新增入库(吨)：{{ record.tonInCount }}</p>
					<p>新增入库(辆)：{{ record.truckInCount }}</p>
					<p>贷款未审批(吨)：{{ record.tonInCheckCount }}</p>
					<p>还款未出库(吨)：{{ record.tonOutFinishCount }}</p>
				</template>
				<template v-if="column.dataIndex === 'priceChildren'">
					<span v-for="textValue in record.priceChildren" :key="textValue.grainGroup" color="green">
						<div style="float: none">
							<div style="width:50px;border:1px solid #d3e3fd;float:left;text-align:center">{{ textValue.grainGroup }}</div>
							<div style="width:80px;border:1px solid #d3e3fd;float:left;text-align:center">{{ textValue.loanPrice }}</div>
							<div style="width:50px;border:1px solid #d3e3fd;float:left;text-align:center"> {{ textValue.loanRatio }}%</div>
							<div style="width:115px;border:1px solid #d3e3fd;float:left;text-align:center"> {{ textValue.loanTonW }}</div>
							<div style="width:50px;border:1px solid #d3e3fd;float:left;text-align:center"> {{ textValue.dampRatio }}</div>
							<div style="width:55px;border:1px solid #d3e3fd;float:left;text-align:center"> {{ textValue.canonRatio }}%</div>
							<div style="clear: both"></div><div></div>
						</div>
					</span>
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a @click="detailFormRef.onOpen(record)">详情</a>
					<a @click="fileFormRef.onOpen(Object.assign(record,{'addFlag':1,'removeFlag':1}))" v-if="hasPerm('depotGrainInfoFileEdit')">
						<a-divider type="vertical"/>
						文件管理</a>
					<a @click="fileFormRef.onOpen(Object.assign(record,{'addFlag':2,'removeFlag':2}))" v-if="hasPerm('depotGrainInfoFile')">
						<a-divider type="vertical"/>
						文件</a>
					<a @click="formRef.onOpen(record)" v-if="hasPerm('depotGrainInfoEdit')">
						<a-divider type="vertical"/>
						编辑</a>
					<a-popconfirm title="确定要删除吗？" @confirm="deleteDepotGrainInfo(record)">
						<a-button type="link" danger size="small" v-if="hasPerm('depotGrainInfoDelete')">
							<a-divider type="vertical"/>
							删除
						</a-button>
					</a-popconfirm>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="table.refresh(true)"/>
	<DetailForm ref="detailFormRef"/>
	<FileForm ref="fileFormRef"/>
</template>

<script setup name="depotGrainInfo">
import {message} from 'ant-design-vue'
import tool from '@/utils/tool'
import Form from './form.vue'
import DetailForm from './detailForm.vue'
import FileForm from '../depotFile/form.vue'
import depotGrainInfoApi from '@/api/biz/depotGrainInfoApi'
import dictOptionsApi from "@/api/biz/dictOptionsApi";
import * as XLSX from "xlsx";
import moment from "moment";

let searchFormState = reactive({})
const searchFormRef = ref()
const table = ref()
const formRef = ref()
const detailFormRef = ref()
const fileFormRef = ref()
const toolConfig = {refresh: true, height: true, columnSetting: true, striped: false}
const depotOptions = ref([])
// 查询区域显示更多控制
const advanced = ref(false)
const toggleAdvanced = () => {
	advanced.value = !advanced.value
}
const columns = [
	{
		title: '企业客户',
		dataIndex: 'depotGrainName'
	},
	{
		title: '贷款合同号',
		dataIndex: 'contractCode'
	},
	{
		title: '位置',
		dataIndex: 'addressCity'
	},
	{
		title: '仓储',
		dataIndex: 'depotSize'
	},
	{
		title: '贷款',
		dataIndex: 'loanSum'
	},
	{
		title: '粮食种类 | 质押价格 | 质押率 | 库存数量(万吨) | 潮干比 | 干粮水份',
		dataIndex: 'priceChildren',
	},
	{
		title: '新增入出库',
		dataIndex: 'tonInCount'
	},
]
// 操作栏通过权限判断是否显示
columns.push({
	title: '操作',
	dataIndex: 'action',
	align: 'center',
	width: '150px'
})
let selectedRowKeys = ref([])
// 列表选择配置
const options = {
	alert: {
		show: false,
		clear: () => {
			selectedRowKeys = ref([])
		}
	},
	rowSelection: {
		onChange: (selectedRowKey, selectedRows) => {
			selectedRowKeys.value = selectedRowKey
		}
	}
}
dictOptionsApi.options(Object.assign('', {'dictValue': '粮库'})).then((data) => {
	depotOptions.value = data
})
let loadDataLst
const loadData = (parameter) => {
	const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
	return depotGrainInfoApi.depotGrainInfoPage(Object.assign(parameter, searchFormParam)).then((data) => {
		loadDataLst = data.records
		return data
	})
}
// 删除
const deleteDepotGrainInfo = (record) => {
	let params = [
		{
			id: record.id
		}
	]
	depotGrainInfoApi.depotGrainInfoDelete(params).then(() => {
		table.value.refresh(true)
	})
}
// 批量删除
const deleteBatchDepotGrainInfo = () => {
	if (selectedRowKeys.value.length < 1) {
		message.warning('请选择一条或多条数据')
		return false
	}
	const params = selectedRowKeys.value.map((m) => {
		return {
			id: m
		}
	})
	depotGrainInfoApi.depotGrainInfoDelete(params).then(() => {
		table.value.clearRefreshSelected()
	})
}
// 导出
const exportTable = () => {
	let tableData = [['企业客户', '贷款合同号',  '位置(市)', '位置(区/县市)', '库容(万吨)', '日烘干能力(吨)'
	,'当前库存数量(万吨)', '贷款额度',  '贷款金额', '贷款余额', '贷款利息', '贷款到期日'
	,'已质押库存数(万吨)', '入库待申请数量(吨)',  '入库待申请车数(辆)', '入库待审批数量(吨)', '出库已审批数量(吨)', '累计贷款金额'
	,'累计还款金额', '累计质押库存数(万吨)',  '入库交易额', '出库交易额', '创建时间']]
	let idStr = ''
	if (selectedRowKeys.value.length > 0) {
		selectedRowKeys.value.map((m) => {
			idStr = idStr + ',' + m
		})
	}
	loadDataLst.map((m) => {
		if (!idStr || idStr.indexOf(m.id) > 0)
			tableData.push([m.depotGrainName, m.contractCode, m.addressCity, m.addressCounty, m.depotSize, m.dayDrySize
			,m.depotCount, m.loanLimit, m.loanSum, m.loanBalance, m.loanInterest, m.loanEndDate
				,m.mortgageCount, m.tonInCount, m.truckInCount, m.tonInCheckCount, m.tonOutFinishCount, m.loanSumTotal
				,m.repaySumTotal, m.mortgageCountTotal, m.inAmount, m.outAmount, m.createTime])
	})
	let workSheet = XLSX.utils.aoa_to_sheet(tableData)
	let bookNew = XLSX.utils.book_new()
	XLSX.utils.book_append_sheet(bookNew, workSheet, 'Sheet1')
	let name = '客户档案信息' + moment().format('_YYYY-MM-DD') + '.xlsx'
	XLSX.writeFile(bookNew, name)
}
</script>
