<template>
    <view class="order-detail-page">
        <!-- 顶部状态栏 -->
        <view class="status-header">
            <view class="status-text" v-if="orderInfo.cancelStatus == 1">已取消</view>
            <view class="status-text" v-else-if="orderInfo.refundStatus == 3">交易关闭</view>
            <view class="status-text" v-else-if="orderInfo.refundStatus == 2">退款中</view>
            <view class="status-text" v-else>{{ statusText }}</view>
            <view class="status-desc" v-if="orderInfo.status == '2'">
                订单编号：{{ orderInfo.orderId }}
                <u-icon name="copy" size="28" @click="copyOrderNo" style="margin-left: 10rpx;"></u-icon>
            </view>
            <view class="status-desc" v-else-if="orderInfo.status == '3'">
                订单编号：{{ orderInfo.orderId }}
                <u-icon name="copy" size="28" @click="copyOrderNo" style="margin-left: 10rpx;"></u-icon>
            </view>
            <view class="status-desc-block" v-else-if="orderInfo.status == '8'">
                <view class="status-tip">还剩9天22时18分自动确认收货</view>
                <view class="status-desc-row">
                    订单编号：{{ orderInfo.orderId }}
                    <u-icon name="copy" size="28" @click="copyOrderNo" style="margin-left: 10rpx;"></u-icon>
                </view>
            </view>
            <view class="status-desc" v-else>
                订单编号：{{ orderInfo.orderId }}
                <u-icon name="copy" size="28" @click="copyOrderNo" style="margin-left: 10rpx;"></u-icon>
            </view>
        </view>

        <!-- 地址信息 -->
        <view class="info-card address-card">
            <view class="icon-box">
                <image src="/pages-mine/static/adderss.png" style="width: 40rpx; height: 40rpx"></image>
            </view>
            <view class="address-content">
                <view class="user-info">
                    <text class="name">{{ orderInfo.receiverName }}</text>
                    <text class="mobile">{{ orderInfo.receiverMobile }}</text>
                </view>
                <view class="address-detail">{{ orderInfo.receiverAddress }}</view>
            </view>
            <u-icon name="arrow-right" color="#999" size="28"></u-icon>
        </view>

        <!-- 物流信息 (已发货/已完成/退款) -->
        <view class="info-card logistics-card"
            v-if="['8', '12', '10'].includes(String(orderInfo.status)) && orderInfo.waybillCode">
            <view class="icon-box">
                <u-icon name="car-fill" color="#38C148" size="40"></u-icon>
            </view>
            <view class="logistics-content" @click="viewLogistics">
                <view class="company-name">物流单号: {{ orderInfo.waybillCode }}</view>
                <view class="latest-trace">点击查看物流详情</view>
            </view>
            <u-icon name="arrow-right" color="#999" size="28"></u-icon>
        </view>

        <!-- 商品列表 -->
        <view class="info-card goods-card">
            <view class="goods-item" v-for="(goods, index) in orderInfo.detailVoList" :key="index">
                <image :src="goods.cover" mode="aspectFill" class="goods-cover"></image>
                <view class="goods-info">
                    <view class="goods-title">{{ goods.bookName }}</view>
                    <view class="goods-quality" v-if="goods.isbn">ISBN：{{ goods.isbn }}</view>
                    <view class="price-box">
                        <text class="price">¥{{ goods.price }}</text>
                        <text class="num">x{{ goods.num }}</text>
                    </view>
                </view>
                <!-- 商品级按钮，如退款 -->
                <view class="goods-action" v-if="['3', '8'].includes(String(orderInfo.status))">
                    <u-button size="mini" shape="circle" plain
                        custom-style="margin-left: 20rpx; height: 50rpx; line-height: 50rpx;">退款</u-button>
                </view>
                <view class="goods-action" v-if="['10'].includes(String(orderInfo.status))">
                    <u-button size="mini" shape="circle" plain disabled
                        custom-style="margin-left: 20rpx; height: 50rpx; line-height: 50rpx; background: #f5f5f5; color: #999; border: none;">退款成功</u-button>
                </view>
            </view>

            <!-- 价格明细 -->
            <view class="price-detail">
                <view class="row">
                    <text>邮费</text>
                    <text>¥ {{ orderInfo.expressMoney || '0.00' }}</text>
                </view>
                <view class="row">
                    <text>商品金额</text>
                    <text class="goods-total">¥{{ orderInfo.totalMoney }}</text>
                </view>
                <view class="row">
                    <text>优惠金额</text>
                    <text class="discount">-¥{{ orderInfo.discountMoney || '0.00' }}</text>
                </view>
                <view class="real-pay-row">
                    <text>实付款 ({{ orderInfo.payType == 1 ? '余额支付' : '微信支付' }})</text>
                    <text class="real-price">¥ {{ orderInfo.payMoney }}</text>
                </view>
            </view>
        </view>

        <!-- 订单时间信息 -->
        <view class="info-card time-card">
            <view class="row">
                <text class="label">下单时间</text>
                <text class="value">{{ orderInfo.createTime }}</text>
            </view>
            <view class="row" v-if="orderInfo.payTime">
                <text class="label">付款时间</text>
                <text class="value">{{ orderInfo.payTime }}</text>
            </view>
            <view class="row" v-if="orderInfo.deliveryTime">
                <text class="label">发货时间</text>
                <text class="value">{{ orderInfo.deliveryTime }}</text>
            </view>
            <view class="row" v-if="orderInfo.finishTime">
                <text class="label">完成时间</text>
                <text class="value">{{ orderInfo.finishTime }}</text>
            </view>
        </view>

        <!-- 底部操作栏 -->
        <order-bottom-bar :orderInfo="orderInfo" @action="handleAction"></order-bottom-bar>

        <!-- 占位符，防止底部按钮遮挡内容 -->
        <view style="height: 120rpx;"></view>
    </view>
</template>

<script>
    import OrderBottomBar from '../components/order-bottom-bar.vue';

    export default {
        components: {
            OrderBottomBar
        },
        data() {
            return {
                orderInfo: {
                    status: '2',
                    orderId: '',
                    detailVoList: [],
                    receiverName: '',
                    receiverMobile: '',
                    receiverAddress: '',
                    expressMoney: 0,
                    totalMoney: 0,
                    discountMoney: 0,
                    payMoney: 0,
                    createTime: ''
                }
            };
        },
        computed: {
            statusText() {
                const map = {
                    '1': '等待买家付款',
                    '2': '等待卖家发货',
                    '3': '已发货',
                    '4': '交易完成',
                };
                return map[String(this.orderInfo.status)] || '未知状态';
            }
        },
        onLoad(options) {
            if (options.orderId) {
                this.loadOrderDetail(options.orderId);
            }
        },
        methods: {
            loadOrderDetail(orderId) {
                uni.$u.http.get('/token/shop/order/getOrderDetail', {
                    orderId: orderId
                }).then((res) => {
                    if (res.code === 200) {
                        this.orderInfo = res.data;
                    }
                });
            },
            copyOrderNo() {
                uni.setClipboardData({
                    data: String(this.orderInfo.orderId),
                    success: () => {
                        uni.showToast({ title: '复制成功', icon: 'none' });
                    }
                });
            },
            viewLogistics() {
                uni.navigateTo({
                    url: `/pages-car/pages/logistics-detail?orderId=${this.orderInfo.orderId}`
                });
            },
            handleAction(type) {
                if (type === 'complaint') {
                    uni.setStorageSync('tempComplaintOrder', this.orderInfo);
                    uni.navigateTo({
                        url: `/pages-car/pages/complaint?orderId=${this.orderInfo.orderId}`
                    });
                    return;
                }
                uni.showToast({ title: `点击了${type}`, icon: 'none' });
            }
        }
    };
</script>

<style lang="scss" scoped>
    .order-detail-page {
        min-height: 100vh;
        background-color: #F5F5F5;
        padding-bottom: 20rpx;

        .status-header {
            background-color: #d1f2d8; // Light green bg
            padding: 40rpx 30rpx;
            color: #333;

            .status-text {
                font-size: 36rpx;
                font-weight: bold;
                margin-bottom: 10rpx;
            }

            .status-desc {
                font-size: 26rpx;
                color: #666;
                display: flex;
                align-items: center;
            }

            .status-desc-block {
                font-size: 26rpx;
                color: #666;

                .status-tip {
                    margin-bottom: 10rpx;
                }

                .status-desc-row {
                    display: flex;
                    align-items: center;
                }
            }
        }

        .info-card {
            background-color: #fff;
            margin: 20rpx;
            border-radius: 16rpx;
            padding: 30rpx;
            display: flex;

            &.address-card {
                align-items: center;

                .icon-box {
                    margin-right: 20rpx;
                }

                .address-content {
                    flex: 1;
                    margin-right: 20rpx;

                    .user-info {
                        font-size: 30rpx;
                        font-weight: 500;
                        margin-bottom: 10rpx;

                        .mobile {
                            margin-left: 20rpx;
                        }
                    }

                    .address-detail {
                        font-size: 26rpx;
                        color: #666;
                        line-height: 1.4;
                    }
                }
            }

            &.logistics-card {
                align-items: center;

                .icon-box {
                    margin-right: 20rpx;
                }

                .logistics-content {
                    flex: 1;
                    margin-right: 20rpx;

                    .company-name {
                        font-size: 28rpx;
                        color: #38C148;
                        margin-bottom: 6rpx;
                    }

                    .latest-trace {
                        font-size: 26rpx;
                        color: #333;
                        margin-bottom: 6rpx;
                        display: -webkit-box;
                        -webkit-box-orient: vertical;
                        -webkit-line-clamp: 2;
                        overflow: hidden;
                    }

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

            &.goods-card {
                display: block;

                .goods-item {
                    display: flex;
                    margin-bottom: 30rpx;

                    .goods-cover {
                        width: 120rpx;
                        height: 120rpx;
                        border-radius: 8rpx;
                        margin-right: 20rpx;
                        background-color: #eee;
                    }

                    .goods-info {
                        flex: 1;

                        .goods-title {
                            font-size: 28rpx;
                            color: #333;
                            margin-bottom: 10rpx;
                        }

                        .goods-quality {
                            font-size: 24rpx;
                            color: #999;
                        }

                        .price-box {
                            margin-top: 10rpx;
                            display: flex;
                            justify-content: space-between;

                            .price {
                                font-weight: 500;
                            }

                            .num {
                                color: #999;
                            }
                        }
                    }

                    .goods-action {
                        display: flex;
                        align-items: center;
                    }
                }

                .price-detail {
                    border-top: 1rpx solid #eee;
                    padding-top: 20rpx;

                    .row {
                        display: flex;
                        justify-content: space-between;
                        font-size: 26rpx;
                        color: #666;
                        margin-bottom: 10rpx;

                        .goods-total {
                            color: #38C148;
                        }

                        .discount {
                            color: #38C148;
                        }
                    }

                    .real-pay-row {
                        display: flex;
                        justify-content: space-between;
                        font-size: 30rpx;
                        font-weight: 500;
                        margin-top: 20rpx;

                        .real-price {
                            color: #38C148;
                        }
                    }
                }
            }

            &.time-card {
                display: block;

                .row {
                    display: flex;
                    justify-content: space-between;
                    font-size: 24rpx;
                    color: #999;
                    margin-bottom: 10rpx;

                    &:last-child {
                        margin-bottom: 0;
                    }
                }
            }
        }
    }
</style>
