thêm giao diện quản lý thuyền

This commit is contained in:
Tran Anh Tuan
2025-12-10 19:49:54 +07:00
parent df4318fed4
commit 3e1c4dcbc5
24 changed files with 2091 additions and 135 deletions

View File

@@ -0,0 +1,847 @@
import Select, { SelectOption } from "@/components/Select";
import { ThemedText } from "@/components/themed-text";
import { Colors } from "@/config";
import { ColorScheme, useTheme } from "@/hooks/use-theme-context";
import { usePort } from "@/state/use-ports";
import { useShipGroups } from "@/state/use-ship-groups";
import { useShipTypes } from "@/state/use-ship-types";
import { useThings } from "@/state/use-thing";
import DateTimePicker from "@react-native-community/datetimepicker";
import { useEffect, useMemo, useState } from "react";
import { Controller, useForm } from "react-hook-form";
import {
KeyboardAvoidingView,
Modal,
Platform,
Pressable,
ScrollView,
StyleSheet,
TextInput,
TouchableOpacity,
View,
} from "react-native";
import { SafeAreaView } from "react-native-safe-area-context";
interface CreateOrUpdateShipProps {
initialValue?: Model.ShipBodyRequest;
isOpen?: boolean;
type?: "create" | "update";
onSubmit?: (data: Model.ShipBodyRequest) => void;
onClose?: () => void;
}
const CreateOrUpdateShip = (props: CreateOrUpdateShipProps) => {
const { colors, colorScheme } = useTheme();
const styles = useMemo(
() => createStyles(colors, colorScheme),
[colors, colorScheme]
);
const { shipTypes, getShipTypes } = useShipTypes();
const { ports, getPorts } = usePort();
const { shipGroups, getShipGroups } = useShipGroups();
const { things, getThings } = useThings();
// State for date picker
const [showDatePicker, setShowDatePicker] = useState(false);
// Initialize form with react-hook-form
const {
control,
handleSubmit,
formState: { errors },
setValue,
watch,
reset,
} = useForm<Model.ShipBodyRequest>({
defaultValues: props.initialValue || {
fishing_license_expiry_date: new Date(),
},
});
// Watch the date field for picker display
const dateValue = watch("fishing_license_expiry_date");
// Fetch data when modal opens
useEffect(() => {
if (props.isOpen) {
// Fetch ship types if not loaded
if (shipTypes === null || shipTypes.length === 0) {
getShipTypes();
}
// Fetch ports if not loaded
if (ports === null) {
getPorts();
}
// Fetch ship groups if not loaded
if (shipGroups === null) {
getShipGroups();
}
// Fetch things when modal opens
const payloadThings: Model.SearchThingBody = {
offset: 0,
limit: 200,
order: "name",
dir: "asc",
};
getThings(payloadThings);
// Reset form with initial values if provided
if (props.initialValue) {
reset(props.initialValue);
}
}
}, [props.isOpen, props.initialValue, reset]);
// Prepare options for selects
const shipTypeOptions = useMemo<SelectOption[]>(() => {
return (shipTypes || []).map((type) => ({
label: type.name || "",
value: type.id || 0,
}));
}, [shipTypes]);
const portOptions = useMemo<SelectOption[]>(() => {
return (ports?.ports || []).map((port) => ({
label: port.name || "",
value: port.id || 0,
}));
}, [ports]);
const shipGroupOptions = useMemo<SelectOption[]>(() => {
return (shipGroups || []).map((group) => ({
label: group.name || "",
value: group.id || "",
}));
}, [shipGroups]);
const thingOptions = useMemo<SelectOption[]>(() => {
// Filter things that are not assigned to any ship
const unassignedThings = (things || []).filter(
(thing) => !thing.metadata?.ship_id
);
return unassignedThings.map((thing) => ({
label: thing.name || "",
value: thing.id || "",
}));
}, [things]);
// Handle date picker change
const handleDateChange = (_: any, selectedDate?: Date) => {
if (selectedDate) {
setValue("fishing_license_expiry_date", selectedDate);
}
// On Android, close picker after selection
// On iOS, keep it open until user confirms with the button
if (Platform.OS === "android") {
setShowDatePicker(false);
}
};
// Format date for display
const formatDateForDisplay = (date: Date | string | undefined) => {
if (!date) return "";
const d = typeof date === "string" ? new Date(date) : date;
return d.toLocaleDateString("vi-VN", {
day: "2-digit",
month: "2-digit",
year: "numeric",
});
};
// Handle form submission
const onSubmit = (data: Model.ShipBodyRequest) => {
// Ensure numeric fields are numbers
const payload: Model.ShipBodyRequest = {
...data,
ship_type: Number(data.ship_type),
home_port: Number(data.home_port),
ship_length: Number(data.ship_length),
ship_power: Number(data.ship_power),
fishing_license_expiry_date: data.fishing_license_expiry_date,
};
props.onSubmit?.(payload);
};
return (
<Modal
animationType="slide"
transparent={true}
visible={props.isOpen}
onRequestClose={props.onClose}
>
<SafeAreaView style={{ flex: 1 }} edges={["top", "left", "right"]}>
<View style={styles.container}>
<Pressable style={styles.backdrop} onPress={props.onClose} />
<KeyboardAvoidingView
behavior={Platform.OS === "ios" ? "padding" : "height"}
style={styles.keyboardAvoidingView}
>
<View style={styles.modalContent}>
{/* Header */}
<View style={styles.header}>
<View style={styles.dragIndicator} />
<ThemedText style={styles.headerTitle}>
{props.type === "create" ? "Thêm tàu mới" : "Cập nhật tàu"}
</ThemedText>
<TouchableOpacity
onPress={props.onClose}
style={styles.closeButton}
>
<ThemedText style={styles.closeButtonText}></ThemedText>
</TouchableOpacity>
</View>
{/* Form Content */}
<ScrollView
style={styles.scrollView}
contentContainerStyle={styles.scrollContent}
showsVerticalScrollIndicator={false}
keyboardShouldPersistTaps="handled"
>
{/* Registration Number - Only show in create mode */}
{props.type === "create" && (
<View style={styles.fieldGroup}>
<ThemedText style={styles.label}>Số đăng *</ThemedText>
<Controller
control={control}
name="reg_number"
rules={{ required: "Vui lòng nhập số đăng ký" }}
render={({ field: { onChange, onBlur, value } }) => (
<TextInput
style={[
styles.input,
{
borderColor: errors.reg_number
? "red"
: colors.border,
},
]}
placeholder="Nhập số đăng ký"
onBlur={onBlur}
onChangeText={(text) => onChange(text.trim())}
value={value}
placeholderTextColor={colors.textSecondary}
/>
)}
/>
{errors.reg_number && (
<ThemedText style={styles.errorText}>
{errors.reg_number.message}
</ThemedText>
)}
</View>
)}
{/* Ship Name */}
<View style={styles.fieldGroup}>
<ThemedText style={styles.label}>Tên tàu *</ThemedText>
<Controller
control={control}
name="name"
rules={{ required: "Vui lòng nhập tên tàu" }}
render={({ field: { onChange, onBlur, value } }) => (
<TextInput
style={[
styles.input,
{ borderColor: errors.name ? "red" : colors.border },
]}
placeholder="Nhập tên tàu"
onBlur={onBlur}
onChangeText={onChange}
value={value}
placeholderTextColor={colors.textSecondary}
/>
)}
/>
{errors.name && (
<ThemedText style={styles.errorText}>
{errors.name.message}
</ThemedText>
)}
</View>
{/* Ship Type */}
<View style={styles.fieldGroup}>
<ThemedText style={styles.label}>Loại tàu *</ThemedText>
<Controller
control={control}
name="ship_type"
rules={{ required: "Vui lòng chọn loại tàu" }}
render={({ field: { onChange, value } }) => (
<Select
value={value}
onChange={onChange}
options={shipTypeOptions}
placeholder="Chọn loại tàu"
style={[
styles.selectInput,
{
borderColor: errors.ship_type
? "red"
: colors.border,
},
]}
/>
)}
/>
{errors.ship_type && (
<ThemedText style={styles.errorText}>
{errors.ship_type.message}
</ThemedText>
)}
</View>
{/* Home Port */}
<View style={styles.fieldGroup}>
<ThemedText style={styles.label}>
Cảng đăng đ *
</ThemedText>
<Controller
control={control}
name="home_port"
rules={{ required: "Vui lòng chọn cảng đăng ký" }}
render={({ field: { onChange, value } }) => (
<Select
value={value}
onChange={onChange}
options={portOptions}
placeholder="Chọn cảng đăng ký"
style={[
styles.selectInput,
{
borderColor: errors.home_port
? "red"
: colors.border,
},
]}
/>
)}
/>
{errors.home_port && (
<ThemedText style={styles.errorText}>
{errors.home_port.message}
</ThemedText>
)}
</View>
{/* Fishing License Number */}
<View style={styles.fieldGroup}>
<ThemedText style={styles.label}>Số giấy phép *</ThemedText>
<Controller
control={control}
name="fishing_license_number"
rules={{ required: "Vui lòng nhập số giấy phép" }}
render={({ field: { onChange, onBlur, value } }) => (
<TextInput
style={[
styles.input,
{
borderColor: errors.fishing_license_number
? "red"
: colors.border,
},
]}
placeholder="Nhập số giấy phép"
onBlur={onBlur}
onChangeText={onChange}
value={value}
placeholderTextColor={colors.textSecondary}
/>
)}
/>
{errors.fishing_license_number && (
<ThemedText style={styles.errorText}>
{errors.fishing_license_number.message}
</ThemedText>
)}
</View>
{/* Fishing License Expiry Date */}
<View style={styles.fieldGroup}>
<ThemedText style={styles.label}>Ngày hết hạn *</ThemedText>
<Controller
control={control}
name="fishing_license_expiry_date"
rules={{
required: "Vui lòng chọn ngày hết hạn",
validate: (date) => {
if (!date) return "Vui lòng chọn ngày hết hạn";
const selectedDate = new Date(date);
const today = new Date();
today.setHours(0, 0, 0, 0);
if (selectedDate < today) {
return "Ngày hết hạn không thể là ngày trong quá khứ";
}
return true;
},
}}
render={({ field: { onChange, value } }) => (
<TouchableOpacity
onPress={() => setShowDatePicker(true)}
style={[
styles.input,
styles.dateInput,
{
borderColor: errors.fishing_license_expiry_date
? "red"
: colors.border,
},
]}
>
<ThemedText
style={{
color: value ? colors.text : colors.textSecondary,
}}
>
{formatDateForDisplay(value) || "Chọn ngày hết hạn"}
</ThemedText>
</TouchableOpacity>
)}
/>
{errors.fishing_license_expiry_date && (
<ThemedText style={styles.errorText}>
{errors.fishing_license_expiry_date.message}
</ThemedText>
)}
</View>
{/* Ship Length */}
<View style={styles.fieldGroup}>
<ThemedText style={styles.label}>Chiều dài (m) *</ThemedText>
<Controller
control={control}
name="ship_length"
rules={{
required: "Vui lòng nhập chiều dài",
pattern: {
value: /^\d*\.?\d+$/,
message: "Vui lòng nhập số hợp lệ",
},
validate: (value) => {
const num = Number(value);
if (isNaN(num) || num <= 0) {
return "Chiều dài phải lớn hơn 0";
}
return true;
},
}}
render={({ field: { onChange, onBlur, value } }) => (
<TextInput
style={[
styles.input,
{
borderColor: errors.ship_length
? "red"
: colors.border,
},
]}
placeholder="Nhập chiều dài tàu"
onBlur={onBlur}
onChangeText={onChange}
value={value?.toString()}
keyboardType="numeric"
placeholderTextColor={colors.textSecondary}
/>
)}
/>
{errors.ship_length && (
<ThemedText style={styles.errorText}>
{errors.ship_length.message}
</ThemedText>
)}
</View>
{/* Ship Power */}
<View style={styles.fieldGroup}>
<ThemedText style={styles.label}>
Công suất ( lực) *
</ThemedText>
<Controller
control={control}
name="ship_power"
rules={{
required: "Vui lòng nhập công suất",
pattern: {
value: /^\d*\.?\d+$/,
message: "Vui lòng nhập số hợp lệ",
},
validate: (value) => {
const num = Number(value);
if (isNaN(num) || num <= 0) {
return "Công suất phải lớn hơn 0";
}
return true;
},
}}
render={({ field: { onChange, onBlur, value } }) => (
<TextInput
style={[
styles.input,
{
borderColor: errors.ship_power
? "red"
: colors.border,
},
]}
placeholder="Nhập công suất tàu"
onBlur={onBlur}
onChangeText={onChange}
value={value?.toString()}
keyboardType="numeric"
placeholderTextColor={colors.textSecondary}
/>
)}
/>
{errors.ship_power && (
<ThemedText style={styles.errorText}>
{errors.ship_power.message}
</ThemedText>
)}
</View>
{/* Ship Group - Only show in update mode */}
{props.type === "update" && (
<View style={styles.fieldGroup}>
<ThemedText style={styles.label}>Đi tàu</ThemedText>
<Controller
control={control}
name="ship_group_id"
render={({ field: { onChange, value } }) => (
<Select
value={value}
onChange={onChange}
options={shipGroupOptions}
placeholder="Chọn đội tàu"
style={styles.selectInput}
/>
)}
/>
</View>
)}
{/* Device/Thing - Only show in create mode */}
{props.type === "create" && (
<View style={styles.fieldGroup}>
<ThemedText style={styles.label}>
Thiết bị kết nối *
</ThemedText>
<Controller
control={control}
name="thing_id"
rules={{ required: "Vui lòng chọn thiết bị kết nối" }}
render={({ field: { onChange, value } }) => (
<Select
value={value}
onChange={onChange}
options={thingOptions}
placeholder="Chọn thiết bị kết nối"
style={[
styles.selectInput,
{
borderColor: errors.thing_id
? "red"
: colors.border,
},
]}
/>
)}
/>
{errors.thing_id && (
<ThemedText style={styles.errorText}>
{errors.thing_id.message}
</ThemedText>
)}
</View>
)}
</ScrollView>
{/* Action Buttons */}
<View style={styles.actionButtons}>
<TouchableOpacity
style={[styles.resetButton, { borderColor: colors.border }]}
onPress={() => {
reset(props.initialValue || {});
}}
>
<ThemedText
style={[styles.resetButtonText, { color: colors.text }]}
>
Nhập lại
</ThemedText>
</TouchableOpacity>
<TouchableOpacity
style={[
styles.submitButton,
{ backgroundColor: colors.primary },
]}
onPress={handleSubmit(onSubmit)}
>
<ThemedText style={styles.submitButtonText}>
{props.type === "create" ? "Thêm tàu" : "Cập nhật"}
</ThemedText>
</TouchableOpacity>
</View>
</View>
</KeyboardAvoidingView>
</View>
</SafeAreaView>
{/* Date Picker Modal - Only show on Android as modal, iOS shows inline */}
{Platform.OS === "android" && showDatePicker && (
<DateTimePicker
value={
typeof dateValue === "string"
? new Date(dateValue)
: dateValue || new Date()
}
mode="date"
display="default"
onChange={handleDateChange}
minimumDate={new Date()}
/>
)}
{Platform.OS === "ios" && showDatePicker && (
<Modal
transparent={true}
animationType="fade"
visible={showDatePicker}
onRequestClose={() => setShowDatePicker(false)}
>
<SafeAreaView style={styles.datePickerModal}>
<View style={styles.datePickerContent}>
<View style={styles.datePickerHeader}>
<ThemedText style={styles.datePickerTitle}>
Chọn ngày hết hạn
</ThemedText>
<TouchableOpacity onPress={() => setShowDatePicker(false)}>
<ThemedText style={styles.datePickerClose}></ThemedText>
</TouchableOpacity>
</View>
<DateTimePicker
value={
typeof dateValue === "string"
? new Date(dateValue)
: dateValue || new Date()
}
mode="date"
display="spinner"
onChange={handleDateChange}
themeVariant={colorScheme}
textColor={colors.text}
minimumDate={new Date()}
style={styles.datePickerIOS}
/>
<TouchableOpacity
style={[
styles.datePickerButton,
{ backgroundColor: colors.primary },
]}
onPress={() => setShowDatePicker(false)}
>
<ThemedText style={styles.datePickerButtonText}>
Xác nhận
</ThemedText>
</TouchableOpacity>
</View>
</SafeAreaView>
</Modal>
)}
</Modal>
);
};
const createStyles = (colors: typeof Colors.light, scheme: ColorScheme) =>
StyleSheet.create({
container: {
flex: 1,
position: "relative",
},
keyboardAvoidingView: {
flex: 1,
justifyContent: "flex-end",
},
backdrop: {
position: "absolute",
top: 0,
left: 0,
right: 0,
bottom: 0,
backgroundColor: "rgba(0, 0, 0, 0.3)",
},
modalContent: {
height: "90%",
backgroundColor: colors.background,
borderTopLeftRadius: 24,
borderTopRightRadius: 24,
shadowColor: "#000",
shadowOffset: {
width: 0,
height: -4,
},
shadowOpacity: 0.25,
shadowRadius: 8,
elevation: 10,
},
header: {
flexDirection: "row",
alignItems: "center",
justifyContent: "center",
paddingVertical: 16,
paddingHorizontal: 20,
borderBottomWidth: 1,
borderBottomColor: colors.border,
position: "relative",
},
dragIndicator: {
position: "absolute",
top: 8,
width: 40,
height: 4,
backgroundColor: colors.border,
borderRadius: 2,
},
headerTitle: {
fontSize: 18,
fontWeight: "700",
textAlign: "center",
color: colors.text,
},
closeButton: {
position: "absolute",
right: 16,
top: 16,
width: 32,
height: 32,
alignItems: "center",
justifyContent: "center",
borderRadius: 16,
},
closeButtonText: {
fontSize: 20,
fontWeight: "300",
color: colors.text,
},
scrollView: {
flex: 1,
padding: 20,
},
scrollContent: {
paddingBottom: Platform.OS === "ios" ? 120 : 80,
},
fieldGroup: {
marginBottom: 24,
},
label: {
fontSize: 15,
fontWeight: "600",
marginBottom: 8,
color: colors.text,
},
input: {
borderWidth: 1,
borderRadius: 12,
paddingHorizontal: 16,
paddingVertical: 14,
fontSize: 15,
backgroundColor: colors.surface,
color: colors.text,
},
selectInput: {
borderWidth: 1,
borderRadius: 12,
backgroundColor: colors.surface,
},
dateInput: {
justifyContent: "center",
},
errorText: {
fontSize: 13,
color: "red",
marginTop: 4,
},
actionButtons: {
flexDirection: "row",
paddingHorizontal: 20,
paddingVertical: 16,
gap: 12,
borderTopWidth: 1,
borderTopColor: colors.border,
},
resetButton: {
flex: 1,
paddingVertical: 14,
borderRadius: 12,
borderWidth: 1,
alignItems: "center",
justifyContent: "center",
},
resetButtonText: {
fontSize: 16,
fontWeight: "600",
},
submitButton: {
flex: 1,
paddingVertical: 14,
borderRadius: 12,
alignItems: "center",
justifyContent: "center",
},
submitButtonText: {
color: "#ffffff",
fontSize: 16,
fontWeight: "600",
},
// Date Picker Modal Styles
datePickerModal: {
flex: 1,
justifyContent: "flex-end",
backgroundColor: "rgba(0, 0, 0, 0.5)",
},
datePickerContent: {
backgroundColor: colors.background,
borderTopLeftRadius: 24,
borderTopRightRadius: 24,
paddingBottom: 20,
},
datePickerHeader: {
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
paddingHorizontal: 20,
paddingVertical: 16,
borderBottomWidth: 1,
borderBottomColor: colors.border,
},
datePickerTitle: {
fontSize: 16,
fontWeight: "600",
color: colors.text,
},
datePickerClose: {
fontSize: 20,
color: colors.text,
},
datePickerIOS: {
height: 200,
marginTop: 20,
},
datePickerButton: {
marginHorizontal: 20,
paddingVertical: 14,
borderRadius: 12,
alignItems: "center",
marginTop: 20,
},
datePickerButtonText: {
color: "#ffffff",
fontSize: 16,
fontWeight: "600",
},
});
export default CreateOrUpdateShip;

View File

@@ -0,0 +1,523 @@
import { queryShipsImage } from "@/controller/DeviceController";
import { useThemeContext } from "@/hooks/use-theme-context";
import { useGroup } from "@/state/use-group";
import { usePort } from "@/state/use-ports";
import { useShipTypes } from "@/state/use-ship-types";
import { Ionicons } from "@expo/vector-icons";
import { fromByteArray } from "base64-js";
import { Image } from "expo-image";
import { useEffect, useState } from "react";
import { StyleSheet, Text, TouchableOpacity, View } from "react-native";
interface ShipCardProps {
ship: Model.Ship;
onPress?: () => void;
}
export default function ShipCard({ ship, onPress }: ShipCardProps) {
const { colors } = useThemeContext();
const { ports, getPorts } = usePort();
const { shipTypes, getShipTypes } = useShipTypes();
const [shipImage, setShipImage] = useState<string | null>(null);
const { groups, getUserGroups, getChildrenOfGroups, childrenOfGroups } =
useGroup();
useEffect(() => {
if (ports === null) {
getPorts();
}
}, [ports, getPorts]);
useEffect(() => {
if (!shipTypes || shipTypes.length === 0) {
getShipTypes();
}
}, [shipTypes, getShipTypes]);
useEffect(() => {
if (groups === null) {
getUserGroups();
}
}, [groups, getUserGroups]);
useEffect(() => {
if (groups && ship.ship_group_id) {
const groupId = groups?.groups?.[0]?.id || "";
// childrenOfGroups is initialised as null in the store; check for null to fetch once
if (groupId && childrenOfGroups == null) {
getChildrenOfGroups(groupId);
}
}
}, [groups, childrenOfGroups, getChildrenOfGroups]);
// Themed styles
useEffect(() => {
let mounted = true;
const loadShipImage = async () => {
try {
const resp = await queryShipsImage(ship.id || "");
const contentType = resp.headers["content-type"] || "image/jpeg";
const uint8 = new Uint8Array(resp.data); // ArrayBuffer -> Uint8Array
const base64 = fromByteArray(uint8); // base64-js
const uri = `data:${contentType};base64,${base64}`;
if (!mounted) return;
// assign received value to state if present; adapt to actual resp shape as needed
setShipImage(uri);
} catch (error) {
// console.log("Error when get image: ", error);
}
};
loadShipImage();
return () => {
mounted = false;
};
}, [ship]);
const themedStyles = {
card: {
backgroundColor: colors.card,
shadowColor: colors.text,
},
title: {
color: colors.text,
},
subtitle: {
color: colors.textSecondary,
},
label: {
color: colors.textSecondary,
},
value: {
color: colors.text,
},
divider: {
backgroundColor: colors.separator,
},
badge: {
backgroundColor: colors.primary + "15",
borderColor: colors.primary,
},
badgeText: {
color: colors.primary,
},
infoBox: {
backgroundColor: colors.primary + "10",
},
infoIcon: {
color: colors.primary,
},
};
// ============ IMAGE VARIANT ============
if (shipImage) {
return (
<TouchableOpacity
style={[styles.imageCard, themedStyles.card]}
onPress={onPress}
activeOpacity={0.8}
>
{/* Image Section */}
<View style={styles.imageContainer}>
{shipImage ? (
<Image
source={{ uri: shipImage }}
style={styles.shipImage}
resizeMode="cover"
/>
) : (
<View
style={[
styles.imagePlaceholder,
{ backgroundColor: colors.backgroundSecondary },
]}
>
<Ionicons name="boat" size={48} color={colors.textSecondary} />
</View>
)}
{/* Ship Type Badge */}
{shipTypes && (
<View style={styles.typeBadge}>
<Ionicons name="boat-outline" size={14} color="#fff" />
<Text style={styles.typeBadgeText}>
{shipTypes.find((type) => type.id === ship.ship_type)?.name ||
"Unknown"}
</Text>
</View>
)}
</View>
{/* Info Section */}
<View style={styles.imageCardContent}>
{/* Title & Registration */}
<Text style={[styles.imageCardTitle, themedStyles.title]}>
{ship.name || "Unknown Ship"}
</Text>
<View style={styles.regRow}>
<View style={[styles.regBadge, themedStyles.badge]}>
<Text style={[styles.regBadgeText, themedStyles.badgeText]}>
{ship.reg_number || "-"}
</Text>
</View>
{childrenOfGroups && (
<View style={styles.locationRow}>
<Ionicons
name="location"
size={14}
color={colors.textSecondary}
/>
<Text style={[styles.locationText, themedStyles.subtitle]}>
{childrenOfGroups.groups?.find(
(group) => group?.metadata?.code === ship.province_code
)?.name || "-"}
</Text>
</View>
)}
</View>
{/* Info Grid */}
<View style={styles.imageInfoGrid}>
<InfoBox
icon="resize"
label="Length"
value={ship.ship_length ? `${ship.ship_length}m` : "-"}
themedStyles={themedStyles}
/>
<InfoBox
icon="flash"
label="Engine Power"
value={ship.ship_power ? `${ship.ship_power} HP` : "-"}
themedStyles={themedStyles}
/>
<InfoBox
icon="document-text"
label="License"
value={ship.fishing_license_number || "-"}
themedStyles={themedStyles}
/>
<InfoBox
icon="navigate"
label="Home Port"
value={
ports?.ports
? ports.ports.find((port) => port.id === ship.home_port)
?.name || "-"
: "-"
}
themedStyles={themedStyles}
/>
</View>
</View>
</TouchableOpacity>
);
}
// ============ COMPACT VARIANT ============
return (
<TouchableOpacity
style={[styles.compactCard, themedStyles.card]}
onPress={onPress}
activeOpacity={0.8}
>
{/* Header */}
<View style={styles.compactHeader}>
<View style={[styles.shipIcon, themedStyles.infoBox]}>
<Ionicons name="boat" size={24} color={colors.primary} />
</View>
<View style={styles.compactHeaderText}>
<Text style={[styles.compactTitle, themedStyles.title]}>
{ship.name || "Unknown Ship"}
</Text>
<Text style={[styles.compactSubtitle, themedStyles.subtitle]}>
{shipTypes.find((type) => type.id === ship.ship_type)?.name ||
"Unknown"}
</Text>
</View>
<View style={[styles.regBadge, themedStyles.badge]}>
<Text style={[styles.regBadgeText, themedStyles.badgeText]}>
{ship.reg_number || "-"}
</Text>
</View>
</View>
{/* Info Grid */}
<View style={styles.compactInfoGrid}>
<CompactInfoBox
icon="resize"
label="Length"
value={ship.ship_length ? `${ship.ship_length}m` : "-"}
themedStyles={themedStyles}
/>
<CompactInfoBox
icon="flash"
label="Power"
value={ship.ship_power ? `${ship.ship_power} HP` : "-"}
themedStyles={themedStyles}
/>
<CompactInfoBox
icon="navigate"
label="Port"
value={
ports?.ports
? ports.ports.find((port) => port.id === ship.home_port)?.name ||
"-"
: "-"
}
themedStyles={themedStyles}
/>
<CompactInfoBox
icon="document-text"
label="License"
value={ship.fishing_license_number || "-"}
themedStyles={themedStyles}
/>
<CompactInfoBox
icon="location"
label="Province"
value={
childrenOfGroups?.groups?.find(
(group) => group?.metadata?.code === ship.province_code
)?.name || "-"
}
themedStyles={themedStyles}
/>
</View>
{/* Footer - IMO & MMSI */}
{(ship.imo_number || ship.mmsi_number) && (
<>
<View style={[styles.divider, themedStyles.divider]} />
<View style={styles.footerInfo}>
{ship.imo_number && (
<View style={styles.footerRow}>
<Text style={[styles.footerLabel, themedStyles.label]}>
IMO Number:
</Text>
<Text style={[styles.footerValue, themedStyles.value]}>
{ship.imo_number}
</Text>
</View>
)}
{ship.mmsi_number && (
<View style={styles.footerRow}>
<Text style={[styles.footerLabel, themedStyles.label]}>
MMSI Number:
</Text>
<Text style={[styles.footerValue, themedStyles.value]}>
{ship.mmsi_number}
</Text>
</View>
)}
</View>
</>
)}
</TouchableOpacity>
);
}
// ============ SUB-COMPONENTS ============
interface InfoBoxProps {
icon: keyof typeof Ionicons.glyphMap;
label: string;
value: string;
themedStyles: any;
}
function InfoBox({ icon, label, value, themedStyles }: InfoBoxProps) {
return (
<View style={styles.infoBox}>
<Ionicons name={icon} size={18} color={themedStyles.infoIcon.color} />
<Text style={[styles.infoLabel, themedStyles.label]}>{label}</Text>
<Text style={[styles.infoValue, themedStyles.value]}>{value}</Text>
</View>
);
}
function CompactInfoBox({ icon, label, value, themedStyles }: InfoBoxProps) {
return (
<View style={[styles.compactInfoBox, themedStyles.infoBox]}>
<Ionicons name={icon} size={16} color={themedStyles.infoIcon.color} />
<View style={styles.compactInfoText}>
<Text style={[styles.compactInfoLabel, themedStyles.label]}>
{label}
</Text>
<Text style={[styles.compactInfoValue, themedStyles.value]}>
{value}
</Text>
</View>
</View>
);
}
// ============ STYLES ============
const styles = StyleSheet.create({
// === IMAGE VARIANT ===
imageCard: {
borderRadius: 16,
overflow: "hidden",
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.1,
shadowRadius: 8,
elevation: 3,
marginVertical: 8,
marginHorizontal: 16,
boxShadow: "0 2px 8px rgba(0,0,0,0.1)",
},
imageContainer: {
height: 180,
position: "relative",
},
shipImage: {
width: "100%",
height: "100%",
},
imagePlaceholder: {
width: "100%",
height: "100%",
alignItems: "center",
justifyContent: "center",
},
typeBadge: {
position: "absolute",
top: 12,
left: 12,
flexDirection: "row",
alignItems: "center",
backgroundColor: "rgba(59, 130, 246, 0.9)",
paddingHorizontal: 10,
paddingVertical: 6,
borderRadius: 8,
gap: 6,
},
typeBadgeText: {
color: "#fff",
fontSize: 12,
fontWeight: "600",
},
imageCardContent: {
padding: 16,
},
imageCardTitle: {
fontSize: 18,
fontWeight: "700",
marginBottom: 8,
},
regRow: {
flexDirection: "row",
alignItems: "center",
gap: 12,
marginBottom: 16,
},
regBadge: {
paddingHorizontal: 10,
paddingVertical: 4,
borderRadius: 6,
borderWidth: 1,
},
regBadgeText: {
fontSize: 12,
fontWeight: "600",
},
locationRow: {
flexDirection: "row",
alignItems: "center",
gap: 4,
},
locationText: {
fontSize: 13,
},
imageInfoGrid: {
flexDirection: "row",
flexWrap: "wrap",
gap: 12,
},
infoBox: {
width: "47%",
flexDirection: "row",
alignItems: "center",
gap: 8,
paddingVertical: 8,
},
infoLabel: {
fontSize: 12,
},
infoValue: {
fontSize: 14,
fontWeight: "600",
marginLeft: "auto",
},
// === COMPACT VARIANT ===
compactCard: {
borderRadius: 16,
padding: 16,
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.08,
shadowRadius: 8,
elevation: 2,
marginVertical: 8,
marginHorizontal: 16,
},
compactHeader: {
flexDirection: "row",
alignItems: "center",
marginBottom: 16,
},
shipIcon: {
width: 48,
height: 48,
borderRadius: 12,
alignItems: "center",
justifyContent: "center",
},
compactHeaderText: {
flex: 1,
marginLeft: 12,
},
compactTitle: {
fontSize: 16,
fontWeight: "700",
},
compactSubtitle: {
fontSize: 13,
marginTop: 2,
},
compactInfoGrid: {
flexDirection: "row",
flexWrap: "wrap",
gap: 10,
},
compactInfoBox: {
flexDirection: "row",
alignItems: "center",
paddingHorizontal: 12,
paddingVertical: 10,
borderRadius: 10,
gap: 8,
minWidth: "47%",
flexGrow: 1,
},
compactInfoText: {
flex: 1,
},
compactInfoLabel: {
fontSize: 11,
},
compactInfoValue: {
fontSize: 14,
fontWeight: "600",
},
divider: {
height: 1,
marginVertical: 12,
},
footerInfo: {
gap: 6,
},
footerRow: {
flexDirection: "row",
justifyContent: "space-between",
},
footerLabel: {
fontSize: 13,
},
footerValue: {
fontSize: 13,
fontWeight: "500",
},
});