Spaces:
Running

File size: 4,352 Bytes
f287aeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import React, { useState } from 'react';
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '../../ui/dialog';
import { Button } from '../../ui/button';
import { Input } from '../../ui/input';
import { Label } from '../../ui/label';
import { RefreshCw, Plus } from 'lucide-react';

interface AddTankModalProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  onConfirm: (data: { name: string; capacity: number; volume: number; location: string }) => Promise<void>;
  initialData?: { name: string; capacity: number; volume: number; location: string };
  mode?: 'add' | 'edit';
}

export function AddTankModal({
  open,
  onOpenChange,
  onConfirm,
  initialData,
  mode = 'add'
}: AddTankModalProps) {
  const [name, setName] = useState(initialData?.name || '');
  const [location, setLocation] = useState(initialData?.location || '');
  const [capacity, setCapacity] = useState(initialData?.capacity || 5000);
  const [volume, setVolume] = useState(initialData?.volume || 50);

  React.useEffect(() => {
    if (open && initialData) {
      setName(initialData.name);
      setLocation(initialData.location);
      setCapacity(initialData.capacity);
      setVolume(initialData.volume);
    } else if (open && mode === 'add') {
      setName('');
      setLocation('');
      setCapacity(5000);
      setVolume(50);
    }
  }, [open, initialData, mode]);
  const [isSaving, setIsSaving] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    setIsSaving(true);
    setError(null);
    try {
      await onConfirm({ name, capacity, volume, location });
      setName('');
      setLocation('');
      setCapacity(5000);
      setVolume(50);
    } catch (err) {
      setError((err as Error).message);
    } finally {
      setIsSaving(false);
    }
  };

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="max-w-md">
        <DialogHeader>
          <DialogTitle>{mode === 'edit' ? 'Edit Tank' : 'Add New Tank'}</DialogTitle>
        </DialogHeader>
        <form onSubmit={handleSubmit} className="space-y-4">
          <div className="space-y-2">
            <Label htmlFor="tank-name">Tank Name *</Label>
            <Input
              id="tank-name"
              value={name}
              onChange={(e) => setName(e.target.value)}
              placeholder="e.g. Tank A-05"
              required
            />
          </div>
          <div className="space-y-2">
            <Label htmlFor="tank-location">Location *</Label>
            <Input
              id="tank-location"
              value={location}
              onChange={(e) => setLocation(e.target.value)}
              placeholder="e.g. Section A"
              required
            />
          </div>
          <div className="grid grid-cols-2 gap-4">
            <div className="space-y-2">
              <Label htmlFor="tank-capacity">Capacity (kg) *</Label>
              <Input
                id="tank-capacity"
                type="number"
                value={capacity}
                onChange={(e) => setCapacity(Number(e.target.value))}
                required
              />
            </div>
            <div className="space-y-2">
              <Label htmlFor="tank-volume">Volume (m³) *</Label>
              <Input
                id="tank-volume"
                type="number"
                value={volume}
                onChange={(e) => setVolume(Number(e.target.value))}
                required
              />
            </div>
          </div>
          {error && <p className="text-sm text-red-600 font-medium">{error}</p>}
          <div className="flex gap-2 pt-4">
            <Button type="button" variant="outline" className="flex-1" onClick={() => onOpenChange(false)}>
              Cancel
            </Button>
            <Button type="submit" className="flex-1 bg-[#088395] hover:bg-[#0A4D68]" disabled={isSaving}>
              {isSaving ? <RefreshCw className="w-4 h-4 animate-spin mr-2" /> : mode === 'edit' ? <RefreshCw className="w-4 h-4 mr-2" /> : <Plus className="w-4 h-4 mr-2" />}
              {mode === 'edit' ? 'Update Tank' : 'Create Tank'}
            </Button>
          </div>
        </form>
      </DialogContent>
    </Dialog>
  );
}