<template>
    <view class="main">
        <!-- 顶部固定导航 -->
        <view class="header">
            <!-- <view class="headerTop" :style="{ height: menu.height + 'px' }">
                <view class="backBtn" @click="handleBack">
                    <image class="backIcon" src="/static/images/home/backArrowblack.svg" />
                </view>
                <view class="pageName">我的订单</view>
                <view class="placeholder"></view>
            </view>

            <view class="filterSection">
                <view class="searchBox">
                    <image class="searchIcon" src="/static/images/home/search_icon.svg" />
                    <input class="searchInput" placeholder="请输入商品名称关键字" v-model="searchText" confirm-type="search"
                        @confirm="handleSearch" />
                </view>
                <view class="filterBtn" @click="handleShowFilter">
                    <image class="filterIcon" src="/static/images/home/select_icon.svg" />
                    <text>筛选</text>
                </view>
            </view> -->

            <!-- Tabs 切换 -->
            <view class="tabs">
                <view v-for="(item, index) in tabList" :key="index" class="tabItem"
                    :class="{ active: currentTab === item.value }" @click="handleTabChange(item.value)">
                    {{ item.label }}
                </view>
            </view>

            <!-- 数据汇总统计 -->
            <view class="summaryLine" v-if="orderList.length > 0">
                共 <text class="highlight">{{ totalCount }}</text> 笔订单,
                合计 <text class="highlight">{{ totalAmount }}</text> 元
            </view>
        </view>

        <!-- 订单列表内容区 -->
        <scroll-view class="listContainer" scroll-y @scrolltolower="handleLoadMore" :style="{ height: listHeight }">
            <view class="orderList" v-if="orderList.length > 0">
                <view class="orderItem" v-for="(order, index) in orderList" :key="index" @click="handleGoDetail(order)">
                    <view class="orderItemHeader">
                        <view class="orderTime">
                            {{ order.SALEBILL_STATE <= 1010 ? '期望送达: ' : '' }}{{ order.RESERVATION_DATE ||
                                order.ORDER_DATE }} </view>
                                <view class="orderStatus">{{ getStatusText(order.SALEBILL_STATE) }}</view>
                        </view>

                        <view class="orderItemContent">
                            <scroll-view class="imgScroll" scroll-x>
                                <view class="imgList">
                                    <view class="imgWrapper" v-for="(img, idx) in order.IMAGELIST" :key="idx">
                                        <image class="productImg"
                                            :src="img.IMAGE_URL || 'https://eshangtech.com/ShopICO/no-picture.png'"
                                            mode="aspectFill" />
                                        <view class="productName">{{ img.IMAGE_TITLE }}</view>
                                    </view>
                                </view>
                            </scroll-view>
                            <view class="orderStats">
                                <view class="amountBox">
                                    <text class="unit">¥</text>
                                    <text class="amount">{{ order.ORDER_AMOUNT }}</text>
                                </view>
                                <view class="count">共{{ order.TOTAL_COUNT }}件</view>
                            </view>
                        </view>

                        <view class="orderItemFooter">
                            <view class="descText" v-if="order.SALEBILL_DESC">{{ order.SALEBILL_DESC }}</view>
                        </view>
                    </view>
                    <view class="noMore" v-if="isOver">我是有底线的</view>
                </view>

                <!-- 空状态 -->
                <view class="emptyBox" v-else>
                    <view class="emptyText">暂无订单记录</view>
                </view>
                <view style="height: 40rpx;"></view>
        </scroll-view>

        <!-- 筛选弹窗 -->
        <uni-popup ref="filterPopup" type="bottom">
            <view class="filterPopupBox">
                <view class="popHeader">筛选时间</view>
                <view class="timeOptions">
                    <view class="timeItem" :class="{ active: timeIndex === item.value }"
                        v-for="(item, index) in timeList" :key="index" @click="timeIndex = item.value">
                        {{ item.label }}
                    </view>
                </view>
                <view class="confirmBtn" @click="handleFilterConfirm">确认</view>
            </view>
        </uni-popup>
    </view>
</template>

<script>
import { mapGetters } from "vuex";
export default {
    data() {
        return {
            menu: {},
            searchText: "",
            currentTab: 1,
            tabList: [
                { label: '全部订单', value: 1 },
                { label: '待发货', value: 2 },
                { label: '已发货', value: 3 },
                { label: '已接收', value: 4 }
            ],
            timeIndex: '',
            timeList: [
                { label: '一周内', value: 'week' },
                { label: '近一月', value: 'month' },
                { label: '近三月', value: 'threeMouth' }
            ],
            orderList: [],
            totalCount: 0,
            totalAmount: "0.00",
            pageIndex: 1,
            pageSize: 10,
            isOver: false,
            isLoading: false,
            listHeight: "0px",
            // 状态映射：参考 buyOrder
            statusQueryMap: {
                1: '',
                2: '1010,3000',
                3: '3050',
                4: '4000,5000'
            }
        };
    },
    computed: {
        ...mapGetters({
            user: "user",
        }),
    },
    onLoad(options) {
        this.menu = uni.getMenuButtonBoundingClientRect();
        if (options.status) {
            // 根据传入的状态码字符串匹配相应的 Tab
            if (options.status === '1090,2000') {
                this.currentTab = 2; // 待发货
            } else if (options.status === '2010') {
                this.currentTab = 3; // 已发货
            } else if (options.status === '3000,4000') {
                this.currentTab = 4; // 已接收
            } else {
                this.currentTab = 1; // 全部
            }
        }
        this.calculateListHeight();
        this.handleRefresh();
    },
    methods: {
        calculateListHeight() {
            // 简单计算：页面高度 - 头部固定高度
            // 头部固定高度约为 menu.bottom + 搜索栏(88rpx) + Tabs(88rpx) + Summary(72rpx) + 间距
            uni.getSystemInfo({
                success: (res) => {
                    const headerHeight = this.menu.bottom + uni.upx2px(88 + 88 + 72 + 40);
                    this.listHeight = (res.windowHeight - headerHeight) + 'px';
                }
            });
        },
        handleBack() {
            uni.navigateBack();
        },
        handleRefresh() {
            this.pageIndex = 1;
            this.isOver = false;
            this.orderList = [];
            this.fetchOrders();
        },
        handleLoadMore() {
            if (this.isOver || this.isLoading) return;
            this.pageIndex++;
            this.fetchOrders();
        },
        handleSearch() {
            this.handleRefresh();
        },
        handleTabChange(val) {
            this.currentTab = val;
            this.handleRefresh();
        },
        handleShowFilter() {
            this.$refs.filterPopup.open();
        },
        handleFilterConfirm() {
            this.$refs.filterPopup.close();
            this.handleRefresh();
        },
        getStatusText(state) {
            if (state === 1090) {
                return '待发货'
            } else if (state === 2000) {
                return '备货中'
            } else if (state === 2010) {
                return '已发货'
            } else if (state === 3000) {
                return '已接收'
            } else if (state === 4000) {
                return '已完成'
            }
        },
        async fetchOrders() {
            // 这里预留后端接口调用
            // 目前使用 mock 数据演示布局
            this.isLoading = true;
            uni.showLoading({ title: "加载中..." });

            const res = await this.$api.$samemberGet('/EShangApiMain/WeChatMall/GetMallOrderList', {
                WeChat_AppId: "wxee018fb96955552a",
                MembershipId: this.user.MEMBERSHIP_ID,
                orderState: this.currentTab === 2 ? '1090,2000' : this.currentTab === 3 ? '2010' : this.currentTab === 4 ? '3000,4000' : this.currentTab === 1 ? '1090,2000,2010,3000,4000' : ''
            })
            let mockList = res.Result_Data.List || [];

            if (this.pageIndex >= 3) {
                this.isOver = true;
            }

            this.orderList = this.pageIndex === 1 ? mockList : [...this.orderList, ...mockList];
            this.totalCount = this.orderList.length;
            this.totalAmount = this.orderList.reduce((sum, item) => sum + parseFloat(item.ORDER_AMOUNT), 0).toFixed(2);

            this.isLoading = false;
            uni.hideLoading();
        },
        handleGoDetail(order) {
            uni.navigateTo({
                url: `/pages/ownWater/waterOrderDetail?id=${order.PURCHASE_ID || order.SALEBILL_ID}`
            });
        }
    }
};
</script>

<style lang="less" scoped>
.main {
    background: #f2f4f5;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.header {
    background: #fff;
    flex-shrink: 0;
    z-index: 100;
    box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.02);

    .headerTop {
        display: flex;
        align-items: center;
        padding: 0 30rpx;
        position: relative;

        .backBtn {
            width: 80rpx;
            height: 100%;
            display: flex;
            align-items: center;

            .backIcon {
                width: 44rpx;
                height: 44rpx;
            }
        }

        .pageName {
            flex: 1;
            text-align: center;
            font-size: 32rpx;
            font-weight: bold;
            color: #020E1A;
        }

        .placeholder {
            width: 80rpx;
        }
    }

    .filterSection {
        display: flex;
        padding: 20rpx 30rpx;
        align-items: center;

        .searchBox {
            flex: 1;
            height: 72rpx;
            background: #F2F4F5;
            border-radius: 36rpx;
            display: flex;
            align-items: center;
            padding: 0 24rpx;
            margin-right: 20rpx;

            .searchIcon {
                width: 32rpx;
                height: 32rpx;
                margin-right: 12rpx;
            }

            .searchInput {
                flex: 1;
                font-size: 28rpx;
                color: #333;
            }
        }

        .filterBtn {
            display: flex;
            align-items: center;
            font-size: 28rpx;
            color: #6C737A;

            .filterIcon {
                width: 32rpx;
                height: 32rpx;
                margin-right: 8rpx;
            }
        }
    }

    .tabs {
        display: flex;
        height: 88rpx;
        border-bottom: 2rpx solid #F2F4F5;

        .tabItem {
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 28rpx;
            color: #6C737A;
            position: relative;

            &.active {
                color: #1890FF;
                font-weight: bold;

                &::after {
                    content: '';
                    position: absolute;
                    bottom: 0;
                    width: 48rpx;
                    height: 6rpx;
                    background: #1890FF;
                    border-radius: 4rpx;
                }
            }
        }
    }

    .summaryLine {
        height: 72rpx;
        padding: 0 30rpx;
        display: flex;
        align-items: center;
        font-size: 26rpx;
        color: #6C737A;

        .highlight {
            color: #020E1A;
            font-weight: bold;
            margin: 0 6rpx;
        }
    }
}

.listContainer {
    flex: 1;
    width: 100%;
}

.orderList {
    padding: 24rpx;

    .orderItem {
        background: #fff;
        border-radius: 16rpx;
        padding: 24rpx;
        margin-bottom: 24rpx;
        box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.03);

        .orderItemHeader {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20rpx;

            .orderTime {
                font-size: 24rpx;
                color: #fa5151;
            }

            .orderStatus {
                font-size: 28rpx;
                color: #1890FF;
                font-weight: 500;
            }
        }

        .orderItemContent {
            display: flex;
            align-items: center;
            margin-bottom: 20rpx;
            position: relative;

            .imgScroll {
                flex: 1;
                white-space: nowrap;
                margin-right: 20rpx;

                .imgList {
                    display: flex;

                    .imgWrapper {
                        display: flex;
                        flex-direction: column;
                        align-items: center;
                        margin-right: 16rpx;

                        .productImg {
                            width: 120rpx;
                            height: 120rpx;
                            border-radius: 8rpx;
                            background: #f8f8f8;
                        }

                        .productName {
                            font-size: 20rpx;
                            color: #666;
                            width: 120rpx;
                            overflow: hidden;
                            text-overflow: ellipsis;
                            text-align: center;
                            margin-top: 4rpx;
                        }
                    }
                }
            }

            .orderStats {
                text-align: right;
                background: linear-gradient(to right, rgba(255, 255, 255, 0), #fff 20%);
                padding-left: 40rpx;

                .amountBox {
                    color: #020E1A;
                    font-weight: bold;
                    margin-bottom: 4rpx;

                    .unit {
                        font-size: 24rpx;
                    }

                    .amount {
                        font-size: 36rpx;
                        font-family: 'DIN Alternate';
                    }
                }

                .count {
                    font-size: 24rpx;
                    color: #9FA3A8;
                }
            }
        }

        .orderItemFooter {
            border-top: 1rpx solid #F2F4F5;
            padding-top: 20rpx;
            display: flex;
            justify-content: space-between;
            align-items: center;

            .descText {
                font-size: 24rpx;
                color: #FF6219;
                flex: 1;
                margin-right: 20rpx;
            }

            .actionBtns {
                display: flex;

                .btn {
                    height: 60rpx;
                    padding: 0 24rpx;
                    border-radius: 30rpx;
                    font-size: 26rpx;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    margin-left: 16rpx;

                    &.normal {
                        border: 1rpx solid #D9DBDD;
                        color: #6C737A;
                    }

                    &.active {
                        border: 1rpx solid #1890FF;
                        color: #1890FF;
                    }
                }
            }
        }
    }
}

.emptyBox {
    padding-top: 200rpx;
    display: flex;
    flex-direction: column;
    align-items: center;

    .emptyImg {
        width: 240rpx;
        height: 240rpx;
        opacity: 0.6;
    }

    .emptyText {
        font-size: 28rpx;
        color: #999;
        margin-top: 20rpx;
    }
}

.noMore {
    text-align: center;
    font-size: 24rpx;
    color: #ccc;
    padding: 20rpx 0;
}

.filterPopupBox {
    background: #fff;
    border-radius: 24rpx 24rpx 0 0;
    padding: 40rpx 30rpx calc(40rpx + env(safe-area-inset-bottom));

    .popHeader {
        font-size: 32rpx;
        font-weight: bold;
        margin-bottom: 40rpx;
        text-align: center;
    }

    .timeOptions {
        display: flex;
        justify-content: space-between;
        margin-bottom: 60rpx;

        .timeItem {
            width: 210rpx;
            height: 72rpx;
            background: #F2F4F5;
            border-radius: 8rpx;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 28rpx;
            color: #333;

            &.active {
                background: #E7F3FF;
                color: #1890FF;
                border: 1rpx solid #1890FF;
            }
        }
    }

    .confirmBtn {
        height: 88rpx;
        background: #1890FF;
        color: #fff;
        border-radius: 44rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 30rpx;
        font-weight: bold;
    }
}
</style>
