<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" @clickIcon="handleSearch" />
		</view>

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

		<!-- 团餐列表 -->
		<view class="group-meal-list" v-if="identity">
			<view class="meal-item" v-for="(meal, index) in leaderOrderList" :key="meal.id">
				<view class="meal-name">{{ meal.title }}</view>
				<view class="meal-price">本单佣金合计: <text class="price-value">¥{{ (meal.commission || 0).toFixed(2) || 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="leaderOrderList.length == 0" />
			</view>
		</view>

		<!-- 订单列表 -->
		<view class="order-list" v-else>
			<OrderItem v-for="(order, index) in memberOrderList" :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="memberOrderList.length == 0" />
			</view>
		</view>

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

<script>
	import mixinsList from '@/mixins/list.js'
	import tabber from '@/components/base/tabbar.vue'
	import OrderItem from '@/components/order/OrderItem.vue'

	export default {
		mixins: [mixinsList],
		components: {
			tabber,
			OrderItem
		},
		computed: {
			tabs() {
				return this.identity ? this.tabMeal : this.tabOrder
			},
		},
		data() {
			return {
				keyword: '',
				title: '',
				tabOrder: [{
					name: '待支付'
				},
				{
					name: '待出餐'
				},
				{
					name: '送餐中'
				},
				{
					name: '待取餐'
				},
				{
					name: '已完成'
				}
				],
				tabMeal: [{
					name: '待出餐'
				},
				{
					name: '已出餐'
				},
				{
					name: '待取餐'
				},
				{
					name: '已完成'
				}
				],
				status: 0,
				memberOrderList: [],
				leaderOrderList: [],
				identity: uni.getStorageSync('identity'),
				mixinsListApi: '',
				mixinsListKey: '',
			}
		},
		onLoad(args) {			
			// 这里需要做热更新
			this.mixinsListApi = this.identity ? 'queryLeaderOrderList' : 'queryMemberOrderList',
			this.mixinsListKey = this.identity ? 'leaderOrderList' : 'memberOrderList'
			
			// 检查是否有tabIndex参数，如果有则自动切换到对应tab
			if (args.tabIndex !== undefined) {
				const index = parseInt(args.tabIndex)
				if (!isNaN(index) && index >= 0 && index < this.tabs.length) {
					this.status = index
				}
			}
			
		},
		methods: {
			handleSearch() {
				this.title = this.keyword
				this.getData()
				this.keyword = ''
				this.title = ''
			},
			beforeGetData() {
				const params = {
					status: this.status,
					title: this.title || ''
				}
				return params
			},
			//点击tab栏
			clickTabs({
				index
			}) {
				this.status = index
				this.getData()
			},
			// 跳转到新订单详情页
			goToOrderDetail(order) {
				this.$utils.navigateTo({
					url: '/pages_order/order/newOrderDetail?id=' + order.id
				})
			},
			// 查看团餐订单
			viewOrder(meal) {
				this.$utils.navigateTo({
					url: '/pages_order/order/groupMealDetail?id=' + meal.id
				})
			},
			// // 处理取消订单
			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()
								}	
							})

						}
					}
				})
			},
			// 处理取餐完成
			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>