import { IconSymbol } from "@/components/ui/icon-symbol"; import React, { useState } from "react"; import { Modal, ScrollView, Text, TextInput, TouchableOpacity, View, } from "react-native"; import styles from "./style/NetDetailModal.styles"; // --------------------------- // 🧩 Interface // --------------------------- interface FishCatch { fish_species_id: number; fish_name: string; catch_number: number; catch_unit: string; fish_size: number; fish_rarity: number; fish_condition: string; gear_usage: string; } interface NetDetail { id: string; stt: string; trangThai: string; thoiGianBatDau?: string; thoiGianKetThuc?: string; viTriHaThu?: string; viTriThuLuoi?: string; doSauHaThu?: string; doSauThuLuoi?: string; catchList?: FishCatch[]; ghiChu?: string; } interface NetDetailModalProps { visible: boolean; onClose: () => void; netData: NetDetail | null; } // --------------------------- // 🧵 Component Modal // --------------------------- const NetDetailModal: React.FC = ({ visible, onClose, netData, }) => { const [isEditing, setIsEditing] = useState(false); const [editableCatchList, setEditableCatchList] = useState([]); const [selectedFishIndex, setSelectedFishIndex] = useState( null ); const [selectedUnitIndex, setSelectedUnitIndex] = useState( null ); const [selectedConditionIndex, setSelectedConditionIndex] = useState< number | null >(null); // Khởi tạo dữ liệu khi netData thay đổi React.useEffect(() => { if (netData?.catchList) { setEditableCatchList(netData.catchList); } }, [netData]); if (!netData) return null; const isCompleted = netData.trangThai === "Đã hoàn thành"; // Danh sách tên cá có sẵn const fishNameOptions = [ "Cá chim trắng", "Cá song đỏ", "Cá hồng", "Cá nục", "Cá ngừ đại dương", "Cá mú trắng", "Cá hồng phớn", "Cá hổ Napoleon", "Cá nược", "Cá đuối quạt", ]; // Danh sách đơn vị const unitOptions = ["kg", "con", "tấn"]; // Danh sách tình trạng const conditionOptions = ["Còn sống", "Chết", "Bị thương"]; const handleEdit = () => { setIsEditing(!isEditing); }; const handleSave = () => { setIsEditing(false); // TODO: Save data to backend console.log("Saved catch list:", editableCatchList); }; const handleCancel = () => { setIsEditing(false); setEditableCatchList(netData.catchList || []); }; const updateCatchItem = ( index: number, field: keyof FishCatch, value: string | number ) => { setEditableCatchList((prev) => prev.map((item, i) => { if (i === index) { const updatedItem = { ...item }; if ( field === "catch_number" || field === "fish_size" || field === "fish_rarity" ) { updatedItem[field] = Number(value) || 0; } else { updatedItem[field] = value as never; } return updatedItem; } return item; }) ); }; const totalCatch = editableCatchList.reduce( (sum, item) => sum + item.catch_number, 0 ); const infoItems = [ { label: "Số thứ tự", value: netData.stt }, { label: "Trạng thái", value: netData.trangThai, isStatus: true, }, { label: "Thời gian bắt đầu", value: netData.thoiGianBatDau || "Chưa cập nhật", }, { label: "Thời gian kết thúc", value: netData.thoiGianKetThuc || "Chưa cập nhật", }, { label: "Vị trí hạ thu", value: netData.viTriHaThu || "Chưa cập nhật", }, { label: "Vị trí thu lưới", value: netData.viTriThuLuoi || "Chưa cập nhật", }, { label: "Độ sâu hạ thu", value: netData.doSauHaThu || "Chưa cập nhật", }, { label: "Độ sâu thu lưới", value: netData.doSauThuLuoi || "Chưa cập nhật", }, ]; return ( {/* Header */} Chi tiết mẻ lưới {isEditing ? ( <> Hủy Lưu ) : ( )} {/* Content */} {/* Thông tin chung */} {infoItems.map((item, index) => ( {item.label} {item.isStatus ? ( {item.value} ) : ( {item.value} )} ))} {/* Danh sách cá bắt được */} Danh sách cá bắt được Tổng: {totalCatch.toLocaleString()} kg {editableCatchList.map((fish, index) => ( {/* Tên cá - Select */} Tên cá {isEditing ? ( setSelectedFishIndex( selectedFishIndex === index ? null : index ) } > {fish.fish_name} {selectedFishIndex === index && ( {fishNameOptions.map((option, optIndex) => ( { updateCatchItem(index, "fish_name", option); setSelectedFishIndex(null); }} > {option} ))} )} ) : ( {fish.fish_name} )} {/* Số lượng & Đơn vị */} Số lượng {isEditing ? ( updateCatchItem(index, "catch_number", value) } keyboardType="numeric" placeholder="0" /> ) : ( {fish.catch_number} )} Đơn vị {isEditing ? ( setSelectedUnitIndex( selectedUnitIndex === index ? null : index ) } > {fish.catch_unit} {selectedUnitIndex === index && ( {unitOptions.map((option, optIndex) => ( { updateCatchItem(index, "catch_unit", option); setSelectedUnitIndex(null); }} > {option} ))} )} ) : ( {fish.catch_unit} )} {/* Kích thước & Độ hiếm */} Kích thước (cm) {isEditing ? ( updateCatchItem(index, "fish_size", value) } keyboardType="numeric" placeholder="0" /> ) : ( {fish.fish_size} cm )} Độ hiếm {isEditing ? ( updateCatchItem(index, "fish_rarity", value) } keyboardType="numeric" placeholder="1-5" /> ) : ( {fish.fish_rarity} )} {/* Tình trạng */} Tình trạng {isEditing ? ( setSelectedConditionIndex( selectedConditionIndex === index ? null : index ) } > {fish.fish_condition} {selectedConditionIndex === index && ( {conditionOptions.map((option, optIndex) => ( { updateCatchItem(index, "fish_condition", option); setSelectedConditionIndex(null); }} > {option} ))} )} ) : ( {fish.fish_condition} )} {/* Ngư cụ sử dụng */} Ngư cụ sử dụng {isEditing ? ( updateCatchItem(index, "gear_usage", value) } placeholder="Nhập ngư cụ..." /> ) : ( {fish.gear_usage || "Không có"} )} ))} {/* Ghi chú */} {netData.ghiChu && ( Ghi chú {netData.ghiChu} )} ); }; export default NetDetailModal;