|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { |
| 3 | + Box, |
| 4 | + Container, |
| 5 | + Paper, |
| 6 | + Typography, |
| 7 | + TextField, |
| 8 | + Button, |
| 9 | + IconButton, |
| 10 | + Select, |
| 11 | + MenuItem, |
| 12 | + FormControl, |
| 13 | + InputLabel, |
| 14 | + Checkbox, |
| 15 | + FormControlLabel, |
| 16 | + Divider, |
| 17 | +} from '@mui/material'; |
| 18 | +import { |
| 19 | + ArrowBack as ArrowBackIcon, |
| 20 | + Add as AddIcon, |
| 21 | + Delete as DeleteIcon, |
| 22 | +} from '@mui/icons-material'; |
| 23 | + |
| 24 | +interface CellEntry { |
| 25 | + id: string; |
| 26 | + cellId: string; |
| 27 | +} |
| 28 | + |
| 29 | +interface BoundaryPoint { |
| 30 | + id: string; |
| 31 | + lat: string; |
| 32 | + lng: string; |
| 33 | +} |
| 34 | + |
| 35 | +export default function CreateEditSite() { |
| 36 | + const [name, setName] = useState(''); |
| 37 | + const [longitude, setLongitude] = useState(''); |
| 38 | + const [latitude, setLatitude] = useState(''); |
| 39 | + const [status, setStatus] = useState(''); |
| 40 | + const [address, setAddress] = useState(''); |
| 41 | + const [cells, setCells] = useState<CellEntry[]>([]); |
| 42 | + const [colorEnabled, setColorEnabled] = useState(true); |
| 43 | + const [colorValue, setColorValue] = useState(''); |
| 44 | + const [boundaryEnabled, setBoundaryEnabled] = useState(true); |
| 45 | + const [boundaryPoints, setBoundaryPoints] = useState<BoundaryPoint[]>([]); |
| 46 | + |
| 47 | + const handleBack = () => { |
| 48 | + console.log('Navigate back'); |
| 49 | + window.open('/admin/list-sites', '_self'); |
| 50 | + }; |
| 51 | + |
| 52 | + const handleSave = () => { |
| 53 | + console.log('Save site'); |
| 54 | + }; |
| 55 | + |
| 56 | + const addCell = () => { |
| 57 | + const newCell: CellEntry = { |
| 58 | + id: Date.now().toString(), |
| 59 | + cellId: '' |
| 60 | + }; |
| 61 | + setCells([...cells, newCell]); |
| 62 | + }; |
| 63 | + |
| 64 | + const deleteCell = (id: string) => { |
| 65 | + setCells(cells.filter(cell => cell.id !== id)); |
| 66 | + }; |
| 67 | + |
| 68 | + const updateCellId = (id: string, cellId: string) => { |
| 69 | + setCells(cells.map(cell => cell.id === id ? { ...cell, cellId } : cell)); |
| 70 | + }; |
| 71 | + |
| 72 | + const addBoundaryPoint = () => { |
| 73 | + const newPoint: BoundaryPoint = { |
| 74 | + id: Date.now().toString(), |
| 75 | + lat: '', |
| 76 | + lng: '' |
| 77 | + }; |
| 78 | + setBoundaryPoints([...boundaryPoints, newPoint]); |
| 79 | + }; |
| 80 | + |
| 81 | + const deleteBoundaryPoint = (id: string) => { |
| 82 | + setBoundaryPoints(boundaryPoints.filter(point => point.id !== id)); |
| 83 | + }; |
| 84 | + |
| 85 | + const updateBoundaryPoint = (id: string, field: 'lat' | 'lng', value: string) => { |
| 86 | + setBoundaryPoints(boundaryPoints.map(point => |
| 87 | + point.id === id ? { ...point, [field]: value } : point |
| 88 | + )); |
| 89 | + }; |
| 90 | + |
| 91 | + return ( |
| 92 | + <Container maxWidth="md" sx={{ mt: 4, mb: 4 }}> |
| 93 | + <Paper elevation={3} sx={{ p: 3 }}> |
| 94 | + <Box sx={{ display: 'flex', alignItems: 'center', mb: 3 }}> |
| 95 | + <IconButton onClick={handleBack} sx={{ mr: 2 }}> |
| 96 | + <ArrowBackIcon /> |
| 97 | + </IconButton> |
| 98 | + <Button |
| 99 | + variant="contained" |
| 100 | + onClick={handleSave} |
| 101 | + sx={{ |
| 102 | + backgroundColor: '#4caf50', |
| 103 | + '&:hover': { |
| 104 | + backgroundColor: '#388e3c', |
| 105 | + }, |
| 106 | + }} |
| 107 | + > |
| 108 | + Save |
| 109 | + </Button> |
| 110 | + </Box> |
| 111 | + <Box sx={{ mb: 3 }}> |
| 112 | + <TextField |
| 113 | + fullWidth |
| 114 | + label="Name" |
| 115 | + value={name} |
| 116 | + onChange={(e) => setName(e.target.value)} |
| 117 | + sx={{ mb: 2 }} |
| 118 | + /> |
| 119 | + |
| 120 | + <Box sx={{ display: 'flex', gap: 2, mb: 2 }}> |
| 121 | + <TextField |
| 122 | + fullWidth |
| 123 | + label="Longitude" |
| 124 | + value={longitude} |
| 125 | + onChange={(e) => setLongitude(e.target.value)} |
| 126 | + /> |
| 127 | + <TextField |
| 128 | + fullWidth |
| 129 | + label="Latitude" |
| 130 | + value={latitude} |
| 131 | + onChange={(e) => setLatitude(e.target.value)} |
| 132 | + /> |
| 133 | + </Box> |
| 134 | + |
| 135 | + <FormControl fullWidth sx={{ mb: 2 }}> |
| 136 | + <InputLabel>Status</InputLabel> |
| 137 | + <Select |
| 138 | + value={status} |
| 139 | + onChange={(e) => setStatus(e.target.value)} |
| 140 | + label="Status" |
| 141 | + > |
| 142 | + <MenuItem value="active">Active</MenuItem> |
| 143 | + <MenuItem value="inactive">Inactive</MenuItem> |
| 144 | + <MenuItem value="maintenance">Maintenance</MenuItem> |
| 145 | + </Select> |
| 146 | + </FormControl> |
| 147 | + |
| 148 | + <TextField |
| 149 | + fullWidth |
| 150 | + label="Address" |
| 151 | + value={address} |
| 152 | + onChange={(e) => setAddress(e.target.value)} |
| 153 | + sx={{ mb: 2 }} |
| 154 | + /> |
| 155 | + </Box> |
| 156 | + <Box sx={{ mb: 3 }}> |
| 157 | + <Box sx={{ display: 'flex', alignItems: 'center', mb: 2 }}> |
| 158 | + <Typography variant="h6" sx={{ mr: 2 }}>Cells</Typography> |
| 159 | + <IconButton |
| 160 | + onClick={addCell} |
| 161 | + sx={{ |
| 162 | + backgroundColor: '#4caf50', |
| 163 | + color: 'white', |
| 164 | + width: 24, |
| 165 | + height: 24, |
| 166 | + '&:hover': { |
| 167 | + backgroundColor: '#388e3c', |
| 168 | + }, |
| 169 | + }} |
| 170 | + > |
| 171 | + <AddIcon sx={{ fontSize: 16 }} /> |
| 172 | + </IconButton> |
| 173 | + </Box> |
| 174 | + |
| 175 | + {cells.map((cell) => ( |
| 176 | + <Box key={cell.id} sx={{ display: 'flex', alignItems: 'center', mb: 1, gap: 2 }}> |
| 177 | + <Typography variant="body2" sx={{ minWidth: 60 }}>Cell ID</Typography> |
| 178 | + <TextField |
| 179 | + size="small" |
| 180 | + value={cell.cellId} |
| 181 | + onChange={(e) => updateCellId(cell.id, e.target.value)} |
| 182 | + sx={{ flexGrow: 1 }} |
| 183 | + /> |
| 184 | + <Button |
| 185 | + variant="contained" |
| 186 | + color="error" |
| 187 | + onClick={() => deleteCell(cell.id)} |
| 188 | + sx={{ |
| 189 | + backgroundColor: '#d32f2f', |
| 190 | + minWidth: 80, |
| 191 | + '&:hover': { |
| 192 | + backgroundColor: '#b71c1c', |
| 193 | + }, |
| 194 | + }} |
| 195 | + > |
| 196 | + Delete |
| 197 | + </Button> |
| 198 | + </Box> |
| 199 | + ))} |
| 200 | + </Box> |
| 201 | + |
| 202 | + <Divider sx={{ my: 3 }} /> |
| 203 | + |
| 204 | + <Box sx={{ mb: 3 }}> |
| 205 | + <FormControlLabel |
| 206 | + control={ |
| 207 | + <Checkbox |
| 208 | + checked={colorEnabled} |
| 209 | + onChange={(e) => setColorEnabled(e.target.checked)} |
| 210 | + /> |
| 211 | + } |
| 212 | + label="Color" |
| 213 | + /> |
| 214 | + {colorEnabled && ( |
| 215 | + <TextField |
| 216 | + fullWidth |
| 217 | + value={colorValue} |
| 218 | + onChange={(e) => setColorValue(e.target.value)} |
| 219 | + sx={{ mt: 1 }} |
| 220 | + /> |
| 221 | + )} |
| 222 | + </Box> |
| 223 | + <Box sx={{ mb: 3 }}> |
| 224 | + <Box sx={{ display: 'flex', alignItems: 'center', mb: 2 }}> |
| 225 | + <FormControlLabel |
| 226 | + control={ |
| 227 | + <Checkbox |
| 228 | + checked={boundaryEnabled} |
| 229 | + onChange={(e) => setBoundaryEnabled(e.target.checked)} |
| 230 | + /> |
| 231 | + } |
| 232 | + label="Boundary" |
| 233 | + /> |
| 234 | + {boundaryEnabled && ( |
| 235 | + <IconButton |
| 236 | + onClick={addBoundaryPoint} |
| 237 | + sx={{ |
| 238 | + backgroundColor: '#4caf50', |
| 239 | + color: 'white', |
| 240 | + width: 24, |
| 241 | + height: 24, |
| 242 | + ml: 2, |
| 243 | + '&:hover': { |
| 244 | + backgroundColor: '#388e3c', |
| 245 | + }, |
| 246 | + }} |
| 247 | + > |
| 248 | + <AddIcon sx={{ fontSize: 16 }} /> |
| 249 | + </IconButton> |
| 250 | + )} |
| 251 | + </Box> |
| 252 | + |
| 253 | + {boundaryEnabled && boundaryPoints.map((point) => ( |
| 254 | + <Box key={point.id} sx={{ display: 'flex', alignItems: 'center', mb: 1, gap: 2 }}> |
| 255 | + <Typography variant="body2" sx={{ minWidth: 80 }}>(Lat, Long)</Typography> |
| 256 | + <TextField |
| 257 | + size="small" |
| 258 | + placeholder="Lat" |
| 259 | + value={point.lat} |
| 260 | + onChange={(e) => updateBoundaryPoint(point.id, 'lat', e.target.value)} |
| 261 | + sx={{ flexGrow: 1 }} |
| 262 | + /> |
| 263 | + <TextField |
| 264 | + size="small" |
| 265 | + placeholder="Long" |
| 266 | + value={point.lng} |
| 267 | + onChange={(e) => updateBoundaryPoint(point.id, 'lng', e.target.value)} |
| 268 | + sx={{ flexGrow: 1 }} |
| 269 | + /> |
| 270 | + <Button |
| 271 | + variant="contained" |
| 272 | + color="error" |
| 273 | + onClick={() => deleteBoundaryPoint(point.id)} |
| 274 | + sx={{ |
| 275 | + backgroundColor: '#d32f2f', |
| 276 | + minWidth: 80, |
| 277 | + '&:hover': { |
| 278 | + backgroundColor: '#b71c1c', |
| 279 | + }, |
| 280 | + }} |
| 281 | + > |
| 282 | + Delete |
| 283 | + </Button> |
| 284 | + </Box> |
| 285 | + ))} |
| 286 | + </Box> |
| 287 | + </Paper> |
| 288 | + </Container> |
| 289 | + ); |
| 290 | +} |
0 commit comments