<template>
	<view class="page">
		<!-- 导航栏 -->
		<navbar title=" " bgColor="#019245" color="#fff" />

		<!-- 搜索框 -->
		<view style="background-color: #fff; padding: 12rpx 20rpx 0rpx; ">
			<uv-search placeholder="搜索商品名" v-model="keyword" :showAction="false" actionText="" height="80rpx" animation
				bgColor="#F5F5F5" inputAlign="center" color="#000" placeholderColor="#979797"
				searchIconSize="50rpx" @search="handleSearch"></uv-search>
		</view>

		<!-- 订单筛选 -->
		<view class="tabs">
			<uv-tabs :list="tabs" :activeStyle="{ color: '#019245'}" lineColor="#019245" :scrollable="false"
				:inactiveStyle="{color: 'black'}" lineHeight="6rpx" lineWidth="55rpx" :current="current"
				@click="clickTabs" />
		</view>

		<!-- 团餐列表 -->
		<view class="group-meal-list" v-if="identity">
			<view class="meal-item" v-for="(meal, index) in mealList" :key="meal.id">
				<view class="meal-name">{{ meal.title }}</view>
				<view class="meal-price">本单佣金合计: <text class="price-value">¥{{ levelInfo.amount || '0.00' }}</text>
				</view>
				<view class="meal-action">
					<button class="order-btn" @tap="viewOrder(meal)">查看订单</button>
				</view>
			</view>
			<view style="margin-top: 200rpx; min-width: 700rpx;">
				<uv-empty mode="order" v-if="mealList.length == 0" />
			</view>
		</view>

		<!-- 订单列表 -->
		<view class="order-list" v-else>
			<OrderItem v-for="(order, index) in orderList" :key="order.id" :order="order" @cancel="handleCancelOrder(order.id)"
				@pick="handlePickOrder(order.id)" @pay="goToOrderDetail(order)" @click="goToOrderDetail(order)" />
			<view style="margin-top: 200rpx; min-width: 700rpx;">
				<uv-empty mode="order" v-if="orderList.length == 0" />
			</view>
		</view>

		<!-- <view class="list">
			<view class="item" v-for="(item, index) in list" @tap="toOrderDetail(item.id)" :key="index">
				<view class="content" :key="index" v-for="(good, index) in item.commonOrderSkuList">
					<view class="top">
						<view class="service">
							{{ good.title }}
						</view>
						<view class="status">
							<text> {{ tabs[Number(item.state) + 1].name }}</text>
						</view>
					</view>

					<view class="main">
						<view class="left">
							<image mode="aspectFill" :src="good.image && good.image.split(',')[0]"></image>
						</view>

						<view class="right">
							<view class="text-hidden-1">
								客户姓名：{{item.name}}
							</view>
							<view class="text-hidden-1">
								下单时间：{{item.createTime}}
							</view>
							<view class="text-hidden-1">
								联系电话：{{item.phone}}
							</view>

						</view>
					</view>

				</view>

				<view class="bottom">
					<view class="price">
						<text class="total-title">总价格：</text>
						<text class="unit">￥</text>
						<text class="num">{{item.price}}</text>
						<text class="c-unit">元</text>
					</view>

					<view @tap.stop="toPayOrder(item)" class="btn" v-if="item.state == 0">
						立即付款
					</view>

					<view @tap.stop="cancelOrder(item)" class="btn" v-if="item.state == 0">
						取消订单
					</view>

					<view class="btn" @tap.stop="confirmOrder(item)" v-if="item.state == 2">
						确认收货
					</view>

					<view @tap.stop="$refs.customerServicePopup.open()" class="btn" v-if="item.state > 0">
						联系客服
					</view>
				</view>

			</view>
			<view style="
			margin-top: 20rpx;
			min-width: 700rpx;">
				<uv-empty mode="list" v-if="list.length == 0"></uv-empty>
			</view>
		</view> -->

		<customerServicePopup ref="customerServicePopup" />

		<tabber select="order" />
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'
	import mixinsList from '@/mixins/list.js'
	import mixinsOrder from '@/mixins/order.js'
	import tabber from '@/components/base/tabbar.vue'
	import customerServicePopup from '@/components/config/customerServicePopup.vue'
	import OrderItem from '@/components/order/OrderItem.vue'

	export default {
		mixins: [mixinsList, mixinsOrder],
		components: {
			tabber,
			customerServicePopup,
			OrderItem
		},
		computed: {
			orderList() {
				return this.memberOrderList.filter(order => order.status == this.current)
			},
			mealList() {
				return this.leaderOrderList.filter(order => order.status == this.current)
			},
			tabs() {
				return this.identity ? this.tabMeal : this.tabOrder
			}
		},
		data() {
			return {
				keyword: '',
				tabOrder: [{
					name: '待支付'
				},
				{
					name: '待出餐'
				},
				{
					name: '送餐中'
				},
				{
					name: '待取餐'
				},
				{
					name: '已完成'
				}
				],
				tabMeal: [{
					name: '待出餐'
				},
				{
					name: '已出餐'
				},
				{
					name: '待取餐'
				},
				{
					name: '已完成'
				}
				],
				current: 0,
				memberOrderList: [],
				leaderOrderList: [],
				identity: uni.getStorageSync('identity')
			}
		},
		onLoad(args) {
			this.getData()
			
			// 检查是否有tabIndex参数，如果有则自动切换到对应tab
			if (args.tabIndex !== undefined) {
				const index = parseInt(args.tabIndex)
				if (!isNaN(index) && index >= 0 && index < this.tabs.length) {
					this.current = index
					// 更新查询参数
					// if (index == 0) {
					// 	delete this.queryParams.state
					// } else {
					// 	this.queryParams.state = index - 1
					// }
				}
			}
		},
		methods: {
			handleSearch(value) {
				this.$api('queryMemberOrderList', {
					title: value
				}, res => {
					if (res.code == 200) {
						this.memberOrderList = res.result.records
					}
				})
			},
			getData() {
				uni.showLoading({
					title: '加载中'
				})
				if (!this.identity) {
					this.$api('queryMemberOrderList', {}, res => {
						uni.hideLoading()
						if (res.code == 200) {
							this.memberOrderList = res.result.records
						}
					})
				}
				else {
					this.$api('queryLeaderOrderList', {}, res => {
						uni.hideLoading()
						if (res.code == 200) {
							this.leaderOrderList = res.result.records
							
						}
					})
				}
			},
			//点击tab栏
			clickTabs({
				index
			}) {
				this.current = index
				this.getData()
			},
			// 跳转到新订单详情页
			goToOrderDetail(order) {
				if (order.status === 'completed') {
					this.$utils.navigateTo({
						url: '/pages_order/order/newOrderDetail?id=' + order.id + '&status=' + order.status
					})
				} else {
					this.$utils.navigateTo({
						url: '/pages_order/order/newOrderDetail?id=' + order.id + '&status=' + order.status_dictText
					})
				}
			},
			// 查看团餐订单
			viewOrder(meal) {
				this.$utils.navigateTo({
					url: '/pages_order/order/groupMealDetail?id=' + meal.id + '&status=' + meal.status
				})
			},
			// 根据状态筛选订单
			// filterOrdersByStatus(index) {
			// 	this.loadMockOrders() // 先重置数据
				
			// 	// if (index === 0) return // 全部订单不需要筛选
			// 	const targetStatus = this.statusMap[index]
			// 	if (targetStatus) {
			// 		this.orderList = this.orderList.filter(order => order.status === targetStatus)
			// 		this.groupMeals = this.groupMeals.filter(meal => meal.status === targetStatus)
			// 	}
			// },
			// // 处理取消订单
			handleCancelOrder(orderId) {
				uni.showModal({
					title: '提示',
					content: '确定要取消订单吗？',
					confirmColor: '#019245',
					success: (res) => {
						if (res.confirm) {
							// 模拟取消订单API调用
							this.$api('deleteMemberOrderById', {
								memberOrderId: orderId,
							}, res => {
								if (res.code == 200){
									uni.showToast({
										title: '订单已取消',
										icon: 'success'
									})
									this.getData()
								}	
							})

						}
					}
				})
			},
			// 处理支付订单
			// handlePayOrder(orderId) {
			// 	uni.showToast({
			// 		title: '正在跳转支付...',
			// 		icon: 'loading'
			// 	})
				
			// 	// 模拟支付操作，实际项目中应调用支付API
			// 	setTimeout(() => {
			// 		uni.hideToast()
					
			// 		this.$api('payOrder', {
			// 			orderId: orderId,
			// 			payType: 0
			// 		}, res => {
			// 			if (res.code == 200) {
			// 				uni.showToast({
			// 					title: '支付成功',
			// 					icon: 'success'
			// 				})
			// 			}
			// 		})
					
			// 		// 更新订单状态
			// 		const orderIndex = this.orderList.findIndex(item => item.id === orderId)
			// 		if (orderIndex !== -1) {
			// 			this.orderList[orderIndex].status = 'processing'
						
			// 			// 如果当前标签页不是全部或待发货，则移除该订单
			// 			if (this.current !== 0 && this.current !== 2) {
			// 				this.orderList.splice(orderIndex, 1)
			// 			}
			// 		}
			// 	}, 1500)
			// },
			// 处理取餐完成
			handlePickOrder(orderId) {
				uni.showModal( {
					title: '提示',
					content: '确定取餐完成？',
					confirmColor: '#019245',
					success: (res) => {
						if (res.confirm) {
							this.$api('finishMemberOrderById', { 
								memberOrderId: orderId 
							}, res => {
								if (res.code === 200) {
									uni.showToast({
										title: '取餐完成',
										icon: 'success',
										duration: 2000
									})
									this.getData()
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.page {}

	.tabs {
		background: #fff;
		padding-bottom: 4rpx;
	}
	
	.order-list {
		padding: 0 20rpx;
		// position: relative;
	}

	/* 团餐列表样式 */
	.group-meal-list {
		padding: 20rpx;
	}
	
	.meal-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx 20rpx;
		background-color: #fff;
		margin-bottom: 20rpx;
		border-radius: 10rpx;
	}
	
	.meal-info {
		flex: 1;
	}
	
	.meal-name {
		font-size: 32rpx;
		font-weight: 500;
		margin-bottom: 10rpx;
	}
	
	.meal-price {
		font-size: 28rpx;
		color: $uni-color;
		background-color: #ECFEF4;
		padding: 10rpx 20rpx;
		border-radius: 10rpx;
	}
	
	.price-value {
		margin-left: 10rpx;
		font-weight: 500;	
	}
	
	.meal-action {
		margin-left: 20rpx;
	}
	
	.order-btn {
		background-color: $uni-color;
		color: #fff;
		font-size: 28rpx;
		padding: 10rpx 30rpx;
		border-radius: 30rpx;
		line-height: 1.5;
		min-width: 160rpx;
	}

	.list {
		.item {
			width: calc(100% - 40rpx);
			background-color: #fff;
			margin: 20rpx;
			box-sizing: border-box;
			border-radius: 16rpx;
			padding: 30rpx;

			.content {
				.top {
					display: flex;
					justify-content: space-between;
					align-items: center;
					font-size: 34rpx;

					.status {
						font-weight: 600;
						color: #FFAC2F;
                        flex-shrink: 0;
                        margin-left: 20rpx;
					}
				}

				.main {
					display: flex;
					margin: 20rpx 0rpx;

					.left {
						display: flex;
						align-items: center;
						justify-content: center;
						width: 180rpx;
						height: 180rpx;

						image {
							width: 95%;
							height: 95%;
							border-radius: 10rpx;
						}
					}

					.right {
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						width: calc(100% - 200rpx);
						color: #777;
						font-size: 26rpx;
						padding: 30rpx 20rpx;
						box-sizing: border-box;
						margin-left: 20rpx;
						border-radius: 10rpx;
						background-color: #F8F8F8;
					}
				}
			}

			.bottom {
				display: flex;
				justify-content: space-between;
				font-size: 25rpx;

				.price {
					.total-title {}

					.num {
						font-size: 36rpx;
					}

					.num,
					.unit,
					.c-unit {
						color: $uni-color;
					}
				}

				.btn {
					border: 1px solid #C7C7C7;
					padding: 10rpx 20rpx;
					border-radius: 40rpx;
					color: #575757;
				}
			}
		}
	}
</style>