<template>
	<view>
		<view class="header-buts flex flex-center">
			<view class="flex buts-box">
				<view class="buts" :class="{'buts-active':activeIndex===1}" @click="clickTab(1)">总订单</view>
				<view class="buts" :class="{'buts-active':activeIndex===2}" @click="clickTab(2)">日订单</view>
			</view>
		</view>

		<up-sticky bgColor="#fff" v-if="activeIndex == 1">
			<view class="container-tabs">
				<up-tabs @click="classIfyClickTab" :list="tabList1" lineWidth="68rpx" :activeStyle="{
					color: '#FFFFFF',
					fontWeight: 'bold',
					transform: 'scale(1.05)'
				}" :inactiveStyle="{
					color: '#FFFFFF',
					transform: 'scale(1)'
				}" :itemStyle="{height:'88rpx',width : '33%'}" 
				
				lineColor="#FFFFFF"></up-tabs>
			</view>
		</up-sticky>

		<up-sticky bgColor="#fff" v-else>
			<view class="container-tabs">
				<up-tabs :list="tabList2" lineWidth="68rpx" :activeStyle="{
					color: '#FFFFFF',
					fontWeight: 'bold',
					transform: 'scale(1.05)'
				}" :inactiveStyle="{
					color: '#FFFFFF',
					transform: 'scale(1)'
				}" :itemStyle="{height:'88rpx',padding:'0 52rpx', width : '400rpx'}" 
				@click="classIfyClickTab"
				lineColor="#FFFFFF"></up-tabs>
			</view>
		</up-sticky>

		<view class="container">
			<!-- 加载中状态 -->
			<view v-if="loading" class="loading-container">
				<up-loading-icon size="36" mode="circle" color="#FFAA48"></up-loading-icon>
				<text class="loading-text">加载中...</text>
			</view>
			
			<!-- 总订单内容 -->
			<systemOrder :list="list" :current="current" v-else-if="activeIndex == 1" />
			
			<!-- 日订单内容 -->
            <view v-else>
                <view v-if="dateOrderList && dateOrderList.length > 0">
                    <timelineService 
                    v-for="(item,index) in dateOrderList"
                    :key="index"
                    :date="item.serviceDate"
                    :orderCount="item.num"
                    :status="current === 0"
                    :list="item.orderList"
                    :current="current"
                    />
                </view>
                <view v-else class="empty-state">
                    <image src="/static/images/ydd/empty.png" mode="aspectFit" class="empty-image"></image>
                    <text class="empty-text">暂无订单数据</text>
                </view>
            </view>

			<!-- <orderListByData :list="list" v-else /> -->
		</view>
	</view>
</template>

<script setup>
	import {
		computed,
		reactive,
		ref
	} from "vue";
	import systemOrder from "./components/systemOrder.vue";
	import orderListByData from "./components/orderListByData.vue";
	import timelineService from "./components/timelineService.vue";
	import {
		onShow,
        onLoad,
	} from "@dcloudio/uni-app"
	import {
		getLoginStatus
	} from "@/utils/useMixin.js"
	// import personOrder from "./components/personOrder.vue";
	// import lossOrder from "./components/lossOrder.vue";
	import {
		myList
	} from "@/api/receivingHall/index.js"
	import {
		getOrderDateList
	} from "@/api/order/order.js"
	import {
		useStore
	} from "vuex"
import { getOrderServiceText, getProductNameText } from '@/utils/serviceTime.js'

	onShow(() => {
		if (!getLoginStatus()) return;
		getList();
	})

	const current = ref(0)
	const activeIndex = ref(1)
	const list = ref([])
	const dateOrderList = ref([])
	const loading = ref(false) // 添加loading状态
	const store = useStore();

	const userInfo = computed(() => {
		return store.getters.userInfo;
	})

	const tabList1 = reactive([{
			name: '待服务',
			badge: {
				value: 0,
			}
		},
		{
			name: '进行中',
			badge: {
				value: 0,
			}
		},
		{
			name: '已完成',
			badge: {
				value: 0,
			}
		},
	])

	const tabList2 = reactive([{
			name: '待上门',
			badge: {
				value: 0,
			}
		},
		{
			name: '已完成',
			badge: {
				value: 0,
			}
		},
	])

	function getList() {
		loading.value = true; // 开始加载

        let index = current.value;

		if (activeIndex.value == 1) {
			myList({
                orderStatus: index,
                userId: userInfo.value.userId
            })
            .then(res => {
                if (res.code == 200) {
                    list.value = res.data.rows
                    
                    tabList1[index].badge.value = res.data.total
                    
                    list.value.forEach(item => {
                        item.h5OrderVO.petVOList.forEach(pet => {
                            pet.orderServiceText = getOrderServiceText(pet.id, item.h5OrderVO.orderServiceList)
                            pet.productNameText = getProductNameText(pet.id, item.h5OrderVO.orderItemList, item.h5OrderVO.orderServiceList)
                        })
                    })
                }
            })
            .finally(() => {
                if(activeIndex.value == 1){
                    loading.value = false; // 结束加载
                }
            })
		}else{
            dateOrderList.value = []
            getOrderDateList({
                status: index,
                userId: userInfo.value.userId
            })
            .then(res => {
                if (res.code == 200) {
                    dateOrderList.value = res.data
                    // 更新标签数量显示
                    let totalOrders = 0;
                    dateOrderList.value.forEach(item => {
                        totalOrders += item.num || 0;
                    });
                    tabList2[index].badge.value = totalOrders;
                }
            })
            .finally(() => {
                if(activeIndex.value == 2){
                    loading.value = false; // 结束加载
                }
            })
        }
	}

    function clickTab(index) {
        current.value = 0;
        activeIndex.value = index;
        getList();
    }

	const classIfyClickTab = (item) => {
		current.value = item.index;
		getList();
	}
</script>

<style scoped lang="scss">
	@import "index.scss";
	
	.loading-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 40rpx;
		
		.loading-text {
			margin-top: 20rpx;
			color: #999;
			font-size: 28rpx;
		}
	}
	
	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 80rpx 40rpx;
		
		.empty-image {
			width: 200rpx;
			height: 200rpx;
			margin-bottom: 20rpx;
		}
		
		.empty-text {
			color: #999;
			font-size: 28rpx;
		}
	}
</style>