File size: 23,936 Bytes
afd56bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
import React, { useState, useEffect } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { Search, Plus, Filter, FolderOpen, MoreVertical, Clock, CheckCircle, ChevronRight, Activity, Trash2, Download, Grid, List, Sparkles } from 'lucide-react';
import { useNavigate, useLocation } from 'react-router-dom';
import WizardModal from '../components/dashboard/WizardModal';
import EmptyProjectsState from '../components/dashboard/EmptyProjectsState';
import { useProjectStore } from '../store/useProjectStore';
import { deleteProject } from '../api/client';
import toast from 'react-hot-toast';
import * as XLSX from 'xlsx';
import { analytics } from '../utils/analytics';

const Projects: React.FC = () => {
    const navigate = useNavigate();
    const location = useLocation();
    const { projects, fetchProjects } = useProjectStore();
    const [searchQuery, setSearchQuery] = useState('');
    const [statusFilter, setStatusFilter] = useState('all');
    const [showWizard, setShowWizard] = useState(false);
    const [currentPage, setCurrentPage] = useState(1);
    const [viewMode, setViewMode] = useState<'grid' | 'list'>('grid');
    const [menuOpenId, setMenuOpenId] = useState<string | null>(null);
    const itemsPerPage = 6;

    useEffect(() => {
        fetchProjects();
    }, [fetchProjects]);

    // Obs艂uga powrotu po Stripe Checkout
    useEffect(() => {
        const params = new URLSearchParams(location.search);
        if (params.get('upgraded') === '1' || params.get('mock_success') === '1') {
            const tier = params.get('tier') || 'pro';
            toast.success(
                `馃帀 Plan ${tier === 'pro' ? 'Pro' : 'Business'} aktywny! Limity zosta艂y zwi臋kszone.`,
                { duration: 6000, icon: <Sparkles size={18} color="#10b981" /> }
            );
            analytics.checkoutCompleted(tier);
            // Usu艅 parametry z URL bez reload
            window.history.replaceState({}, '', '/projects');
        }
    }, [location.search]);

    // Filter logic
    const filteredProjects = projects.filter(p => {
        if (statusFilter !== 'all' && p.status !== statusFilter) return false;
        if (searchQuery && !p.title.toLowerCase().includes(searchQuery.toLowerCase())) return false;
        return true;
    });

    const getStatusConfig = (p: any) => {
        let progress = 0;
        if (p.sections && p.sections.length > 0) {
            const approvedCount = p.sections.filter((s: any) => s.is_approved).length;
            progress = Math.round((approvedCount / p.sections.length) * 100);
        } else {
            let baseProgress = 0;
            if (p.external_context) {
                if (p.external_context.region) baseProgress += 10;
                if (p.external_context.company_size) baseProgress += 10;
                if (p.external_context.innovation_scale) baseProgress += 10;
                if (p.external_context.ai_matches && p.external_context.ai_matches.length > 0) baseProgress += 20;
            }
            if (p.title && p.title !== 'Nowy Projekt') baseProgress += 5;
            if (p.description && p.description.length > 10) baseProgress += 15;
            
            progress = Math.min(baseProgress, 99); // max 99 if no sections
            
            if (p.status === 'completed') progress = 100;
        }

        let statusToUse = p.status;
        if (statusToUse === 'draft') {
            const filledCount = p.sections?.filter((s: any) => s.content && s.content.length > 50).length || 0;
            const approvedCount = p.sections?.filter((s: any) => s.is_approved).length || 0;
            if (approvedCount > 0 || filledCount > 0 || progress > 5) statusToUse = 'in_progress';
        }

        switch(statusToUse) {
            case 'draft': return { label: 'Szkic', color: 'var(--text-muted)', bg: 'rgba(255, 255, 255, 0.05)', icon: <Activity size={14}/>, progress };
            case 'in_progress': return { label: 'W Trakcie', color: 'var(--accent-blue)', bg: 'rgba(59, 130, 246, 0.1)', icon: <Activity size={14}/>, progress };
            case 'completed': return { label: 'Zako艅czony (Wygenerowany)', color: 'var(--accent-green)', bg: 'rgba(16, 185, 129, 0.1)', icon: <CheckCircle size={14}/>, progress: 100 };
            default: return { label: 'Nieznany', color: 'var(--text-muted)', bg: 'var(--bg-elevated)', icon: null, progress: 0 };
        }
    };

    const formatDate = (dateString: string) => {
        const d = new Date(dateString);
        return d.toLocaleDateString('pl-PL');
    };

    const handleDelete = async (id: string, e: React.MouseEvent) => {
        e.stopPropagation();
        if (window.confirm("Czy na pewno chcesz usun膮膰 projekt? Ta operacja jest nieodwracalna.")) {
            try {
                await deleteProject(id);
                toast.success("Projekt usuni臋ty");
                fetchProjects();
            } catch (error) {
                toast.error("B艂膮d podczas usuwania projektu");
            }
        }
        setMenuOpenId(null);
    };

    const handleExport = () => {
        const dataToExport = filteredProjects.map(p => ({
            "Nazwa projektu": p.title,
            "Program": p.program_name || 'Brak',
            "Status": getStatusConfig(p).label,
            "Data utworzenia": formatDate(p.created_at),
            "% uko艅czenia": `${getStatusConfig(p).progress}%`,
        }));

        const worksheet = XLSX.utils.json_to_sheet(dataToExport);
        const workbook = XLSX.utils.book_new();
        XLSX.utils.book_append_sheet(workbook, worksheet, "Projekty");
        XLSX.writeFile(workbook, "Moje_Projekty.xlsx");
    };

    return (
        <div style={{ padding: '2rem 3rem', maxWidth: '1400px', margin: '0 auto', width: '100%', height: '100%', overflowY: 'auto' }}>
            
            <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: '3rem' }}>
                <div>
                    <h1 className="display-font" style={{ fontSize: '2.5rem', color: 'var(--text-primary)', marginBottom: '0.5rem', display: 'flex', alignItems: 'center', gap: '0.8rem' }}>
                        <FolderOpen size={36} color="var(--accent-green)" />
                        Moje Projekty
                    </h1>
                    <p style={{ color: 'var(--text-secondary)', fontSize: '1.05rem', marginLeft: '3.4rem' }}>Zarz膮dzaj swoimi wnioskami dotacyjnymi i powracaj do starych sesji RAG.</p>
                </div>
                
                <motion.button 
                    whileHover={{ scale: 1.05, boxShadow: '0 0 25px rgba(16, 185, 129, 0.5)' }}
                    whileTap={{ scale: 0.95 }}
                    className="btn btn-primary"
                    onClick={() => setShowWizard(true)}
                    style={{ padding: '1rem 2rem', fontSize: '1.05rem', background: 'linear-gradient(90deg, var(--accent-green), #3b82f6)', boxShadow: '0 0 20px rgba(16,185,129,0.3)', border: 'none', color: '#000', fontWeight: 800 }}
                >
                    <Plus size={20} /> Nowy Projekt Dotacyjny
                </motion.button>
            </div>

            {/* FILTRY */}
            <div style={{ display: 'flex', gap: '1rem', marginBottom: '2rem', flexWrap: 'wrap' }}>
                <div style={{ position: 'relative', flex: 1, minWidth: '300px' }}>
                    <Search size={18} color="var(--text-muted)" style={{ position: 'absolute', top: '50%', transform: 'translateY(-50%)', left: '1.2rem' }} />
                    <input 
                        type="text" 
                        placeholder="Szukaj projektu po nazwie..." 
                        value={searchQuery}
                        onChange={(e) => setSearchQuery(e.target.value)}
                        style={{ width: '100%', padding: '1rem 1rem 1rem 3rem', background: 'rgba(255,255,255,0.03)', border: '1px solid rgba(255,255,255,0.1)', borderRadius: '12px', color: '#fff', outline: 'none' }}
                    />
                </div>
                <div style={{ display: 'flex', gap: '1rem', alignItems: 'center', background: 'rgba(255,255,255,0.03)', padding: '0.5rem', borderRadius: '12px', border: '1px solid rgba(255,255,255,0.1)' }}>
                    <Filter size={18} color="var(--text-muted)" style={{ marginLeft: '1rem' }} />
                    <select 
                        value={statusFilter} 
                        onChange={(e) => setStatusFilter(e.target.value)}
                        style={{ background: 'transparent', border: 'none', color: '#fff', fontSize: '1rem', padding: '0.5rem 1rem', outline: 'none', cursor: 'pointer' }}
                    >
                        <option value="all" style={{ background: '#0B0E14', color: '#fff' }}>Wszystkie statusy</option>
                        <option value="draft" style={{ background: '#0B0E14', color: '#fff' }}>Szkic</option>
                        <option value="in_progress" style={{ background: '#0B0E14', color: '#fff' }}>W trakcie</option>
                        <option value="completed" style={{ background: '#0B0E14', color: '#fff' }}>Gotowe wnioski</option>
                    </select>
                </div>
                
                <div style={{ display: 'flex', gap: '0.5rem', marginLeft: 'auto', alignItems: 'center' }}>
                    <button className="btn btn-secondary" onClick={handleExport} style={{ display: 'flex', alignItems: 'center', gap: '0.4rem', padding: '0.5rem 1rem', fontSize: '0.9rem' }}>
                        <Download size={16} /> Eksportuj XLSX
                    </button>
                    <div style={{ display: 'flex', background: 'rgba(255,255,255,0.03)', borderRadius: '8px', overflow: 'hidden', border: '1px solid rgba(255,255,255,0.1)' }}>
                        <button 
                            onClick={() => setViewMode('grid')}
                            style={{ background: viewMode === 'grid' ? 'rgba(59,130,246,0.2)' : 'transparent', border: 'none', padding: '0.6rem 0.8rem', color: viewMode === 'grid' ? '#fff' : 'var(--text-muted)', cursor: 'pointer' }}
                        ><Grid size={18} /></button>
                        <button 
                            onClick={() => setViewMode('list')}
                            style={{ background: viewMode === 'list' ? 'rgba(59,130,246,0.2)' : 'transparent', border: 'none', padding: '0.6rem 0.8rem', color: viewMode === 'list' ? '#fff' : 'var(--text-muted)', cursor: 'pointer' }}
                        ><List size={18} /></button>
                    </div>
                </div>
            </div>

            {projects.length === 0 ? (
                <div style={{ display: 'flex', justifyContent: 'center', marginTop: '4rem' }}>
                    <EmptyProjectsState onCreateClick={() => setShowWizard(true)} />
                </div>
            ) : filteredProjects.length === 0 ? (
                <div style={{ display: 'flex', justifyContent: 'center', marginTop: '4rem' }}>
                    <div className="glass-card" style={{ padding: '3rem', textAlign: 'center', color: 'var(--text-muted)' }}>
                        <Search size={48} color="var(--accent-blue)" style={{ opacity: 0.5, marginBottom: '1rem' }} />
                        <h3>Brak wynik贸w wyszukiwania</h3>
                        <p>呕aden projekt nie spe艂nia wybranych kryteri贸w filtra lub wyszukiwania.</p>
                        <button className="btn btn-secondary" onClick={() => { setSearchQuery(''); setStatusFilter('all'); }} style={{ marginTop: '1rem' }}>Zresetuj filtry</button>
                    </div>
                </div>
            ) : viewMode === 'grid' && (
                <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fill, minmax(350px, 1fr))', gap: '1.5rem' }}>
                    <AnimatePresence>
                        {filteredProjects.slice((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage).map((p, i) => {
                            const config = getStatusConfig(p);
                            return (
                                <motion.div 
                                    key={p.id}
                                    initial={{ opacity: 0, y: 20 }}
                                    animate={{ opacity: 1, y: 0 }}
                                    exit={{ opacity: 0, scale: 0.9 }}
                                    transition={{ delay: i * 0.05 }}
                                    className="glass-card"
                                    style={{ padding: '1.5rem', display: 'flex', flexDirection: 'column', position: 'relative' }}
                                >
                                    <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', marginBottom: '1rem' }}>
                                        <div style={{ background: config.bg, color: config.color, padding: '0.4rem 0.8rem', borderRadius: '6px', fontSize: '0.8rem', fontWeight: 800, display: 'flex', alignItems: 'center', gap: '0.4rem' }}>
                                            {config.icon} {config.label}
                                        </div>
                                        <div style={{ position: 'relative' }}>
                                            <div 
                                                onClick={(e) => { e.stopPropagation(); setMenuOpenId(menuOpenId === p.id ? null : p.id); }}
                                                style={{ cursor: 'pointer', padding: '0.2rem' }}
                                            >
                                                <MoreVertical size={20} color="var(--text-muted)" />
                                            </div>
                                            {menuOpenId === p.id && (
                                                <div style={{ position: 'absolute', right: 0, top: '100%', background: '#1c212c', border: '1px solid rgba(255,255,255,0.1)', borderRadius: '8px', padding: '0.5rem', zIndex: 10, minWidth: '150px', boxShadow: '0 4px 12px rgba(0,0,0,0.5)' }}>
                                                    <button 
                                                        onClick={(e) => handleDelete(p.id, e)}
                                                        style={{ display: 'flex', alignItems: 'center', gap: '0.5rem', width: '100%', background: 'transparent', border: 'none', color: '#ef4444', padding: '0.5rem', cursor: 'pointer', textAlign: 'left', borderRadius: '4px', fontSize: '0.9rem' }}
                                                    >
                                                        <Trash2 size={16} /> Usu艅 projekt
                                                    </button>
                                                </div>
                                            )}
                                        </div>
                                    </div>
                                    
                                    <h3 style={{ fontSize: '1.2rem', color: 'var(--text-primary)', marginBottom: '0.5rem', fontWeight: 700, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }} title={p.title}>{p.title}</h3>
                                    
                                    <div style={{ color: 'var(--text-muted)', fontSize: '0.9rem', marginBottom: '1.5rem', display: 'flex', alignItems: 'center', gap: '0.5rem' }}>
                                        <span style={{ color: 'var(--accent-blue)', fontWeight: 600 }}>{p.program_name || "Brak wybranego programu"}</span>
                                    </div>

                                    <div style={{ marginTop: 'auto' }}>
                                        <div style={{ display: 'flex', justifyContent: 'space-between', fontSize: '0.8rem', color: 'var(--text-secondary)', marginBottom: '0.4rem' }}>
                                            <span>Post臋p wniosku (AI)</span>
                                            <span style={{ fontWeight: 800, color: 'var(--text-primary)' }}>{config.progress}%</span>
                                        </div>
                                        <div className="progress-rail" style={{ height: '6px', background: 'rgba(255,255,255,0.1)', borderRadius: '3px' }}>
                                            <motion.div 
                                                className="progress-fill" 
                                                initial={{ width: 0 }} 
                                                animate={{ width: `${config.progress}%` }} 
                                                transition={{ duration: 1, delay: i * 0.1 }}
                                                style={{ background: config.color, borderRadius: '3px' }}
                                            />
                                        </div>
                                    </div>

                                    <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginTop: '1.5rem', borderTop: '1px solid rgba(255,255,255,0.05)', paddingTop: '1.2rem' }}>
                                        <div style={{ display: 'flex', alignItems: 'center', gap: '0.5rem', color: 'var(--text-muted)', fontSize: '0.85rem' }}>
                                            <Clock size={14} /> {formatDate(p.created_at)}
                                        </div>
                                        <motion.button 
                                            whileHover={{ x: 5, color: '#fff' }}
                                            onClick={() => navigate(`/projects/${p.id}`)}
                                            style={{ display: 'flex', alignItems: 'center', gap: '0.4rem', background: 'transparent', border: 'none', color: 'var(--accent-green)', fontWeight: 800, cursor: 'pointer', fontSize: '0.95rem' }}
                                        >
                                            Otw贸rz Projekt <ChevronRight size={18} />
                                        </motion.button>
                                    </div>
                                </motion.div>
                            )
                        })}
                    </AnimatePresence>
                </div>
            )}
            
            {filteredProjects.length > 0 && viewMode === 'list' && (
                <div style={{ background: 'var(--bg-elevated)', borderRadius: '12px', border: '1px solid rgba(255,255,255,0.05)', overflow: 'hidden' }}>
                    <table style={{ width: '100%', borderCollapse: 'collapse', textAlign: 'left', fontSize: '0.95rem' }}>
                        <thead style={{ background: 'rgba(255,255,255,0.02)', borderBottom: '1px solid rgba(255,255,255,0.05)' }}>
                            <tr>
                                <th style={{ padding: '1.2rem', color: 'var(--text-secondary)', fontWeight: 600 }}>Projekt</th>
                                <th style={{ padding: '1.2rem', color: 'var(--text-secondary)', fontWeight: 600 }}>Program</th>
                                <th style={{ padding: '1.2rem', color: 'var(--text-secondary)', fontWeight: 600 }}>Status</th>
                                <th style={{ padding: '1.2rem', color: 'var(--text-secondary)', fontWeight: 600 }}>Data utworzenia</th>
                                <th style={{ padding: '1.2rem', textAlign: 'right', color: 'var(--text-secondary)', fontWeight: 600 }}>Akcje</th>
                            </tr>
                        </thead>
                        <tbody>
                            {filteredProjects.slice((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage).map((p) => {
                                const config = getStatusConfig(p);
                                return (
                                    <tr key={p.id} className="hover-bg-light" style={{ borderBottom: '1px solid rgba(255,255,255,0.02)' }}>
                                        <td style={{ padding: '1rem 1.2rem', color: 'var(--text-primary)', fontWeight: 700 }}>
                                            <div style={{ display: 'flex', alignItems: 'center', gap: '0.8rem' }}>
                                                <div style={{ width: '30px', height: '30px', background: 'rgba(59,130,246,0.1)', borderRadius: '6px', display: 'flex', alignItems: 'center', justifyContent: 'center', color: 'var(--accent-blue)' }}>
                                                    <FolderOpen size={16} />
                                                </div>
                                                <span style={{ whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis', maxWidth: '300px', display: 'inline-block' }} title={p.title}>{p.title}</span>
                                            </div>
                                        </td>
                                        <td style={{ padding: '1rem 1.2rem', color: 'var(--accent-blue)', fontSize: '0.9rem' }}>{p.program_name || '-'}</td>
                                        <td style={{ padding: '1rem 1.2rem' }}>
                                            <div style={{ display: 'inline-flex', background: config.bg, color: config.color, padding: '0.3rem 0.6rem', borderRadius: '4px', fontSize: '0.8rem', fontWeight: 800, alignItems: 'center', gap: '0.3rem' }}>
                                                {config.icon} {config.label}
                                            </div>
                                        </td>
                                        <td style={{ padding: '1rem 1.2rem', color: 'var(--text-muted)', fontSize: '0.9rem' }}>{formatDate(p.created_at)}</td>
                                        <td style={{ padding: '1rem 1.2rem', textAlign: 'right' }}>
                                            <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'flex-end', gap: '1rem' }}>
                                                <button onClick={() => navigate(`/projects/${p.id}`)} style={{ background: 'transparent', border: 'none', color: 'var(--accent-green)', fontWeight: 700, cursor: 'pointer', fontSize: '0.9rem' }}>Ot贸wrz</button>
                                                <button onClick={(e) => handleDelete(p.id, e)} style={{ background: 'transparent', border: 'none', color: '#ef4444', cursor: 'pointer' }}><Trash2 size={18} /></button>
                                            </div>
                                        </td>
                                    </tr>
                                );
                            })}
                        </tbody>
                    </table>
                </div>
            )}
            
            {filteredProjects.length > itemsPerPage && (
                <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', gap: '1rem', marginTop: '3rem' }}>
                    <button 
                        className="btn btn-secondary" 
                        disabled={currentPage === 1}
                        onClick={() => setCurrentPage(p => Math.max(1, p - 1))}
                    >Poprzednia</button>
                    <span style={{ color: 'var(--text-muted)', fontSize: '0.9rem' }}>
                        Strona {currentPage} z {Math.ceil(filteredProjects.length / itemsPerPage)}
                    </span>
                    <button 
                        className="btn btn-secondary" 
                        disabled={currentPage === Math.ceil(filteredProjects.length / itemsPerPage)}
                        onClick={() => setCurrentPage(p => p + 1)}
                    >Nast臋pna</button>
                </div>
            )}

            {showWizard && <WizardModal onClose={() => setShowWizard(false)} />}
        </div>
    );
};

export default Projects;