File size: 8,798 Bytes
4d76deb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import React, { useState, useEffect } from 'react';
import { createClient } from '@supabase/supabase-js';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faTachometerAlt, faCalendarAlt, faTools, faWrench, faClipboardCheck, faChartLine, faUsersCog, faBell, faBars } from '@fortawesome/free-solid-svg-icons';
import Swal from 'sweetalert2';
import Dashboard from './components/Dashboard';
import Equipment from './components/Equipment';

// Initialize Supabase client
const supabase = createClient(
  import.meta.env.VITE_SUPABASE_URL,
  import.meta.env.VITE_SUPABASE_ANON_KEY
);

function App() {
  const [currentTab, setCurrentTab] = useState('dashboard');
  const [equipment, setEquipment] = useState([]);
  const [maintenanceEvents, setMaintenanceEvents] = useState([]);
  const [isLoggedIn, setIsLoggedIn] = useState(false);
  const [sidebarCollapsed, setSidebarCollapsed] = useState(false);

  useEffect(() => {
    const checkUser = async () => {
      const { data: { user } } = await supabase.auth.getUser();
      if (user) {
        setIsLoggedIn(true);
        loadData();
      }
    };
    checkUser();
  }, []);

  const loadData = async () => {
    try {
      const [equipmentResult, eventsResult] = await Promise.all([
        supabase.from('equipment').select('*').order('created_at', { ascending: false }),
        supabase.from('maintenance_events').select('*').order('date', { ascending: false })
      ]);

      if (equipmentResult.error) throw equipmentResult.error;
      if (eventsResult.error) throw eventsResult.error;

      setEquipment(equipmentResult.data || []);
      setMaintenanceEvents(eventsResult.data || []);
    } catch (error) {
      console.error('Error loading data:', error);
      Swal.fire({
        title: 'Ошибка',
        text: 'Не удалось загрузить данные',
        icon: 'error'
      });
    }
  };

  const handleLogin = async (e) => {
    e.preventDefault();
    const formData = new FormData(e.target);
    const email = formData.get('email');
    const password = formData.get('password');

    try {
      const { error } = await supabase.auth.signInWithPassword({
        email,
        password
      });

      if (error) throw error;

      setIsLoggedIn(true);
      loadData();
    } catch (error) {
      Swal.fire({
        title: 'Ошибка входа',
        text: error.message,
        icon: 'error'
      });
    }
  };

  const handleLogout = async () => {
    await supabase.auth.signOut();
    setIsLoggedIn(false);
  };

  const handleAddEquipment = async (formData) => {
    try {
      const { error } = await supabase.from('equipment').insert([{
        name: formData.get('name'),
        type: formData.get('type'),
        serial: formData.get('serial'),
        inventory: formData.get('inventory'),
        commission_date: formData.get('commissionDate'),
        maintenance_interval: parseInt(formData.get('interval')),
        description: formData.get('description'),
        status: 'active'
      }]);

      if (error) throw error;

      loadData();
      Swal.fire({
        title: 'Успешно',
        text: 'Оборудование добавлено',
        icon: 'success'
      });
    } catch (error) {
      Swal.fire({
        title: 'Ошибка',
        text: error.message,
        icon: 'error'
      });
    }
  };

  if (!isLoggedIn) {
    return (
      <div className="fixed inset-0 bg-black bg-opacity-70 flex items-center justify-center">
        <div className="bg-white rounded-lg shadow-lg w-full max-w-md p-6">
          <h3 className="text-xl font-bold mb-4">Вход в систему</h3>
          <form onSubmit={handleLogin} className="space-y-4">
            <input
              type="email"
              name="email"
              placeholder="Email"
              required
              className="w-full px-4 py-2 border rounded-lg"
            />
            <input
              type="password"
              name="password"
              placeholder="Пароль"
              required
              className="w-full px-4 py-2 border rounded-lg"
            />
            <button
              type="submit"
              className="w-full bg-blue-600 hover:bg-blue-700 text-white py-2 rounded-lg"
            >
              Войти
            </button>
          </form>
        </div>
      </div>
    );
  }

  return (
    <div className="flex min-h-screen bg-gray-100">
      {/* Sidebar */}
      <div className={`fixed top-0 left-0 h-full bg-blue-800 text-white ${sidebarCollapsed ? 'w-16' : 'w-64'} p-4 transition-all duration-300`}>
        <div className="flex items-center justify-between mb-6">
          {!sidebarCollapsed && <h1 className="text-xl font-bold">MES System</h1>}
          <button onClick={() => setSidebarCollapsed(!sidebarCollapsed)} className="text-white hover:text-blue-200">
            <FontAwesomeIcon icon={faBars} />
          </button>
        </div>

        <nav>
          <ul className="space-y-2">
            {[
              { id: 'dashboard', icon: faTachometerAlt, label: 'Главная' },
              { id: 'schedule', icon: faCalendarAlt, label: 'График ТО' },
              { id: 'equipment', icon: faTools, label: 'Оборудование' },
              { id: 'maintenance', icon: faClipboardCheck, label: 'Техобслуживание' },
              { id: 'reports', icon: faChartLine, label: 'Отчеты' },
              { id: 'users', icon: faUsersCog, label: 'Пользователи' }
            ].map(item => (
              <li key={item.id}>
                <button
                  onClick={() => setCurrentTab(item.id)}
                  className={`w-full text-left px-4 py-2 rounded-lg hover:bg-blue-700 flex items-center ${currentTab === item.id ? 'bg-blue-700' : ''}`}
                >
                  <FontAwesomeIcon icon={item.icon} className="mr-2" />
                  {!sidebarCollapsed && item.label}
                </button>
              </li>
            ))}
          </ul>
        </nav>
      </div>

      {/* Main Content */}
      <div className={`flex-1 ${sidebarCollapsed ? 'ml-16' : 'ml-64'} p-6 transition-all duration-300`}>
        {/* Header */}
        <header className="bg-white shadow-sm p-4 rounded-lg mb-6 flex justify-between items-center">
          <h1 className="text-2xl font-semibold">
            {currentTab === 'dashboard' ? 'Главная панель' :
             currentTab === 'schedule' ? 'График ТО' :
             currentTab === 'equipment' ? 'Оборудование' :
             currentTab === 'maintenance' ? 'Техобслуживание' :
             currentTab === 'reports' ? 'Отчеты' : 'Пользователи'}
          </h1>
          <div className="flex space-x-4">
            <button className="p-2 rounded-full hover:bg-gray-100 relative">
              <FontAwesomeIcon icon={faBell} className="text-gray-600" />
              <span className="absolute top-0 right-0 bg-red-500 text-white text-xs w-5 h-5 flex items-center justify-center rounded-full">3</span>
            </button>
            <button
              onClick={handleLogout}
              className="bg-red-500 text-white px-4 py-2 rounded-lg hover:bg-red-600"
            >
              Выход
            </button>
          </div>
        </header>

        {/* Content based on current tab */}
        <div className="bg-white rounded-lg shadow p-6">
          {currentTab === 'dashboard' && (
            <Dashboard equipment={equipment} maintenanceEvents={maintenanceEvents} />
          )}
          {currentTab === 'equipment' && (
            <Equipment
              equipment={equipment}
              onAdd={handleAddEquipment}
              onEdit={(item) => {
                // Handle edit
              }}
              onDelete={async (id) => {
                try {
                  const { error } = await supabase
                    .from('equipment')
                    .delete()
                    .eq('id', id);
                  
                  if (error) throw error;
                  
                  loadData();
                  Swal.fire({
                    title: 'Успешно',
                    text: 'Оборудование удалено',
                    icon: 'success'
                  });
                } catch (error) {
                  Swal.fire({
                    title: 'Ошибка',
                    text: error.message,
                    icon: 'error'
                  });
                }
              }}
            />
          )}
          {/* Add other tab components here */}
        </div>
      </div>
    </div>
  );
}

export default App;