<template>
	<view class="detail-page-container">
		<erp-nav-bar title="订单详情" />

		<!-- 2. 状态横幅区域：不再包含状态栏边距 -->
		<view class="status-banner-container" :class="order.statusType" id="nav-header">
			<view class="status-banner-content">
				<view class="header-main">
					<text class="status-title">{{ order.statusName }}</text>
					<text class="status-sub">{{ statusSubText }}</text>
				</view>
				<view class="header-icon-wrap">
					<view class="status-visual-icon"></view>
				</view>
			</view>
		</view>

		<!-- 2. 主内容滚动区：计算锁定高度以支持 100% 滚动 -->
		<scroll-view scroll-y class="main-content-scroll" :style="{ height: scrollHeight }" :show-scrollbar="false">
			<view class="detail-inner-box">
				<!-- 卡片：规格清单 (支持多个型号展现) -->
				<view class="model-item-card-inner" v-for="(model, mIdx) in (order.models || [order])" :key="mIdx">
					<view class="data-group-card" :class="{ 'first-card': mIdx === 0 }">
						<view class="card-head">规格清单 #{{ mIdx + 1 }}</view>
						<view class="data-item"><text class="l">产品型号</text><text class="v bold">{{ model.type }}</text>
						</view>
						<view class="data-item"><text class="l">型号名称</text><text class="v">{{ model.typeName ||
							'铝型材主料' }}</text></view>
						<view class="data-item"><text class="l">型材材质</text><text class="v">{{ model.material ||
							'6063-T5' }}</text></view>
						<view class="line-split"></view>
						<view class="data-item"><text class="l">表面处理</text><text class="v">{{ model.surface }}</text>
						</view>
						<view class="data-item"><text class="l">包装方式</text><text class="v">{{ model.package ||
							'普通包装' }}</text></view>
						<view class="line-split"></view>
						<view class="data-item"><text class="l">订单长度</text><text class="v">{{ model.length }} mm</text>
						</view>
						<view class="data-item"><text class="l">型材壁厚</text><text class="v">{{ model.wallThickness ||
							'1.2' }} mm</text></view>
						<view class="data-item"><text class="l">需求支数</text><text class="v highlight">{{ model.count }}
								支</text></view>
					</view>
				</view>

				<!-- 卡片：订单详情 -->
				<view class="data-group-card shadow-less">
					<view class="card-head">订单详情</view>
					<view class="data-item"><text class="l">订单单号</text><text class="v selectable">{{ order.orderNo
							}}</text></view>
					<!-- ERP单号 -->
					<view class="data-item" v-if="order.erpDocCode">
						<text class="l">ERP 单号</text>
						<text class="v selectable erp-no">{{ order.erpDocCode }}</text>
					</view>
					<view class="data-item"><text class="l">下单日期</text><text class="v">{{ order.time }}</text></view>
					<view class="data-item"><text class="l">支付方式</text><text class="v">月结扣款</text></view>
				</view>

				<!-- 底部占位 -->
				<view class="list-bottom-placeholder"></view>
			</view>
		</scroll-view>

		<!-- 3. 底部固定操作栏 -->
		<view class="detail-action-bar-fixed" id="footer-bar">
			<view class="action-btn-wrap" v-if="order.status === 0">
				<button class="action-btn cancel" @click="doCancel">撤销该订单</button>
				<button class="action-btn primary" @click="callSales">呼叫业务员</button>
			</view>
			<view class="action-btn-wrap single" v-else>
				<button class="action-btn primary" @click="goHome">再下一单</button>
			</view>
			<view class="safe-area-bottom-support"></view>
		</view>
	</view>
</template>

<script>
import ErpNavBar from '@/components/erp-nav-bar.vue';
import { getOrderDetail } from '@/api/erp/order.js';
import { getPhone } from '@/api/system/phone.js';
export default {
	components: { ErpNavBar },
	data() {
		return {
			footerHeight: 80, // px
			headerHeight: 120, // px
			rowId: '',
			loading: false,
			salesPhone: '',
			order: {
				orderNo: '-',
				status: 1,
				statusName: '加载中',
				statusType: 'pending',
				models: [],
				erpNo: '',
				time: '-'
			}
		}
	},
	computed: {
		statusSubText() {
			const map = {
				pending: '您的订单已提交，正在排队等待审核...',
				expired: '该订单已被驳回。',
				cancelled: '该订单已被撤销。',
				process: '审核已完成，正在由相关主管签批...',
				making: '订单已入库排产，工厂正在全力生产中...',
				finish: '该订单生产已完成并正式入库。'
			};
			return map[this.order.statusType] || '订单状态更新中';
		},
		scrollHeight() {
			const info = uni.getSystemInfoSync();
			const safeBottom = info.safeAreaInsets ? info.safeAreaInsets.bottom : 0;
			const statusBarHeight = uni.getSystemInfoSync().statusBarHeight || 20;
			return `calc(100vh - ${statusBarHeight + 164}px - ${80 + safeBottom}px)`;
		}
	},
	onLoad(options) {
		if (options.rowId) {
			this.rowId = options.rowId;
			this.loadOrderDetail();
			this.loadPhone();
		}
	},
	methods: {
		async loadOrderDetail() {
			if (this.loading) return;
			this.loading = true;
			try {
				const res = await getOrderDetail(this.rowId);
				const data = res.data || res;
				if (!data) {
					uni.showToast({ title: '订单不存在', icon: 'none' });
					return;
				}

				// 状态展示映射（与 ErpOrderStatus 枚举对齐）
				const statusMap = {
					'-1': { name: '已撤销', type: 'cancelled' },
					0: { name: '待审核', type: 'pending' },
					1: { name: '已驳回', type: 'expired' },
					2: { name: '待签批', type: 'process' },
					3: { name: '生产中', type: 'making' },
					4: { name: '已完成', type: 'finish' }
				};
				const s = statusMap[data.status] || { name: '未知', type: 'expired' };

				this.order = {
					orderNo: data.code || '-',
					rowId: data.rowId,
					status: data.status,
					statusName: s.name,
					statusType: s.type,
					time: data.createTime || '-',
					totalCount: data.totalCount || 0,
					erpDocCode: data.erpDocCode || '',
					models: (data.details || []).map(d => ({
						type: d.modelNum || '未知型号',
						typeName: d.modelName || '铝型材主料',
						material: d.material || '6063-T5',
						surface: d.surfaceName || '无',
						package: d.packName || '普通包装',
						length: d.length ? Number(d.length).toFixed(4) : '0',
						wallThickness: d.wallThickness ? Number(d.wallThickness).toFixed(4) : '1.2',
						count: d.count || 0
					}))
				};
			} catch (e) {
				console.error('加载订单详情失败', e);
				uni.showToast({ title: e || '加载失败', icon: 'none' });
			} finally {
				this.loading = false;
			}
		},
		goBack() { uni.navigateBack(); },
		doCancel() {
			uni.showModal({
				title: '确认撤销',
				content: '确定要撤销该订单吗？',
				confirmColor: '#ff3b30',
				success: (res) => {
					if (res.confirm) {
						this.order.status = -1;
						this.order.statusName = '已撤销';
						this.order.statusType = 'cancelled';
						uni.showToast({ title: '撤销成功' });
					}
				}
			});
		},
		callSales() { uni.makePhoneCall({ phoneNumber: this.salesPhone }); },
		goHome() { uni.reLaunch({ url: '/pages/order/index' }); },
		async loadPhone() {
			try {
				const res = await getPhone();
				this.salesPhone = res.data.salesPhone || '13888888888';
			} catch (e) {
				console.error('加载联系电话失败', e);
				uni.showToast({ title: e || '加载联系电话失败', icon: 'none' });
			}
		}
	}
}
</script>

<style scoped>
/deep/ ::-webkit-scrollbar {
	display: none !important;
	width: 0 !important;
	height: 0 !important;
	-webkit-appearance: none;
	background: transparent;
}

.detail-page-container {
	width: 100vw;
	height: 100vh;
	background: #f8fbfd;
	display: flex;
	flex-direction: column;
	overflow: hidden;
	position: relative;
}


.status-banner-container {
	color: #fff;
	flex-shrink: 0;
}

.status-banner-container.pending {
	background: linear-gradient(135deg, #C1001C 0%, #FF4D4F 100%);
}

.status-banner-container.process {
	background: linear-gradient(135deg, #FF6A00 0%, #EE0979 100%);
}

.status-banner-container.making {
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.status-banner-container.finish {
	background: linear-gradient(135deg, #2af598 0%, #009efd 100%);
}

.status-banner-container.expired {
	background: linear-gradient(135deg, #868f96 0%, #596164 100%);
}

.status-banner-container.cancelled {
	background: linear-gradient(135deg, #a0a0a0 0%, #666666 100%);
}

/* 状态横幅内容 */
.status-banner-content {
	padding: 40rpx;
	padding-bottom: 60rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.header-main {
	flex: 1;
}

.status-title {
	font-size: 48rpx;
	font-weight: bold;
	display: block;
	margin-bottom: 12rpx;
}

.status-sub {
	font-size: 26rpx;
	opacity: 0.9;
}

.status-visual-icon {
	width: 60rpx;
	height: 60rpx;
	border: 4rpx solid rgba(255, 255, 255, 0.3);
	border-radius: 50%;
	opacity: 0.6;
}

/* 修正：移除负边距，并明确滚动方向 */
.main-content-scroll {
	width: 100%;
	flex: 1;
}

.detail-inner-box {
	padding: 30rpx;
	padding-top: 10rpx;
}

.data-group-card {
	background: #fff;
	border-radius: 30rpx;
	padding: 40rpx;
	margin-bottom: 30rpx;
	box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.02);
}

/* 第一张卡片增加顶部间隔（红框1优化点） */
.first-card {
	margin-top: 20rpx;
}

.card-head {
	font-size: 30rpx;
	font-weight: bold;
	color: #333;
	margin-bottom: 30rpx;
	border-left: 8rpx solid #C1001C;
	padding-left: 20rpx;
}

.line-split {
	height: 1rpx;
	background: #f5f5f5;
	margin: 24rpx 0;
}

.data-item {
	display: flex;
	justify-content: space-between;
	margin-bottom: 24rpx;
	font-size: 28rpx;
	align-items: center;
}

.l {
	color: #999;
}

.v {
	color: #333;
	font-weight: 500;
}

.v.bold {
	font-weight: bold;
	font-size: 30rpx;
}

.v.erp-no {
	color: #C1001C;
	font-weight: bold;
}

.v.highlight {
	color: #ff3b30;
	font-weight: bold;
	font-size: 34rpx;
}

.list-bottom-placeholder {
	height: 260rpx;
}

.detail-action-bar-fixed {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	background: #fff;
	padding: 30rpx 40rpx;
	box-shadow: 0 -10rpx 40rpx rgba(0, 0, 0, 0.04);
	z-index: 999;
	flex-shrink: 0;
}

.action-btn-wrap {
	display: flex;
	gap: 24rpx;
}

.action-btn {
	flex: 1;
	height: 96rpx;
	border-radius: 48rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 32rpx;
	font-weight: bold;
}

.action-btn.primary {
	background: #C1001C;
	color: #fff;
	border: none;
}

.action-btn.cancel {
	background: #fff1f0;
	color: #ff3b30;
	border: 1rpx solid #ffccc7;
	font-weight: normal;
}

.safe-area-bottom-support {
	height: constant(safe-area-inset-bottom);
	height: env(safe-area-inset-bottom);
}
</style>
