import { RootState } from "@/store"; import React from "react"; import { useTranslation } from "react-i18next"; import { View, Text, StyleSheet, ScrollView } from "react-native"; import { useSelector } from "react-redux"; const BatteryDetails = () => { const { data } = useSelector((state: RootState) => state.user); const { t } = useTranslation(); const model = data?.batteries[0]?.battery_model ?? "---"; const batteryId = data?.batteries[0]?.battery_id ?? "---"; const bmsId = data?.batteries[0]?.bms_id ?? "---"; const warrantyStartDate = data?.batteries[0]?.warranty_start_date ?? "---"; const warrantyEndDate = data?.batteries[0]?.warranty_end_date ?? "---"; const vimId = data?.batteries[0]?.vim_id ?? "---"; const serialNumber = data?.batteries[0]?.serial_no ?? "---"; const chargerUid = data?.batteries[0]?.charger_uid ?? "---"; const battery = data?.batteries?.[0]; const start = battery?.warranty_start_date ? new Date(battery.warranty_start_date) : null; const end = battery?.warranty_end_date ? new Date(battery.warranty_end_date) : null; const now = new Date(); let progress = 0; let durationText = "---"; if (start && end && !isNaN(start.getTime()) && !isNaN(end.getTime())) { const totalDuration = end.getTime() - start.getTime(); const elapsed = now.getTime() - start.getTime(); const remaining = Math.max(end.getTime() - now.getTime(), 0); progress = Math.min(elapsed / totalDuration, 1); const yearsLeft = Math.floor(remaining / (365.25 * 24 * 60 * 60 * 1000)); const monthsLeft = Math.floor( (remaining % (365.25 * 24 * 60 * 60 * 1000)) / (30.44 * 24 * 60 * 60 * 1000) ); const daysLeft = Math.floor( (remaining % (30.44 * 24 * 60 * 60 * 1000)) / (24 * 60 * 60 * 1000) ); durationText = (() => { const parts: string[] = []; if (yearsLeft > 0) { parts.push(`${yearsLeft} ${t("home.year")}`); } if (monthsLeft > 0) { parts.push(`${monthsLeft} ${t("home.month")}`); } if (daysLeft > 0 || parts.length === 0) { parts.push(`${daysLeft} ${t("home.day")}`); } console.log(parts, "parts"); return parts.join(", "); })(); } const formatDate = (date?: Date | null): string => date && !isNaN(date.getTime()) ? date.toLocaleDateString("en-GB", { day: "2-digit", month: "short", year: "numeric", }) : "---"; const isInWarranty = start && end && now >= start && now <= end && !isNaN(start.getTime()) && !isNaN(end.getTime()); return ( {/* Battery Section */} {t("battery.battery")} {isInWarranty ? ( {t("battery.in-warranty")} ) : ( {t("battery.warranty-expired")} )} {/* Battery Warranty Details Section */} {t("battery.battery-warranty-details")} {start && end && !isNaN(start.getTime()) && !isNaN(end.getTime()) && ( )} {/* VIM Details Section */} {t("battery.vim-details")} {/* Charger Details Section */} {t("battery.charger-details")} ); }; type InfoRowProps = { label: string; value: string; }; const InfoRow: React.FC = ({ label, value }) => ( {label} {value} ); export default BatteryDetails; const styles = StyleSheet.create({ expiredBadgeText: { fontSize: 12, fontWeight: "500", color: "#D51D10", }, container: { backgroundColor: "#F3F5F8", paddingHorizontal: 16, }, card: { backgroundColor: "#FCFCFC", borderRadius: 8, padding: 12, marginBottom: 16, }, cardHeader: { flexDirection: "row", justifyContent: "space-between", alignItems: "center", }, cardTitle: { fontSize: 14, fontWeight: "600", color: "#252A34", }, expiredBadge: { backgroundColor: "#FDE9E7", paddingVertical: 2, paddingHorizontal: 8, borderRadius: 4, }, badge: { backgroundColor: "#DAF5ED", paddingVertical: 2, paddingHorizontal: 8, borderRadius: 4, }, badgeText: { fontSize: 12, fontWeight: "500", color: "#006C4D", }, divider: { height: 1, backgroundColor: "#e5e9f0", marginVertical: 12, }, infoRow: { flexDirection: "row", justifyContent: "space-between", paddingVertical: 4, }, label: { fontSize: 14, color: "#252A34", }, value: { fontSize: 14, fontWeight: "600", color: "#252A34", }, progressBarBackground: { height: 8, backgroundColor: "#E5E9F0", borderRadius: 10, overflow: "hidden", marginTop: 16, }, progressBarFill: { height: 8, backgroundColor: "#00825B", borderRadius: 10, }, });