import { Colors } from "@/config"; import { queryShipGroups } from "@/controller/DeviceController"; import { ColorScheme, useTheme } from "@/hooks/use-theme-context"; import { useShipTypes } from "@/state/use-ship-types"; import { useEffect, useMemo, useState } from "react"; import { Controller, useForm } from "react-hook-form"; import { Animated, Modal, Pressable, ScrollView, StyleSheet, Text, TextInput, TouchableOpacity, View, } from "react-native"; import { GestureHandlerRootView } from "react-native-gesture-handler"; import Select from "./Select"; import Slider from "./Slider"; interface ShipSearchFormProps { initialValues?: Partial; isOpen: boolean; onClose: () => void; onSubmit?: (data: SearchShipResponse) => void; } export interface SearchShipResponse { ship_name: string; ship_length: [number, number]; reg_number: string; ship_power: [number, number]; ship_type: string | number; alarm_list: string; ship_group_id: string; group_id: string; } const ShipSearchForm = (props: ShipSearchFormProps) => { const { colors, colorScheme } = useTheme(); const styles = useMemo( () => createStyles(colors, colorScheme), [colors, colorScheme] ); const { shipTypes, getShipTypes } = useShipTypes(); const [groupShips, setGroupShips] = useState([]); const [slideAnim] = useState(new Animated.Value(0)); const { control, handleSubmit, reset, watch } = useForm({ defaultValues: { ship_name: props.initialValues?.ship_name || "", reg_number: props.initialValues?.reg_number || "", ship_length: [0, 100], ship_power: [0, 100000], ship_type: props.initialValues?.ship_type || "", alarm_list: props.initialValues?.alarm_list || "", ship_group_id: props.initialValues?.ship_group_id || "", group_id: props.initialValues?.group_id || "", }, }); const shipLengthValue = watch("ship_length"); const shipPowerValue = watch("ship_power"); useEffect(() => { if (shipTypes.length === 0) { getShipTypes(); } }, [shipTypes]); useEffect(() => { getShipGroups(); }, []); useEffect(() => { if (props.isOpen) { Animated.spring(slideAnim, { toValue: 1, useNativeDriver: true, tension: 50, friction: 8, }).start(); } else { slideAnim.setValue(0); } }, [props.isOpen]); useEffect(() => { if (props.initialValues) { reset({ ship_name: props.initialValues.ship_name || "", reg_number: props.initialValues.reg_number || "", ship_length: [ props.initialValues.ship_length?.[0] || 0, props.initialValues.ship_length?.[1] || 100, ], ship_power: [ props.initialValues.ship_power?.[0] || 0, props.initialValues.ship_power?.[1] || 100000, ], ship_type: props.initialValues.ship_type || "", alarm_list: props.initialValues.alarm_list || "", ship_group_id: props.initialValues.ship_group_id || "", group_id: props.initialValues.group_id || "", }); } }, [props.initialValues]); const getShipGroups = async () => { try { const response = await queryShipGroups(); if (response && response.data) { setGroupShips(response.data); } } catch (error) { console.error("Error fetching ship groups:", error); } }; const alarmListLabel = [ { label: "Tiếp cận vùng hạn chế", value: "50:10", }, { label: "Đã ra (vào) vùng hạn chế)", value: "50:11", }, { label: "Đang đánh bắt trong vùng hạn chế", value: "50:12", }, ]; const onSubmitForm = (data: SearchShipResponse) => { props.onSubmit?.(data); console.log("Data: ", data); // props.onClose(); }; const onReset = () => { reset({ ship_name: "", reg_number: "", ship_length: [0, 100], ship_power: [0, 100000], ship_type: "", alarm_list: "", ship_group_id: "", group_id: "", }); }; const translateY = slideAnim.interpolate({ inputRange: [0, 1], outputRange: [600, 0], }); return ( true} > {/* Header */} Tìm kiếm tàu {/* Form Content */} {/* Tên tàu */} Tên tàu ( )} /> {/* Số đăng ký */} Số đăng ký ( )} /> {/* Chiều dài */} Chiều dài ({shipLengthValue[0]}m - {shipLengthValue[1]}m) ( onChange(val as [number, number]) } activeColor={colors.primary} /> )} /> {/* Công suất */} Công suất ({shipPowerValue[0]}kW - {shipPowerValue[1]}kW) ( onChange(val as [number, number]) } activeColor={colors.primary} /> )} /> {/* Loại tàu */} Loại tàu ( ({ label: type.label || "", value: type.value || "", }))} placeholder="Chọn loại cảnh báo" mode="multiple" value={value} onChange={onChange} /> )} /> {/* Đội tàu */} Đội tàu (