<template>
    <view class="main">
        <scroll-view class="pageBody" scroll-y>
            <!-- 商品大图 -->
            <view class="swiperBox">
                <swiper class="swiper" circular indicator-dots indicator-active-color="#4BCB7E">
                    <swiper-item v-for="(img, index) in swiperImgs" :key="index">
                        <image class="swiperImg" :src="img" mode="aspectFit" @click="handlePreview(index)" />
                    </swiper-item>
                </swiper>
            </view>

            <!-- 核心信息区 -->
            <view class="infoSection">
                <view class="priceBox">
                    <text class="unit">¥</text>
                    <text class="price">{{ product.COMMODITY_MEMBERPRICE }}</text>
                    <text class="commodityUnit">/{{ product.COMMODITY_UNIT }}</text>
                </view>
                <view class="productName">{{ product.COMMODITY_NAME }}</view>
            </view>

            <!-- 参数规格区 -->
            <view class="detailSection">
                <view class="detailItem">
                    <text class="label">规格</text>
                    <text class="value">{{ product.COMMODITY_RULE || '暂无规格' }}</text>
                </view>
                <view class="detailItem">
                    <text class="label">编号</text>
                    <text class="value">{{ product.COMMODITY_CODE || '-' }}</text>
                </view>
                <view class="detailItem">
                    <text class="label">仓库</text>
                    <text class="value">{{ product.WAREHOUSE_NAME || '-' }}</text>
                </view>
                <view class="detailItem">
                    <text class="label">分类</text>
                    <text class="value">{{ product.TYPE_NAME || '-' }}</text>
                </view>
            </view>

            <view style="height: 170rpx;"></view>
        </scroll-view>

        <!-- 底部操作栏 (极简设计) -->
        <view class="bottomBar">
            <view class="addBtn" @click="addToCart">加入购物车</view>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            menu: {},
            product: {},
            swiperImgs: []
        };
    },
    onLoad(options) {
        this.menu = uni.getMenuButtonBoundingClientRect();
        if (options.data) {
            try {
                const item = JSON.parse(decodeURIComponent(options.data));
                this.product = item;
                console.log('this.productthis.productthis.product', this.product);

                // 处理图片路径
                if (item.IMAGE_PATH) {
                    this.swiperImgs = item.IMAGE_PATH.split(',').filter(i => i);
                } else {
                    this.swiperImgs = ['/static/images/home/defultImg.png'];
                }
            } catch (e) {
                console.error('Parse product error', e);
            }
        }
    },
    methods: {
        handleBack() {
            uni.navigateBack();
        },
        handlePreview(index) {
            uni.previewImage({
                current: index,
                urls: this.swiperImgs
            });
        },
        addToCart() {
            // 适配自有水购物车的本地缓存逻辑
            let cart = uni.getStorageSync("waterShopCar") || [];
            const index = cart.findIndex(i => i.SELLERCOMMODITY_ID === this.product.SELLERCOMMODITY_ID);

            if (index > -1) {
                cart[index].count = (cart[index].count || 1) + 1;
            } else {
                // 保持 waterShopCar.vue 依赖的字段格式
                // 注意：列表页已经做了字段映射，但详情页接收的是原始字段，
                // 这里需要确保存入购物车的值与 waterShopCar 渲染逻辑一致。
                const newItem = {
                    ...this.product,
                    // 确保映射字段存在
                    COMMODITY_MEMBERPRICE: this.product.COSTTAXPRICE,
                    COMMODITY_ID: this.product.SELLERCOMMODITY_ID,
                    count: 1,
                    select: true
                };
                cart.push(newItem);
            }

            uni.setStorageSync("waterShopCar", cart);
            uni.showToast({ title: "已加入购物车", icon: "success" });
        }
    }
};
</script>

<style lang="less" scoped>
.main {
    background: #f0f2f5;
    min-height: 100vh;
    position: relative;
}

.navHeader {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    background: #fff;
    z-index: 100;
    border-bottom: 1rpx solid rgba(0, 0, 0, 0.05);

    .navContent {
        display: flex;
        align-items: center;
        padding: 0 30rpx;

        .backBtn {
            width: 60rpx;
            display: flex;
            align-items: center;

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

        .navTitle {
            flex: 1;
            text-align: center;
            font-size: 32rpx;
            font-weight: bold;
            color: #333;
        }

        .placeholder {
            width: 60rpx;
        }
    }
}

.pageBody {
    height: 100vh;
    box-sizing: border-box;
}

.swiperBox {
    width: 100%;
    height: 600rpx;
    background: linear-gradient(160deg, #f8faf9, #e8f0ec);
    padding: 20rpx;
    box-sizing: border-box;

    .swiper {
        width: 100%;
        height: 100%;

        .swiperImg {
            width: 100%;
            height: 100%;
            border-radius: 16rpx;
        }
    }
}

.infoSection {
    background: #fff;
    padding: 32rpx 30rpx;
    margin: 20rpx;
    border-radius: 20rpx;
    box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.04);

    .priceBox {
        display: flex;
        align-items: baseline;
        margin-bottom: 20rpx;

        .unit {
            font-size: 28rpx;
            font-weight: bold;
            color: #ff4d4f;
        }

        .price {
            font-size: 52rpx;
            font-weight: bold;
            margin: 0 4rpx;
            color: #ff4d4f;
        }

        .commodityUnit {
            font-size: 24rpx;
            color: #999;
            margin-left: 4rpx;
        }
    }

    .productName {
        font-size: 34rpx;
        font-weight: bold;
        color: #222;
        line-height: 1.5;
    }
}

.detailSection {
    background: #fff;
    padding: 10rpx 30rpx;
    margin: 0 20rpx 20rpx;
    border-radius: 20rpx;
    box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.04);

    .detailItem {
        display: flex;
        padding: 28rpx 0;
        border-bottom: 1rpx solid #f5f5f5;
        font-size: 28rpx;

        &:last-child {
            border-bottom: none;
        }

        .label {
            width: 140rpx;
            color: #999;
            flex-shrink: 0;
        }

        .value {
            flex: 1;
            color: #333;
            font-weight: 500;
        }
    }
}

.bottomBar {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 170rpx;
    background: #fff;
    padding: 20rpx 30rpx calc(20rpx + env(safe-area-inset-bottom));
    box-sizing: border-box;
    display: flex;
    align-items: center;
    box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.06);
    z-index: 100;

    .addBtn {
        flex: 1;
        height: 88rpx;
        background: linear-gradient(135deg, #4BCB7E, #36b068);
        color: #fff;
        border-radius: 44rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 32rpx;
        font-weight: bold;
        letter-spacing: 4rpx;
        box-shadow: 0 8rpx 20rpx rgba(75, 203, 126, 0.3);
    }
}
</style>
