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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>IncidentNinja React Edition</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
    <div id="root"></div>
    <script src="https://unpkg.com/react@18/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
    <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
    <script src="https://unpkg.com/feather-icons"></script>
    <script src="https://cdn.jsdelivr.net/npm/vanta@latest/dist/vanta.net.min.js"></script>
    <script type="text/babel">
        const { useState, useEffect } = React;

        const IncidentNinja = () => {
            const [incidents, setIncidents] = useState([]);
            const [filteredIncidents, setFilteredIncidents] = useState([]);
            const [loading, setLoading] = useState(true);
            const [searchTerm, setSearchTerm] = useState('');
            const [priorityFilter, setPriorityFilter] = useState('all');
            const [statusFilter, setStatusFilter] = useState('all');
            const [currentPage, setCurrentPage] = useState(1);
            const [selectedIncident, setSelectedIncident] = useState(null);
            const [showModal, setShowModal] = useState(false);
            const [stats, setStats] = useState({
                total: 0,
                highPriority: 0,
                inProgress: 0,
                resolved: 0
            });

            // Mock data fetch
            useEffect(() => {
                const fetchData = () => {
                    setLoading(true);
                    setTimeout(() => {
                        const mockData = Array.from({ length: 15 }, (_, i) => ({
                            id: `mock-id-${i}`,
                            number: `INC${100000 + i}`,
                            short_description: `Issue with ${['server', 'database', 'application', 'network', 'printer'][i % 5]}`,
                            description: `Detailed description of issue with ${['server', 'database', 'application', 'network', 'printer'][i % 5]}.`,
                            priority: [1, 2, 3][i % 3],
                            state: ['New', 'In Progress', 'Resolved', 'Closed'][i % 4],
                            created: new Date(Date.now() - (i * 3600000)).toISOString(),
                            updated: new Date(Date.now() - (i * 1800000)).toISOString(),
                            work_notes: i % 2 === 0 ? 'Working on the issue.' : 'Investigating further.'
                        }));

                        setIncidents(mockData);
                        setLoading(false);
                    }, 1000);
                };

                fetchData();
            }, []);

            // Filter and pagination logic
            useEffect(() => {
                const filtered = incidents.filter(incident => {
                    const matchesSearch = incident.short_description.toLowerCase().includes(searchTerm.toLowerCase()) || 
                                         incident.number.toLowerCase().includes(searchTerm.toLowerCase());
                    const matchesPriority = priorityFilter === 'all' || incident.priority == priorityFilter;
                    const matchesStatus = statusFilter === 'all' || incident.state === statusFilter;
                    return matchesSearch && matchesPriority && matchesStatus;
                });

                setFilteredIncidents(filtered);

                // Update stats
                setStats({
                    total: incidents.length,
                    highPriority: incidents.filter(i => i.priority === 1).length,
                    inProgress: incidents.filter(i => i.state === 'In Progress').length,
                    resolved: incidents.filter(i => i.state === 'Resolved' || i.state === 'Closed').length
                });
            }, [incidents, searchTerm, priorityFilter, statusFilter]);

            // Vanta.js background effect
            useEffect(() => {
                VANTA.NET({
                    el: "#vanta-bg",
                    color: 0x6366f1,
                    backgroundColor: 0xf8fafc,
                    points: 12,
                    maxDistance: 22,
                    spacing: 18
                });
            }, []);

            const refreshIcons = () => {
                if (window.feather) {
                    window.feather.replace();
                }
            };

            useEffect(() => {
                refreshIcons();
            }, [filteredIncidents, showModal]);

            const handleIncidentClick = (incident) => {
                setSelectedIncident(incident);
                setShowModal(true);
            };

            const handleResolve = () => {
                if (selectedIncident) {
                    const updatedIncidents = incidents.map(inc => 
                        inc.id === selectedIncident.id ? {...inc, state: 'Resolved'} : inc
                    );
                    setIncidents(updatedIncidents);
                    setShowModal(false);
                }
            };

            const IncidentCard = ({ incident }) => (
                <div 
                    className={`incident-card bg-white rounded-lg shadow p-4 cursor-pointer hover:shadow-md transition-shadow duration-200 border-l-4 ${getPriorityBorderClass(incident.priority)}`}
                    onClick={() => handleIncidentClick(incident)}
                >
                    <div className="flex justify-between items-start">
                        <div>
                            <h3 className="font-medium text-gray-900">{incident.number}</h3>
                            <p className="text-sm text-gray-500 mt-1">{incident.short_description}</p>
                        </div>
                        <span className={`px-2 py-1 text-xs rounded-full ${getPriorityBadgeClass(incident.priority)}`}>
                            {getPriorityText(incident.priority)}
                        </span>
                    </div>
                    <div className="mt-4 flex items-center justify-between">
                        <span className={`text-sm ${getStatusColorClass(incident.state)}`}>
                            <i data-feather={getStatusIcon(incident.state)} className="w-3 h-3 inline mr-1"></i>
                            {incident.state}
                        </span>
                        <span className="text-xs text-gray-500">{formatDate(incident.created)}</span>
                    </div>
                </div>
            );

            const StatsCard = ({ title, value, icon, color }) => (
                <div className="bg-white rounded-lg shadow p-4">
                    <div className="flex justify-between items-center">
                        <div>
                            <p className="text-sm text-gray-500">{title}</p>
                            <h3 className="text-2xl font-bold text-gray-800">{value}</h3>
                        </div>
                        <div className={`p-3 rounded-full ${color.bg} ${color.text}`}>
                            <i data-feather={icon} className="w-5 h-5"></i>
                        </div>
                    </div>
                </div>
            );

            return (
                <div className="relative min-h-screen">
                    <div id="vanta-bg" className="fixed inset-0 z-0"></div>
                    <div className="relative z-10">
                        {/* Header */}
                        <header className="bg-white/80 backdrop-blur-md shadow-sm">
                            <div className="max-w-7xl mx-auto px-4 py-6 sm:px-6 lg:px-8 flex justify-between items-center">
                                <div className="flex items-center space-x-2">
                                    <i data-feather="alert-triangle" className="text-indigo-600 w-8 h-8"></i>
                                    <h1 className="text-2xl font-bold text-gray-800">Incident<span className="text-indigo-600">Ninja</span></h1>
                                </div>
                                <div className="flex items-center space-x-4">
                                    <button 
                                        className="flex items-center space-x-1 text-indigo-600 hover:text-indigo-800"
                                        onClick={() => window.location.reload()}
                                    >
                                        <i data-feather="refresh-cw" className="w-5 h-5"></i>
                                        <span>Refresh</span>
                                    </button>
                                    <div className="relative">
                                        <input 
                                            type="text" 
                                            placeholder="Search incidents..." 
                                            className="pl-8 pr-4 py-2 rounded-lg border border-gray-300 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500"
                                            value={searchTerm}
                                            onChange={(e) => setSearchTerm(e.target.value)}
                                        />
                                        <i data-feather="search" className="absolute left-2 top-2.5 text-gray-400 w-4 h-4"></i>
                                    </div>
                                </div>
                            </div>
                        </header>

                        {/* Main Content */}
                        <main className="max-w-7xl mx-auto px-4 py-8 sm:px-6 lg:px-8">
                            <div className="flex justify-between items-center mb-8">
                                <h2 className="text-xl font-semibold text-gray-800">Recent Incidents</h2>
                                <div className="flex space-x-2">
                                    <div className="relative">
                                        <select 
                                            className="appearance-none pl-3 pr-8 py-2 rounded-lg border border-gray-300 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 bg-white"
                                            value={priorityFilter}
                                            onChange={(e) => setPriorityFilter(e.target.value)}
                                        >
                                            <option value="all">All Priorities</option>
                                            <option value="1">High</option>
                                            <option value="2">Medium</option>
                                            <option value="3">Low</option>
                                        </select>
                                        <i data-feather="chevron-down" className="absolute right-3 top-2.5 text-gray-500 w-4 h-4 pointer-events-none"></i>
                                    </div>
                                    <div className="relative">
                                        <select 
                                            className="appearance-none pl-3 pr-8 py-2 rounded-lg border border-gray-300 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 bg-white"
                                            value={statusFilter}
                                            onChange={(e) => setStatusFilter(e.target.value)}
                                        >
                                            <option value="all">All Statuses</option>
                                            <option value="New">New</option>
                                            <option value="In Progress">In Progress</option>
                                            <option value="Resolved">Resolved</option>
                                            <option value="Closed">Closed</option>
                                        </select>
                                        <i data-feather="chevron-down" className="absolute right-3 top-2.5 text-gray-500 w-4 h-4 pointer-events-none"></i>
                                    </div>
                                </div>
                            </div>

                            {/* Stats Cards */}
                            <div className="grid grid-cols-1 md:grid-cols-4 gap-4 mb-8">
                                <StatsCard 
                                    title="Total Incidents" 
                                    value={stats.total} 
                                    icon="alert-circle" 
                                    color={{ bg: 'bg-indigo-100', text: 'text-indigo-600' }}
                                />
                                <StatsCard 
                                    title="High Priority" 
                                    value={stats.highPriority} 
                                    icon="alert-octagon" 
                                    color={{ bg: 'bg-red-100', text: 'text-red-600' }}
                                />
                                <StatsCard 
                                    title="In Progress" 
                                    value={stats.inProgress} 
                                    icon="clock" 
                                    color={{ bg: 'bg-yellow-100', text: 'text-yellow-600' }}
                                />
                                <StatsCard 
                                    title="Resolved" 
                                    value={stats.resolved} 
                                    icon="check-circle" 
                                    color={{ bg: 'bg-green-100', text: 'text-green-600' }}
                                />
                            </div>

                            {/* Loading State */}
                            {loading && (
                                <div className="flex flex-col items-center justify-center py-12">
                                    <div className="animate-spin border-4 border-indigo-500 border-t-transparent rounded-full w-12 h-12 mb-4"></div>
                                    <p className="text-gray-600">Fetching incidents from ServiceNow...</p>
                                </div>
                            )}

                            {/* Incidents List */}
                            {!loading && (
                                <div className="grid grid-cols-1 gap-4">
                                    {filteredIncidents.length > 0 ? (
                                        filteredIncidents.map(incident => (
                                            <IncidentCard key={incident.id} incident={incident} />
                                        ))
                                    ) : (
                                        <div className="flex flex-col items-center justify-center py-12">
                                            <i data-feather="alert-circle" className="w-16 h-16 text-gray-400 mb-4"></i>
                                            <h3 className="text-lg font-medium text-gray-700 mb-2">No incidents found</h3>
                                            <p className="text-gray-500 text-center max-w-md">
                                                There are currently no incidents matching your criteria. Try adjusting your filters.
                                            </p>
                                        </div>
                                    )}
                                </div>
                            )}
                        </main>
                    </div>

                    {/* Incident Details Modal */}
                    {showModal && selectedIncident && (
                        <div className="fixed inset-0 z-50 overflow-y-auto">
                            <div className="flex items-center justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
                                <div className="fixed inset-0 transition-opacity" aria-hidden="true">
                                    <div className="absolute inset-0 bg-gray-500 opacity-75"></div>
                                </div>
                                <span className="hidden sm:inline-block sm:align-middle sm:h-screen" aria-hidden="true">&#8203;</span>
                                <div className="inline-block align-bottom bg-white rounded-lg text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-2xl sm:w-full">
                                    <div className="bg-white px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
                                        <div className="flex justify-between items-start">
                                            <div>
                                                <h3 className="text-lg leading-6 font-medium text-gray-900">
                                                    {selectedIncident.number}: {selectedIncident.short_description}
                                                </h3>
                                                <div className="mt-1 text-sm text-gray-500">
                                                    Created {formatDate(selectedIncident.created)}
                                                </div>
                                            </div>
                                            <button 
                                                className="text-gray-400 hover:text-gray-500"
                                                onClick={() => setShowModal(false)}
                                            >
                                                <i data-feather="x" className="w-6 h-6"></i>
                                            </button>
                                        </div>
                                        <div className="mt-6 grid grid-cols-1 gap-y-4 gap-x-4 sm:grid-cols-2">
                                            <div>
                                                <label className="block text-sm font-medium text-gray-700">Priority</label>
                                                <div className="mt-1 text-sm text-gray-900">
                                                    <span className={`inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium ${getPriorityBadgeClass(selectedIncident.priority)}`}>
                                                        {getPriorityText(selectedIncident.priority)}
                                                    </span>
                                                </div>
                                            </div>
                                            <div>
                                                <label className="block text-sm font-medium text-gray-700">State</label>
                                                <div className="mt-1 text-sm text-gray-900">
                                                    <span className={`inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium ${getStatusBadgeClass(selectedIncident.state)}`}>
                                                        {selectedIncident.state}
                                                    </span>
                                                </div>
                                            </div>
                                            <div>
                                                <label className="block text-sm font-medium text-gray-700">Created</label>
                                                <div className="mt-1 text-sm text-gray-900">
                                                    {formatDateTime(selectedIncident.created)}
                                                </div>
                                            </div>
                                            <div>
                                                <label className="block text-sm font-medium text-gray-700">Updated</label>
                                                <div className="mt-1 text-sm text-gray-900">
                                                    {formatDateTime(selectedIncident.updated)}
                                                </div>
                                            </div>
                                            <div className="sm:col-span-2">
                                                <label className="block text-sm font-medium text-gray-700">Description</label>
                                                <div className="mt-1 text-sm text-gray-900">
                                                    {selectedIncident.description || 'No description provided'}
                                                </div>
                                            </div>
                                            <div className="sm:col-span-2">
                                                <label className="block text-sm font-medium text-gray-700">Work Notes</label>
                                                <div className="mt-1 text-sm text-gray-900">
                                                    {selectedIncident.work_notes || 'No work notes available'}
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div className="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
                                        {selectedIncident.state !== 'Resolved' && selectedIncident.state !== 'Closed' && (
                                            <button 
                                                type="button" 
                                                className="w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-indigo-600 text-base font-medium text-white hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 sm:ml-3 sm:w-auto sm:text-sm"
                                                onClick={handleResolve}
                                            >
                                                Mark as Resolved
                                            </button>
                                        )}
                                        <button 
                                            type="button" 
                                            className="mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm"
                                            onClick={() => setShowModal(false)}
                                        >
                                            Close
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    )}
                </div>
            );
        };

        // Helper functions
        function getPriorityText(priority) {
            switch(priority) {
                case 1: return 'High';
                case 2: return 'Medium';
                case 3: return 'Low';
                default: return 'Unknown';
            }
        }

        function getPriorityBorderClass(priority) {
            switch(priority) {
                case 1: return 'border-red-500';
                case 2: return 'border-yellow-500';
                case 3: return 'border-green-500';
                default: return 'border-gray-500';
            }
        }

        function getPriorityBadgeClass(priority) {
            switch(priority) {
                case 1: return 'bg-red-100 text-red-800';
                case 2: return 'bg-yellow-100 text-yellow-800';
                case 3: return 'bg-green-100 text-green-800';
                default: return 'bg-gray-100 text-gray-800';
            }
        }

        function getStatusColorClass(status) {
            switch(status) {
                case 'New': return 'text-blue-600';
                case 'In Progress': return 'text-yellow-600';
                case 'Resolved': return 'text-green-600';
                case 'Closed': return 'text-gray-600';
                default: return 'text-gray-600';
            }
        }

        function getStatusBadgeClass(status) {
            switch(status) {
                case 'New': return 'bg-blue-100 text-blue-800';
                case 'In Progress': return 'bg-yellow-100 text-yellow-800';
                case 'Resolved': return 'bg-green-100 text-green-800';
                case 'Closed': return 'bg-gray-100 text-gray-800';
                default: return 'bg-gray-100 text-gray-800';
            }
        }

        function getStatusIcon(status) {
            switch(status) {
                case 'New': return 'plus-circle';
                case 'In Progress': return 'clock';
                case 'Resolved': return 'check-circle';
                case 'Closed': return 'lock';
                default: return 'alert-circle';
            }
        }

        function formatDate(isoString) {
            const date = new Date(isoString);
            return date.toLocaleDateString();
        }

        function formatDateTime(isoString) {
            const date = new Date(isoString);
            return date.toLocaleString();
        }

        ReactDOM.render(<IncidentNinja />, document.getElementById('root'));
    </script>
</body>
</html>