import { useLocalSearchParams, useRouter } from 'expo-router'; import React, { useCallback, useEffect, useRef, useState } from 'react'; import { ActivityIndicator, Alert, Dimensions, ImageBackground, ScrollView, StatusBar, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import { RecordModal, RecordModalRef } from '@/app/award-detail/components/RecordModal'; import { BoxSelectionModal, BoxSelectionModalRef } from '@/components/award-detail-yfs/BoxSelectionModal'; import FirstLast from '@/components/award-detail-yfs/FirstLast'; import { NumSelectionModal, NumSelectionModalRef } from '@/components/award-detail-yfs/NumSelectionModal'; import ProductListYfs from '@/components/award-detail-yfs/ProductListYfs'; import ProductSwiper from '@/components/award-detail-yfs/ProductSwiper'; import PurchaseBar from '@/components/award-detail-yfs/PurchaseBar'; import { RuleModal, RuleModalRef } from '@/components/award-detail-yfs/RuleModal'; import { Images } from '@/constants/images'; import { getBoxDetail, getNextBox, getPoolDetail, getPreBox, poolIn, poolOut } from '@/services/award'; import { CheckoutModal, CheckoutModalRef } from '../award-detail/components/CheckoutModal'; const { width } = Dimensions.get('window'); const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#1a1a2e' }, background: { flex: 1 }, loading: { flex: 1, backgroundColor: '#1a1a2e', justifyContent: 'center', alignItems: 'center' }, nav: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', paddingHorizontal: 15, height: 90, zIndex: 100, position: 'absolute', top: 0, left: 0, right: 0, }, backBtn: { width: 40 }, backText: { color: '#fff', fontSize: 24 }, navTitle: { color: '#fff', fontSize: 16, fontWeight: 'bold', maxWidth: '60%' }, placeholder: { width: 40 }, scrollView: { flex: 1 }, detailWrapper: { position: 'relative', minHeight: 800, paddingBottom: 100, }, mainGoodsSection: { width: '100%', height: 504, position: 'relative', zIndex: 1, }, mainGoodsSectionBtext: { width: '100%', height: 74, marginTop: -10, zIndex: 2, }, positionBgLeftBg: { position: 'absolute', left: 0, top: 225, width: 32, height: 188, zIndex: 2, }, positionBgRightBg: { position: 'absolute', right: 0, top: 225, width: 32, height: 188, zIndex: 2, }, positionBut: { position: 'absolute', zIndex: 10, width: 35, height: 34, }, btnBg: { width: '100%', height: '100%', justifyContent: 'center', alignItems: 'center', }, slantedL: { color: '#fff', fontSize: 12, fontWeight: 'bold', transform: [{ rotate: '14deg' }], marginTop: -2, textShadowColor: '#000', textShadowOffset: { width: 1, height: 1 }, textShadowRadius: 1, }, slantedR: { color: '#fff', fontSize: 12, fontWeight: 'bold', transform: [{ rotate: '-16deg' }], marginTop: -2, textShadowColor: '#000', textShadowOffset: { width: 1, height: 1 }, textShadowRadius: 1, }, positionRule: { top: 256, left: 0, }, positionStore: { top: 256, right: 0, }, positionRecord: { top: 300, left: 0, }, positionRefresh: { top: 345, right: 0, }, bottomBar: { position: 'absolute', bottom: 0, left: 0, right: 0, zIndex: 200, } }); export default function AwardDetailYfsScreen() { const { poolId } = useLocalSearchParams<{ poolId: string }>(); const router = useRouter(); const insets = useSafeAreaInsets(); const [loading, setLoading] = useState(true); const [data, setData] = useState(null); const [scrollTop, setScrollTop] = useState(0); const [currentBox, setCurrentBox] = useState(null); const boxRef = useRef(null); const numRef = useRef(null); const checkoutRef = useRef(null); const ruleRef = useRef(null); const recordRef = useRef(null); const getSafePoolId = () => Array.isArray(poolId) ? poolId[0] : poolId; const loadBox = async (boxNum?: string) => { try { const id = getSafePoolId(); const res = await getBoxDetail(id, boxNum); if (res) setCurrentBox(res); } catch (error) { console.error('Failed to load box', error); } }; const loadData = useCallback(async () => { if (!poolId) return; setLoading(true); try { const safePoolId = getSafePoolId(); const res = await getPoolDetail(safePoolId); setData(res); await loadBox(); // Initial box } catch (error) { console.error('Failed to load detail:', error); } setLoading(false); }, [poolId]); useEffect(() => { if (poolId) { poolIn(poolId); return () => { poolOut(poolId); }; } }, [poolId]); const handleBoxSelect = (box: any) => { setCurrentBox(box); }; const handlePrevBox = async () => { if (!currentBox) return; try { const res = await getPreBox(getSafePoolId(), currentBox.number); if (res) setCurrentBox(res); else Alert.alert('提示', '已经是第一盒了'); } catch (e) { Alert.alert('提示', '切换失败'); } }; const handleNextBox = async () => { if (!currentBox) return; try { const res = await getNextBox(getSafePoolId(), currentBox.number); if (res) setCurrentBox(res); else Alert.alert('提示', '已经是最后一盒了'); } catch (e) { Alert.alert('提示', '切换失败'); } }; const handlePay = ({ previewRes, chooseNum, boxNum }: any) => { checkoutRef.current?.show(chooseNum.length, previewRes, boxNum, chooseNum); }; useEffect(() => { loadData(); }, [loadData]); const headerBg = scrollTop > 50 ? '#333' : 'transparent'; if (loading) { return ( ); } return ( {/* Navigation Bar */} router.back()} style={styles.backBtn}> {data?.name || '详情'} setScrollTop(e.nativeEvent.contentOffset.y)} scrollEventThrottle={16} showsVerticalScrollIndicator={false} > {/* Main Goods Section */} { let prob = '0%'; const total = data.leftQuantity || 1; if (item.level === 'A') prob = ((data.leftQuantityA / total) * 100).toFixed(4) + '%'; else if (item.level === 'B') prob = ((data.leftQuantityB / total) * 100).toFixed(4) + '%'; else if (item.level === 'C') prob = ((data.leftQuantityC / total) * 100).toFixed(4) + '%'; else if (item.level === 'D') prob = ((data.leftQuantityD / total) * 100).toFixed(4) + '%'; return { cover: item.spu.cover, name: item.spu.name, level: item.level, probability: prob, quantity: item.quantity }; }) || []} /> {/* Bottom Text Graphic */} {/* FirstLast (Box Info & Nav) */} boxRef.current?.show()} onProductClick={(spu) => console.log(spu)} /> {/* Product List (A/B/C/D) */} {/* Floating Buttons */} ruleRef.current?.show()}> 规则 recordRef.current?.show()}> 记录 router.push('/store')}> 仓库 loadBox(currentBox?.number)}> 刷新 { if (!currentBox) { boxRef.current?.show(); } else { numRef.current?.show(currentBox); } }} onBuyMany={() => { if (!currentBox) { boxRef.current?.show(); } else { numRef.current?.show(currentBox); } }} /> {/* Modals */} { console.log('Success', res); loadData(); // Refresh }} /> ); }