<template>
	<div class="halfMonthFormDerail">
		<br />
		<div
			style="cursor: pointer"
			@click="
				() => {
					router.back()
				}
			"
		>
			<left-outlined />返回
		</div>
		<br />
		<h3 class="detail_title"></h3>
		<a-table bordered :dataSource="tableData" :columns="tableColumns"> </a-table>
	</div>
</template>
<script setup>
	import business from '@/api/basic.info/RotationProgram'
	import { listPagination } from '@/hook/listlistPagination.js'
	import { reactive, ref, watch } from 'vue'
	import { useRouter, useRoute } from 'vue-router'
	import { defineComponent } from 'vue'
	import useRouterdel from '@/router/index'
	const tableColumns = [
		{ title: '库区名称', align: 'center', dataIndex: 'org_name' },
		{ title: '品种', align: 'center', dataIndex: 'grainVarietyName' },
		{ title: '计划数量', align: 'center', dataIndex: 'planQuantity' },
		{
			title: '轮出',
			align: 'center',
			children: [
				{
					title: '本期',
					align: 'center',
					children: [
						{ title: '数量', align: 'center', dataIndex: 'outCurrentQuantity' },
						{ title: '均价', align: 'center', dataIndex: 'outCurrentAvgPrice' }
					]
				},
				{
					title: '累计',
					align: 'center',
					children: [
						{ title: '数量', align: 'center', dataIndex: 'outSumQuantity' },
						{ title: '均价', align: 'center', dataIndex: 'outSumAvgPrice' }
					]
				},
				{ title: '累计轮出数占比', align: 'center', dataIndex: 'outSumRatio' }
			]
		},
		{
			title: '轮入',
			align: 'center',
			children: [
				{
					title: '本期',
					align: 'center',
					children: [
						{ title: '数量', align: 'center', dataIndex: 'inCurrentQuantity' },
						{ title: '均价', align: 'center', dataIndex: 'inCurrentAvgPrice' }
					]
				},
				{
					title: '累计',
					align: 'center',
					children: [
						{ title: '数量', align: 'center', dataIndex: 'inSumQuantity' },
						{ title: '均价', align: 'center', dataIndex: 'inSumAvgPrice' }
					]
				},
				{ title: '累计轮入数占比', align: 'center', dataIndex: 'inSumRatio' }
			]
		},
		{ title: '备注', width: 200, dataIndex: 'remark' }
	]
	const tableData = ref([])
	const router = useRouter()
	const route = useRoute()

	onMounted(() => {
		// getData()
	})

	const HUIXIAN = async (id) => {
		const res = await business.LHJHBYview({ id: id })
		// console.log(res)
		getData(res)
	}
	watch(
		() => route.query.code,
		(newVal) => {
			if (Boolean(route.query.code) == true) {
				HUIXIAN(route.query.id)
			}
		},
		{ immediate: true, deep: true }
	)

	function getData(data) {
		setTotalRow(data)
		tableData.value = []
		tableData.value = data
	}

	function setTotalRow(data) {
		let totalRow = {}
		let rowKeys = Object.keys(data[0])
		rowKeys.forEach((v) => {
			if (v === 'org_name') {
				totalRow[v] = '合计'
			} else {
				totalRow[v] = data.reduce((acc, cur) => {
					return acc + (cur[v] || 0)
				}, 0)
				if (isNaN(totalRow[v])) {
					totalRow[v] = '-'
				}
			}
		})
		data.unshift(totalRow)
	}
</script>
