<template>
	<div class="collect-image-stage">
		<div ref="exportRoot" class="collect-image-sheet">
			<img class="sheet-stamp" :src="stampImage" alt="审核通过章">
			<div class="sheet-title">领用单</div>
			<div class="sheet-store-name">景聚庭</div>
			<div class="sheet-info-grid">
				<div class="info-item">
					<span class="info-label">领用编号</span>
					<span class="info-value">{{ safeText(templateData.collectNo) }}</span>
				</div>
				<div class="info-item">
					<span class="info-label">领用日期</span>
					<span class="info-value">{{ safeText(templateData.collectDate) }}</span>
				</div>
				<div class="info-item">
					<span class="info-label">领用人</span>
					<span class="info-value">{{ safeText(templateData.handledByName) }}</span>
				</div>
				<div class="info-item">
					<span class="info-label">领用人部门</span>
					<span class="info-value">{{ safeText(templateData.handledByOfficeName) }}</span>
				</div>
			</div>
			<div class="section-title">领用列表</div>
			<table class="sheet-table">
				<thead>
					<tr>
						<th class="type-col">领用类型</th>
						<th class="goods-col">物品名称</th>
						<th class="count-col">领用数量</th>
						<th class="company-col">单位</th>
						<th class="remark-col">备注</th>
					</tr>
				</thead>
				<tbody>
					<tr v-for="(item, index) in tableRows" :key="index">
						<td>{{ safeText(item.collectType) }}</td>
						<td>{{ safeText(item.goodsName) }}</td>
						<td>{{ safeText(item.collectNumber) }}</td>
						<td>{{ safeText(item.company) }}</td>
						<td>{{ safeText(item.detailRemarks) }}</td>
					</tr>
				</tbody>
			</table>
			<div class="sheet-footer">
				<div class="footer-row">
					<span class="footer-label">备注</span>
					<span class="footer-value preserve-wrap">{{ safeText(templateData.remarks) }}</span>
				</div>
				<div class="footer-row">
					<span class="footer-label">审批流程</span>
					<span class="footer-value footer-user">{{ safeText(templateData.auditUser) }}</span>
					<span class="footer-value footer-status-message">{{ safeText(templateData.statusMessage) }}</span>
					<span class="footer-value footer-time">{{ safeText(templateData.auditTime) }}</span>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import stampImage from '@/assets/img/flowable/2.png'

export default {
	name: 'CollectImageTemplate',
	props: {
		templateData: {
			type: Object,
			default: () => ({
				collectList: []
			})
		}
	},
	data() {
		return {
			stampImage
		}
	},
	computed: {
		tableRows() {
			if (Array.isArray(this.templateData.collectList) && this.templateData.collectList.length > 0) {
				return this.templateData.collectList
			}
			return [{}]
		},
		auditSummary() {
			const parts = [
				this.normalizeText(this.templateData.auditUser),
				this.normalizeText(this.templateData.statusMessage),
			].filter(Boolean)
			return parts.length ? parts.join('   ') : ''
		}
	},
	methods: {
		normalizeText(value) {
			if (value === null || value === undefined) {
				return ''
			}
			const text = String(value).trim()
			return text && text !== 'null' ? text : ''
		},
		safeText(value) {
			return this.normalizeText(value) || ''
		},
		getExportElement() {
			return this.$refs.exportRoot
		}
	}
}
</script>

<style scoped>
.collect-image-stage {
	position: fixed;
	left: -200vw;
	top: 0;
	pointer-events: none;
}

.collect-image-sheet {
	position: relative;
	width: 1080px;
	padding: 40px 44px;
	box-sizing: border-box;
	background: #ffffff;
	color: #303133;
	font-family: "Microsoft YaHei", sans-serif;
}

.sheet-stamp {
	position: absolute;
	top: 60px;
	right: 28px;
	width: 148px;
	height: 148px;
	object-fit: contain;
	opacity: 0.9;
}

.sheet-title {
	font-size: 30px;
	font-weight: 700;
	letter-spacing: 2px;
	text-align: center;
	margin-bottom: 40px;
}

.sheet-store-name {
	position: absolute;
	top: 96px;
	left: 48px;
	font-size: 17px;
	letter-spacing: 1px;
	text-align: left;
	margin: 0;
	padding: 0;
	z-index: 1;
}

.sheet-info-grid {
	display: grid;
	grid-template-columns: repeat(2, minmax(0, 1fr));
	border: 1px solid #303133;
	border-bottom: none;
}

.info-item {
	display: flex;
	min-height: 54px;
	border-bottom: 1px solid #303133;
}

.info-item:nth-child(odd) {
	border-right: 1px solid #303133;
}

.info-label,
.footer-label {
	flex: 0 0 128px;
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 12px;
	box-sizing: border-box;
	background: #f5f7fa;
	font-weight: 600;
	border-right: 1px solid #303133;
}

.info-value,
.footer-value {
	flex: 1;
	display: flex;
	align-items: center;
	padding: 12px 16px;
	box-sizing: border-box;
	line-height: 1.7;
}

.footer-user {
	flex: 0 0 auto;
	justify-content: flex-start;
	text-align: left;
	padding-left: 10px;
	padding-right: 10px;
	white-space: nowrap;
}

.footer-status-message {
	flex: 1 1 auto;
	min-width: 0;
}

.footer-time {
	flex: 0 0 auto;
	justify-content: flex-end;
	text-align: right;
	padding-left: 10px;
	padding-right: 10px;
	white-space: nowrap;
}

.section-title {
	display: flex;
	align-items: center;
	justify-content: center;
	height: 54px;
	border: 1px solid #303133;
	border-top: none;
	font-size: 20px;
	font-weight: 700;
}

.sheet-table {
	width: 100%;
	border-collapse: separate;
	border-spacing: 0;
	table-layout: fixed;
}

.sheet-table th,
.sheet-table td {
	padding: 14px 10px;
	text-align: center;
	vertical-align: middle;
	line-height: 1.6;
	word-break: break-word;
	border-right: 1px solid #303133;
	border-bottom: 1px solid #303133;
}

.sheet-table th:first-child,
.sheet-table td:first-child {
	border-left: 1px solid #303133;
}

.sheet-table th:last-child,
.sheet-table td:last-child {
	border-right: 1px solid #303133;
}

.sheet-table th {
	background: #f5f7fa;
	font-size: 16px;
	font-weight: 700;
	border-top: none;
}

.sheet-table td {
	font-size: 15px;
}

.type-col {
	width: 20%;
}

.goods-col {
	width: 25%;
}

.count-col {
	width: 16%;
}

.company-col {
	width: 12%;
}

.remark-col {
	width: 27%;
}

.sheet-footer {
	border-left: 1px solid #303133;
	border-right: 1px solid #303133;
	border-bottom: 1px solid #303133;
}

.footer-row {
	display: flex;
	min-height: 56px;
	border-bottom: 1px solid #303133;
}

.footer-row:last-child {
	border-bottom: none;
}

.preserve-wrap {
	white-space: pre-wrap;
}
</style>
