<template>
	<!--购销计划新增 -->
	<a-card :bordered="false">
		<div
			style="cursor: pointer; color: rgb(15, 123, 224)"
			@click="
				() => {
					router.back()
				}
			"
		>
			<left-outlined />返回
		</div>
		<br />
		<div class="pricemoitorrecord">
			<a-form :model="formState" name="basic" autocomplete="off">
				<div class="form">
					<a-form-item class="form-item" label="企业名称" name="dwmc" :label-col="{ span: 7 }">
						<a-input :disabled="code" v-model:value="formState.dwmc" />
					</a-form-item>
					<a-form-item class="form-item" label="计划名称" name="planName" :label-col="{ span: 7 }">
						<a-input :disabled="code" v-model:value="formState.planName" />
					</a-form-item>
					<a-form-item class="form-item" label="轮换年度" name="rotationYear" :label-col="{ span: 7 }">
						<a-input :disabled="code" v-model:value="formState.rotationYear" />
					</a-form-item>
					<a-form-item class="form-item" label="计划类型" name="username" :label-col="{ span: 7 }">
						<a-select v-model:value="formState.planType" :disabled="code" style="width: 100%" placeholder="请选择">
							<a-select-option :value="1">粮食轮换计划 </a-select-option>
							<a-select-option :value="2">油料轮换计划 </a-select-option>
						</a-select>
					</a-form-item>
					<a-form-item class="form-item" label="轮换类型" name="username" :label-col="{ span: 7 }">
						<a-select v-model:value="formState.type" :disabled="code" style="width: 100%" placeholder="请选择">
							<a-select-option :value="1">静态轮换 </a-select-option>
							<a-select-option :value="2">动态轮换 </a-select-option>
						</a-select>
					</a-form-item>
				</div>
				<br />
				<br />
				<br />

				<div class="list" v-for="(item, index) in tableDataDel" :key="index">
					<div>{{ item.orgName }}&nbsp;&nbsp;</div>
					<div>&nbsp;</div>
					<div style="margin-left: 100px">轮出库</div>
					<div>&nbsp;</div>
					<a-table :dataSource="item.businessRpOutboundList" :pagination="pagination" :columns="tableColumns1">
						<!-- <template #bodyCell="{ column, index }">
							<template v-if="column.dataIndex === 'Index'">
								{{ index == 0 ? '合计' : index + 1 }}
							</template>
						</template> -->
					</a-table>
					<br />
					<br />
					<div style="margin-left: 100px">轮入</div>
					<div>&nbsp;</div>
					<a-table :dataSource="item.businessRpInboundList" :pagination="pagination" :columns="tableColumns2">
						<!-- <template #bodyCell="{ column, index }">
							<template v-if="column.dataIndex === 'Index'">
								{{ index == 0 ? '合计' : index + 1 }}
							</template>
						</template> -->
					</a-table>
				</div>
			</a-form>
		</div>
	</a-card>
</template>
<script setup>
	import { listPaginationBAQDE } from '@/hook/listlistPagination.js'
	import { onMounted } from 'vue'
	import { reactive, ref } from 'vue'
	import { defineComponent } from 'vue'
	import business from '@/api/basic.info/rotationprogramApi.js'
	import { useRouter, useRoute } from 'vue-router'
	const router = useRouter()
	const route = useRoute()

	const code = ref(true)

	watch(
		() => route.query.code,
		(newVal) => {
			if (Boolean(route.query.code) == true) {
				code.value = true
			}
		},
		{ immediate: true, deep: true }
	)
	const tableColumns1 = [
		{
			title: '序号',
			dataIndex: 'Index',
			width: '10%',
			align: 'center',
			customRender: ({ text, record, index }) => {
				return `${index + 1}`
			}
		},
		{ title: '粮库名称', width: '10%', align: 'center', dataIndex: 'orgName' },
		{ title: '仓房名称', width: '10%', align: 'center', dataIndex: 'basicStorehouseName' },
		{ title: '货位名称', width: '10%', align: 'center', dataIndex: 'warehouseName' },
		{ title: '品种', align: 'center', dataIndex: 'grainVarietyName' },
		{ title: '性质', align: 'center', dataIndex: 'grainNatureName' },
		{ title: '等级', align: 'center', dataIndex: 'grainGradeName' },
		{ title: '数量', align: 'center', dataIndex: 'sealingQuantity' },
		// { title: '入库年限', dataIndex: 'dataIndex3' },
		{ title: '入库核定价', align: 'center', dataIndex: 'purchasePrice' },
		{ title: '备注', align: 'center', dataIndex: 'remarks' },
		{ title: '查看质检报告', width: '20%', align: 'center', dataIndex: 'inspectionReportId' },
		{ title: '查看定检报告', align: 'center', dataIndex: 'regularInspectionReportIds' }
	]
	const tableColumns2 = [
		{
			title: '序号',
			dataIndex: 'Index',
			width: '10%',
			align: 'center',
			customRender: ({ text, record, index }) => {
				return `${index + 1}`
			}
		},
		{
			title: '粮库名称',
			dataIndex: 'orgName'
		},
		{ title: '品种', dataIndex: 'grainVarietyName' },
		{ title: '性质', dataIndex: 'grainNatureName' },
		{ title: '生产年限', dataIndex: 'productPeriod' },
		{ title: '数量', dataIndex: 'sealingQuantity' }
	]

	const tableData1 = ref([])
	onMounted(() => {
		onSearch()
	})
	const formState = reactive({
		dwmc: '',
		planName: '',
		rotationYear: '',
		planType: '',
		type: ''
	})
	const tableDataDel = ref([])

	const onSearch = async (T) => {
		const res = await business.LHJHview({ rpReportId: route.query.rpReportId, ...T })
		formState.dwmc = res.dwmc
		formState.planName = res.planName
		formState.rotationYear = res.rotationYear.slice(0, 4)
		formState.planType = res.planType
		formState.type = res.type
		tableDataDel.value = res.complexDataList
		console.log(res)
	}
	const { onReset, handlePageChange, pagination, Search, SearchFormDel } = listPaginationBAQDE(onSearch, {})

	function getData(data) {
		setTotalRow(data)
	}
	// function setTotalRow(data) {
	// 	let totalRow = {}
	// 	let rowKeys = Object.keys(data[0])
	// 	rowKeys.forEach((v) => {
	// 		if (v === 'Index') {
	// 			totalRow[v] = '合计'
	// 		} else {
	// 			totalRow[v] = data.reduce((acc, cur) => {
	// 				// console.log(typeof cur.sealingQuantity == 'number')
	// 				return acc + (cur[v] || 0)
	// 			}, 0)
	// 			if (isNaN(totalRow[v])) {
	// 				totalRow[v] = '-'
	// 			}
	// 		}
	// 	})
	// 	// data.unshift(totalRow)
	// 	data.push(totalRow)
	// }

	function setTotalRow(data) {
		let totalRow = {}
		let rowKeys = Object.keys(data[0])

		rowKeys.forEach((v) => {
			if (v === 'Index') {
				totalRow[v] = '合计'
			} else {
				let total = 0
				let hasNonNumericValue = false

				data.forEach((row) => {
					let value = row[v]
					if (typeof value === 'number') {
						total += value
					} else {
						hasNonNumericValue = true
					}
				})

				if (hasNonNumericValue) {
					totalRow[v] = '-'
				} else {
					totalRow[v] = total
				}
			}
		})

		data.push(totalRow)
	}
</script>
<style lang="less" scoped>
	.search {
		padding-top: 20px;
		padding-left: 50px;
		margin-bottom: 32px;
		.btnBox {
			display: inline-block;
			width: 200px;
			display: flex;
			justify-content: space-around;
			align-items: center;
		}
	}
	.silt-title {
		display: flex;
	}
	.form {
		display: flex;
		flex-wrap: wrap;
		.form-item {
			width: 350px;
			margin-right: 20px;
		}
	}
	.item-submit {
		display: flex;
		justify-content: center;
		.btn1 {
			width: 100px;
			margin-right: 20px;
		}
	}
	.ant-select {
		width: 250px;
	}
	.ant-input {
		width: 250px;
	}
	.ant-form-item-label {
		width: 120px;
	}
</style>
