import { IconSymbol } from "@/components/ui/icon-symbol"; import { useTrip } from "@/state/use-trip"; import React, { useEffect, useRef, useState } from "react"; import { Animated, Text, TouchableOpacity, View } from "react-native"; import TripCostDetailModal from "./modal/TripCostDetailModal"; import styles from "./style/TripCostTable.styles"; // --------------------------- // 💰 Component chính // --------------------------- const TripCostTable: React.FC = () => { const [collapsed, setCollapsed] = useState(true); const [contentHeight, setContentHeight] = useState(0); const [modalVisible, setModalVisible] = useState(false); const animatedHeight = useRef(new Animated.Value(0)).current; const { trip } = useTrip(); const data: Model.TripCost[] = trip?.trip_cost ?? []; const tongCong = data.reduce((sum, item) => sum + item.total_cost, 0); // Reset animated height khi dữ liệu thay đổi useEffect(() => { // setContentHeight(0); // Reset để tính lại chiều cao setCollapsed(true); // Reset về trạng thái gập lại }, [data]); const handleToggle = () => { const toValue = collapsed ? contentHeight : 0; Animated.timing(animatedHeight, { toValue, duration: 300, useNativeDriver: false, }).start(); setCollapsed((prev) => !prev); }; const handleViewDetail = () => { setModalVisible(true); }; const handleCloseModal = () => { setModalVisible(false); }; return ( Chi phí chuyến đi {collapsed && ( {tongCong.toLocaleString()} )} {/* Nội dung ẩn để đo chiều cao */} { const height = event.nativeEvent.layout.height; if (height > 0 && contentHeight === 0) { setContentHeight(height); } }} > {/* Header */} Loại Tổng chi phí {/* Body */} {data.map((item, index) => ( {item.type} {item.total_cost.toLocaleString()} ))} {/* Footer */} Tổng cộng {tongCong.toLocaleString()} {/* View Detail Button */} {data.length > 0 && ( Xem chi tiết )} {/* Header */} Loại Tổng chi phí {/* Body */} {data.map((item, index) => ( {item.type} {item.total_cost.toLocaleString()} ))} {/* Footer */} Tổng cộng {tongCong.toLocaleString()} {/* View Detail Button */} {data.length > 0 && ( Xem chi tiết )} {/* Modal */} ); }; export default TripCostTable;