import { Image } from 'expo-image'; import React, { useState } from 'react'; import { Dimensions, FlatList, ImageBackground, StyleSheet, Text, TouchableOpacity, View, } from 'react-native'; import { Images } from '@/constants/images'; const { width } = Dimensions.get('window'); interface Product { cover: string; name: string; level: string; quantity?: number; probability?: string; } interface ProductSwiperProps { products: Product[]; box?: any; // Add box prop onShowSwipe?: (index: number) => void; } export default function ProductSwiper({ products, box, onShowSwipe }: ProductSwiperProps) { const [current, setCurrent] = useState(0); const getLeftNum = (item: any) => { const usedStat = box?.usedStat || box?.used_stat; if (!box || !usedStat) return item.quantity; const spuId = String(item.spu?.id || item.spu_id || item.id); // Try to match ID const itemId = String(item.id); let used: any = null; if (Array.isArray(usedStat)) { used = usedStat.find((u: any) => { const uId = String(u.spuId || u.spu_id || u.id); return uId === spuId || uId === itemId; }); } else { used = usedStat[spuId] || usedStat[itemId]; } if (used) { return item.quantity - (used.quantity || 0); } return item.quantity; }; const getProbability = (item: Product) => { if (!box || !box.leftQuantity) return item.probability || '0%'; const left = getLeftNum(item); const prob = (left / box.leftQuantity * 100).toFixed(4); return parseFloat(prob) === 0 ? '0%' : `${prob}%`; }; if (!products || products.length === 0) return null; const renderItem = ({ item, index }: { item: Product; index: number }) => ( onShowSwipe?.(index)} style={styles.itemContainer} > {/* Details Button/Tag */} {item.level === 'A' ? '超神款' : item.level === 'B' ? '欧皇款' : item.level === 'C' ? '隐藏款' : '普通款'} {getProbability(item)} {/* Name Tag */} {item.name} ); return ( index.toString()} onMomentumScrollEnd={(e) => { const contentOffset = e.nativeEvent.contentOffset; const viewSize = e.nativeEvent.layoutMeasurement; const pageNum = Math.floor(contentOffset.x / viewSize.width); setCurrent(pageNum); }} /> {/* Left/Right Arrows if needed */} {current > 0 && ( )} {current < products.length - 1 && ( )} ); } const styles = StyleSheet.create({ container: { width: '100%', height: 380, // Adjusted height alignItems: 'center', marginTop: 20, }, itemContainer: { width: width, // Full width for paging height: '100%', justifyContent: 'center', alignItems: 'center', }, imageBox: { width: '60%', height: '60%', justifyContent: 'center', alignItems: 'center', }, image: { width: '100%', height: '100%', }, detailsTag: { position: 'absolute', right: 40, bottom: 80, width: 100, height: 60, justifyContent: 'center', alignItems: 'center', }, detailsContent: { alignItems: 'center', justifyContent: 'center', marginTop: 5, }, levelText: { color: '#fff', fontSize: 12, fontWeight: 'bold', textShadowColor: '#000', textShadowOffset: { width: 1, height: 1 }, textShadowRadius: 1, }, probText: { color: '#fff', fontSize: 10, fontWeight: 'bold', textShadowColor: '#000', textShadowOffset: { width: 1, height: 1 }, textShadowRadius: 1, }, nameTag: { position: 'absolute', left: 40, top: 40, width: 40, height: 120, justifyContent: 'center', alignItems: 'center', paddingVertical: 10, }, nameText: { color: '#000', fontSize: 12, fontWeight: 'bold', width: 12, textAlign: 'center', }, arrow: { position: 'absolute', top: '50%', marginTop: -20, width: 40, height: 40, zIndex: 10, }, leftArrow: { left: 10, }, rightArrow: { right: 10, }, arrowImg: { width: '100%', height: '100%', } });