import Select, { SelectOption } from "@/components/Select"; import { ThemedText } from "@/components/themed-text"; import { ThemedView } from "@/components/themed-view"; import { useThemeContext } from "@/hooks/use-theme-context"; import { Ionicons } from "@expo/vector-icons"; import { useEffect } from "react"; import { Controller, useForm } from "react-hook-form"; import { StyleSheet, TextInput, TouchableOpacity, View } from "react-native"; interface AlarmSearchFormProps { initialValue?: { name?: string; level?: number; confirmed?: boolean; }; onSubmit: (payload: { name?: string; level?: number; confirmed?: boolean; }) => void; onReset?: () => void; } interface FormData { name: string; level: number; confirmed: string; // Using string for Select component compatibility } const AlarmSearchForm: React.FC = ({ initialValue, onSubmit, onReset, }) => { const { colors } = useThemeContext(); const levelOptions: SelectOption[] = [ { label: "Tất cả", value: 0 }, { label: "Cảnh báo", value: 1 }, { label: "Nguy hiểm", value: 2 }, ]; const confirmedOptions: SelectOption[] = [ { label: "Tất cả", value: "" }, { label: "Đã xác nhận", value: "true" }, { label: "Chưa xác nhận", value: "false" }, ]; const { control, handleSubmit, reset } = useForm({ defaultValues: { name: initialValue?.name || "", level: initialValue?.level || 0, confirmed: initialValue?.confirmed !== undefined ? initialValue.confirmed.toString() : "", }, }); useEffect(() => { if (initialValue) { reset({ name: initialValue.name || "", level: initialValue.level || 0, confirmed: initialValue.confirmed !== undefined ? initialValue.confirmed.toString() : "", }); } }, [initialValue, reset]); const onFormSubmit = (data: FormData) => { const payload: { name?: string; level?: number; confirmed?: boolean; } = { ...(data.name && { name: data.name }), ...(data.level !== 0 && { level: data.level }), ...(data.confirmed !== "" && { confirmed: data.confirmed === "true", }), }; onSubmit(payload); }; const handleReset = () => { reset({ name: "", level: 0, confirmed: undefined, }); // Submit empty payload to reset filters onSubmit({}); onReset?.(); }; return ( {/* Search Input */} ( Tìm kiếm {value ? ( onChange("")} style={styles.clearButton} > ) : null} )} /> {/* Level and Confirmed Selects */} ( Mức độ )} /> {/* Action Buttons */} Đặt lại Tìm kiếm ); }; const styles = StyleSheet.create({ container: { borderBottomWidth: 1, shadowColor: "#000", shadowOffset: { width: 0, height: 2, }, shadowOpacity: 0.1, shadowRadius: 3.84, elevation: 5, zIndex: 100, }, content: { padding: 16, overflow: "visible", }, inputContainer: { marginBottom: 16, }, label: { fontSize: 14, fontWeight: "500", marginBottom: 6, }, inputWrapper: { flexDirection: "row", alignItems: "center", borderWidth: 1, borderRadius: 8, paddingHorizontal: 12, }, input: { flex: 1, height: 40, fontSize: 16, }, clearButton: { marginLeft: 8, padding: 4, }, row: { flexDirection: "row", justifyContent: "space-between", marginBottom: 16, zIndex: 10, }, halfWidth: { width: "48%", zIndex: 5000, }, selectContainer: { // flex: 1, // Remove this to prevent taking full width zIndex: 5000, }, buttonRow: { flexDirection: "row", justifyContent: "space-between", gap: 12, marginTop: 16, }, button: { flex: 1, height: 40, borderRadius: 8, justifyContent: "center", alignItems: "center", }, secondaryButton: { borderWidth: 1, }, primaryButton: { // backgroundColor is set dynamically }, buttonText: { fontSize: 16, fontWeight: "600", }, }); export default AlarmSearchForm;