<template>
	<view class="list-page-container">
		<erp-nav-bar title="全部订单" />

		<!-- 2. 分类切换：使用翻译位移，彻底消除红框2残影 -->
		<view class="tabs-fixed">
			<view class="tabs-box">
				<view v-for="(tab, index) in tabs" :key="index" class="tab-item"
					:class="{ active: currentTab === index }" @click="switchTab(index)">
					<text class="tab-txt">{{ tab }}</text>
				</view>
				<!-- 指示器：只负责位移，不负责显隐，彻底杜绝虚影 -->
				<view class="indicator-track">
					<view class="indicator-bar" :style="{ transform: 'translateX(' + (currentTab * 100) + '%)' }">
						<view class="bar-inner"></view>
					</view>
				</view>
			</view>
		</view>

		<!-- 3. 固定高度的滚动区：强制启用滚动，解决不能滑动问题 -->
		<scroll-view scroll-y class="order-scroll-view" :style="{ height: scrollHeight }" @scrolltolower="onReachEnd"
			:refresher-enabled="false" :show-scrollbar="false">

			<view class="order-list-inner">
				<view class="order-card" v-for="(item, index) in displayList" :key="index" @click="goDetail(item)">
					<view class="card-head">
						<text class="order-id">单号：{{ item.orderNo }}</text>
						<view class="status-badge" :class="item.statusType">{{ item.statusName }}</view>
					</view>
					<view class="card-body">
						<view class="model-row" v-for="(model, mIdx) in item.models.slice(0, 2)" :key="mIdx">
							<text class="m-type">{{ model.type }}</text>
							<text class="m-spec">{{ model.surface }} | {{ model.length }}mm</text>
							<text class="m-count">{{ model.count }} 支</text>
						</view>
						<view class="more-hint" v-if="item.models.length > 2">
							<text>查看更多 {{ item.models.length - 2 }} 个型号...</text>
						</view>
						<view class="total-summary">
							<text class="summary-label">共计：</text>
							<text class="summary-val">{{ item.models.length }}</text>
							<text class="summary-unit">个型号</text>
							<view class="summary-split"></view>
							<text class="summary-val highlight">{{ item.totalCount }}</text>
							<text class="summary-unit">支</text>
						</view>
					</view>
					<view class="card-foot">
						<text class="time">{{ item.time }}</text>
						<view class="btns">
							<view class="btn-audit" v-if="item.status > 0" @click.stop="showAuditHistory(item)">审核记录
							</view>
							<view class="btn-cancel" v-if="item.status === 0" @click.stop="onCancel(item)">撤销</view>
							<view class="btn-view primary" @click.stop="goDetail(item)">订单详情</view>
						</view>
					</view>
				</view>

				<!-- 加载提示区 -->
				<view class="list-status-info" v-if="displayList.length > 0">
					<view class="loading-wrap" v-if="loading">
						<view class="load-dot"></view><text>排队加载中...</text>
					</view>
					<view class="nomore-wrap" v-if="noMore">
						<text class="nomore-line"></text>
						<text class="nomore-text">已加载全部数据</text>
						<text class="nomore-line"></text>
					</view>
				</view>

				<!-- 缺省态 -->
				<view class="empty-state" v-if="displayList.length === 0 && !loading">
					<image src="https://img.icons8.com/clouds/200/open-box.png" mode="aspectFit"></image>
					<text>暂无订单记录</text>
				</view>

				<view class="safe-bottom"></view>
			</view>
		</scroll-view>

		<!-- 底部菜单栏 -->
		<!-- <erp-tab-bar active="order"></erp-tab-bar> -->

		<!-- 审核记录弹窗 -->
		<view class="overlay" v-if="auditHistoryVisible" @click="auditHistoryVisible = false">
			<view class="audit-history-modal" @click.stop>
				<view class="modal-head">
					<text class="modal-title">审核记录</text>
					<text class="modal-close" @click="auditHistoryVisible = false">关闭</text>
				</view>
				<scroll-view scroll-y class="modal-body">
					<view class="audit-item" v-for="(item, idx) in auditHistoryList" :key="idx">
						<view class="audit-dot" :class="item.auditResult === 1 ? 'pass' : 'reject'"></view>
						<view class="audit-content">
							<view class="audit-row">
								<text class="audit-result" :class="item.auditResult === 1 ? 'pass' : 'reject'">{{
									item.auditResult === 1 ? '通过' : '驳回' }}</text>
								<text class="audit-auditor">{{ item.auditorName || '未知' }}</text>
							</view>
							<text class="audit-reason" v-if="item.rejectReason">驳回理由：{{ item.rejectReason }}</text>
							<text class="audit-time">{{ item.auditTime }}</text>
						</view>
					</view>
					<view class="audit-empty" v-if="auditHistoryList.length === 0">
						<text>暂无审核记录</text>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
import ErpTabBar from '@/components/erp-tab-bar.vue';
import ErpNavBar from '@/components/erp-nav-bar.vue';
import { listMyOrder, cancelOrder, getAuditHistory } from '@/api/erp/order.js';
export default {
	components: { ErpTabBar, ErpNavBar },
	data() {
		return {
			statusBarHeight: 20,
			navBarHeight: 44,
			tabBarHeight: 50, // 对应 100rpx
			currentTab: 0,
			loading: false,
			noMore: false,
			pageNum: 1,
			tabs: ['全部', '已撤销', '待审核', '已驳回', '待签批', '生产中', '已完成'],
			allOrders: [],
			displayList: [],
			auditHistoryVisible: false,
			auditHistoryList: []
		}
	},
	computed: {
		scrollHeight() {
			// 减去状态栏、导航栏、选项卡的高度
			return `calc(100vh - ${this.statusBarHeight + this.navBarHeight + this.tabBarHeight}px)`;
		}
	},
	onLoad(options) {
		const info = uni.getSystemInfoSync();
		this.statusBarHeight = info.statusBarHeight;
		// 修正选项卡高度（单位px）
		this.tabBarHeight = info.windowWidth / 750 * 110;

		if (options.tab) this.currentTab = parseInt(options.tab);
		this.refresh();
	},
	methods: {
		goBack() { uni.navigateBack(); },
		switchTab(i) { this.currentTab = i; this.refresh(); },
		refresh() { this.displayList = []; this.noMore = false; this.pageNum = 1; this.loadData(); },
		onReachEnd() { if (!this.loading && !this.noMore) this.loadData(); },
		async loadData() {
			if (this.loading || this.noMore) return;
			this.loading = true;
			try {
				const params = {
					pageNum: this.pageNum,
					pageSize: 5
				};
				if (this.currentTab > 0) {
					const tabToStatus = [undefined, -1, 0, 1, 2, 3, 4];
					params.status = tabToStatus[this.currentTab];
				}
				const res = await listMyOrder(params);
				const rows = res.rows || [];

				const formattedRows = rows.map(item => {
					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[item.status] || { name: '未知', type: 'expired' };

					return {
						orderNo: item.code,
						rowId: item.rowId,
						status: item.status,
						statusName: s.name,
						statusType: s.type,
						time: item.createTime,
						totalCount: item.totalCount || 0,
						models: (item.details || []).map(d => ({
							type: d.modelNum || '未知型号',
							length: Number(d.length || 0).toFixed(4),
							surface: d.surfaceName || '无',
							count: d.count || 0
						}))
					};
				});

				this.displayList = [...this.displayList, ...formattedRows];
				this.pageNum++;
				this.noMore = rows.length === 0 || this.displayList.length >= (res.total || 0);
			} catch (e) {
				console.error('加载订单列表失败', e);
				uni.showToast({ title: e || '加载订单列表失败', icon: 'none' });
			} finally {
				this.loading = false;
			}
		},
		async onCancel(item) {
			uni.showModal({
				title: '确认撤销',
				content: `确认要撤销订单：${item.orderNo} 吗？`,
				confirmColor: '#ff4d4f',
				success: async (res) => {
					if (res.confirm) {
						uni.showLoading({ title: '撤销中' });
						try {
							await cancelOrder(item.rowId);
							uni.hideLoading();
							uni.showToast({ title: '已撤销', icon: 'success' });
							this.refresh();
						} catch (e) {
							uni.hideLoading();
							uni.showToast({ title: e || '撤销失败', icon: 'none' });
						}
					}
				}
			});
		},
		async showAuditHistory(item) {
			uni.showLoading({ title: '加载中' });
			try {
				const res = await getAuditHistory(item.rowId);
				this.auditHistoryList = res.data || [];
			} catch (e) {
				uni.showToast({ title: e || '加载审核记录失败', icon: 'none' });
				this.auditHistoryList = [];
			} finally {
				uni.hideLoading();
				this.auditHistoryVisible = true;
			}
		},
		goDetail(item) {
			uni.navigateTo({
				url: `/pages/order/detail/index?rowId=${item.rowId}`
			});
		}
	}
}
</script>

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

.list-page-container {
	width: 100vw;
	height: 100vh;
	background: #f8fafc;
	overflow: hidden;
	display: flex;
	flex-direction: column;
}


/* 2. 选项卡：解决残影红框2 */
.tabs-fixed {
	background: #fff;
	width: 100%;
	flex-shrink: 0;
	border-bottom: 1rpx solid #f0f0f0;
}

.tabs-box {
	height: 110rpx;
	position: relative;
	display: flex;
	width: 100%;
}

.tab-item {
	flex: 1;
	display: flex;
	align-items: center;
	justify-content: center;
	z-index: 5;
}

.tab-txt {
	font-size: 28rpx;
	color: #888;
	transition: all 0.2s;
}

.tab-item.active .tab-txt {
	color: #C1001C;
	font-weight: bold;
	font-size: 32rpx;
}

/* 指示器轨道：通过位移消除虚影 */
.indicator-track {
	position: absolute;
	bottom: 10rpx;
	left: 0;
	width: 100%;
	height: 6rpx;
	display: flex;
}

.indicator-bar {
	width: 14.285%;
	height: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: transform 0.3s cubic-bezier(0.18, 0.89, 0.32, 1.28);
}

.bar-inner {
	width: 40rpx;
	height: 100%;
	background: #C1001C;
	border-radius: 6rpx;
}

/* 3. 滚动区：解决不可滑动问题 */
.order-scroll-view {
	width: 100%;
}

.order-list-inner {
	padding: 30rpx;
	padding-bottom: 60rpx;
}

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

.card-head {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 30rpx;
}

.order-id {
	font-size: 28rpx;
	color: #1a1a1a;
	font-weight: bold;
}

.status-badge {
	font-size: 20rpx;
	padding: 4rpx 16rpx;
	border-radius: 8rpx;
}

.status-badge.pending {
	background: #FFF1F2;
	color: #C1001C;
}

.status-badge.expired {
	background: #f5f5f5;
	color: #999;
}

.status-badge.cancelled {
	background: #f0f0f0;
	color: #666;
}

.status-badge.process {
	background: #fff7e6;
	color: #ffa940;
}

.status-badge.making {
	background: #e6fffb;
	color: #36cfc9;
}

.status-badge.finish {
	background: #f6ffed;
	color: #52c41a;
}

.card-body {
	padding: 24rpx 0;
	border-top: 1rpx dashed #f0f0f0;
	margin-bottom: 24rpx;
}

.model-row {
	display: flex;
	align-items: center;
	margin-bottom: 12rpx;
	font-size: 24rpx;
}

.m-type {
	font-size: 28rpx;
	font-weight: bold;
	color: #333;
	width: 140rpx;
}

.m-spec {
	color: #888;
	flex: 1;
	padding: 0 20rpx;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.m-count {
	color: #555;
	width: 100rpx;
	text-align: right;
	font-weight: 500;
}

.more-hint {
	padding: 10rpx 0;
	font-size: 24rpx;
	color: #999;
	text-align: center;
	background: #fafafa;
	border-radius: 8rpx;
	margin-bottom: 16rpx;
}

.total-summary {
	display: flex;
	align-items: center;
	justify-content: flex-end;
	padding-top: 20rpx;
	border-top: 1rpx solid #fafafa;
}

.summary-label {
	font-size: 24rpx;
	color: #999;
}

.summary-val {
	font-size: 32rpx;
	font-weight: bold;
	color: #333;
	margin: 0 4rpx;
}

.summary-val.highlight {
	color: #C1001C;
}

.summary-unit {
	font-size: 22rpx;
	color: #999;
	margin-right: 12rpx;
}

.summary-split {
	width: 1rpx;
	height: 20rpx;
	background: #eee;
	margin: 0 16rpx;
}

.card-foot {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding-top: 10rpx;
}

.time {
	font-size: 24rpx;
	color: #bbb;
}

.btns {
	display: flex;
	gap: 16rpx;
}

.btn-cancel,
.btn-view {
	padding: 12rpx 30rpx;
	border-radius: 30rpx;
	font-size: 24rpx;
}

.btn-cancel {
	border: 1rpx solid #ddd;
	color: #666;
}

.btn-audit {
	border: 1rpx solid #ddd;
	color: #C1001C;
	padding: 12rpx 20rpx;
	border-radius: 30rpx;
	font-size: 24rpx;
}

.btn-view.primary {
	background: #C1001C;
	color: #fff;
}

.list-status-info {
	padding: 40rpx 0;
	display: flex;
	justify-content: center;
}

.nomore-wrap {
	display: flex;
	align-items: center;
	color: #ccc;
	font-size: 24rpx;
}

.nomore-line {
	width: 40rpx;
	height: 1rpx;
	background: #eee;
	margin: 0 20rpx;
}

.loading-wrap {
	color: #999;
	font-size: 24rpx;
	display: flex;
	align-items: center;
}

.load-dot {
	width: 10rpx;
	height: 10rpx;
	background: #C1001C;
	border-radius: 50%;
	margin-right: 10rpx;
	animation: flash 0.6s infinite alternate;
}

@keyframes flash {
	from {
		opacity: 0.3;
	}

	to {
		opacity: 1;
	}
}

.empty-state {
	display: flex;
	flex-direction: column;
	align-items: center;
	padding-top: 200rpx;
	color: #bbb;
	font-size: 28rpx;
}

.empty-state image {
	width: 220rpx;
	height: 220rpx;
	margin-bottom: 30rpx;
	opacity: 0.6;
}

.safe-bottom {
	height: 40rpx;
}

/* 审核记录弹窗 */
.overlay {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background: rgba(0, 0, 0, 0.45);
	z-index: 1000;
	display: flex;
	align-items: center;
	justify-content: center;
}

.audit-history-modal {
	width: 640rpx;
	max-height: 70vh;
	background: #fff;
	border-radius: 24rpx;
	overflow: hidden;
	display: flex;
	flex-direction: column;
}

.modal-head {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 30rpx 36rpx;
	border-bottom: 1rpx solid #f0f0f0;
	flex-shrink: 0;
}

.modal-title {
	font-size: 32rpx;
	font-weight: bold;
	color: #1a1a1a;
}

.modal-close {
	font-size: 26rpx;
	color: #999;
	padding: 8rpx;
}

.modal-body {
	flex: 1;
	padding: 20rpx 36rpx 36rpx;
	max-height: 60vh;
}

.audit-item {
	display: flex;
	padding: 24rpx 0;
	border-left: 2rpx solid #eee;
	margin-left: 12rpx;
	position: relative;
}

.audit-item:last-child {
	border-left-color: transparent;
}

.audit-dot {
	width: 16rpx;
	height: 16rpx;
	border-radius: 50%;
	position: absolute;
	left: -9rpx;
	top: 28rpx;
}

.audit-dot.pass {
	background: #52c41a;
}

.audit-dot.reject {
	background: #ff4d4f;
}

.audit-content {
	margin-left: 30rpx;
	flex: 1;
}

.audit-row {
	display: flex;
	align-items: center;
	gap: 16rpx;
	margin-bottom: 8rpx;
}

.audit-result {
	font-size: 26rpx;
	font-weight: bold;
}

.audit-result.pass {
	color: #52c41a;
}

.audit-result.reject {
	color: #ff4d4f;
}

.audit-auditor {
	font-size: 24rpx;
	color: #666;
}

.audit-reason {
	display: block;
	font-size: 24rpx;
	color: #ff4d4f;
	background: #fff1f0;
	padding: 12rpx 16rpx;
	border-radius: 8rpx;
	margin-bottom: 8rpx;
	line-height: 1.5;
}

.audit-time {
	display: block;
	font-size: 22rpx;
	color: #bbb;
	margin-top: 4rpx;
}

.audit-empty {
	text-align: center;
	padding: 60rpx 0;
	color: #bbb;
	font-size: 28rpx;
}
</style>
