File size: 3,649 Bytes
369b6c9
 
 
b92e226
369b6c9
b92e226
369b6c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b92e226
369b6c9
b92e226
369b6c9
 
 
ea375f7
b92e226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea375f7
b92e226
 
 
 
369b6c9
 
 
 
 
 
 
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
// سجل المشاريع — Saved-project history for a module tool (programme / cashflow).
// Lists the user's saved projects and loads one back into the tool on click.
import React, { useEffect, useState } from 'react';
import { History, Trash2, Loader2, Download } from 'lucide-react';

export default function ModuleHistory({ feature, token, isArabic, onLoad, refreshKey, onExport }) {
  const t = (en, ar) => (isArabic ? ar : en);
  const [items, setItems] = useState([]);
  const [loading, setLoading] = useState(false);
  const [open, setOpen] = useState(false);

  const fetchList = async () => {
    setLoading(true);
    try {
      const res = await fetch(`/api/modules/${feature}`, { headers: { Authorization: `Bearer ${token}` } });
      if (res.ok) setItems(await res.json());
    } catch { /* ignore */ } finally { setLoading(false); }
  };

  useEffect(() => { fetchList(); /* eslint-disable-next-line */ }, [feature, token, refreshKey]);

  const load = async (id) => {
    try {
      const res = await fetch(`/api/modules/${feature}/item/${id}`, { headers: { Authorization: `Bearer ${token}` } });
      if (res.ok) {
        const data = await res.json();
        onLoad({ id: data.id, config: data.config });
      }
    } catch { /* ignore */ }
  };

  const remove = async (id, e) => {
    e.stopPropagation();
    try {
      await fetch(`/api/modules/${feature}/item/${id}`, { method: 'DELETE', headers: { Authorization: `Bearer ${token}` } });
      fetchList();
    } catch { /* ignore */ }
  };

  if (!items.length && !loading) return null;

  return (
    <div style={{ marginBottom: 16 }}>
      <button className="btn btn-secondary" onClick={() => setOpen((o) => !o)} style={{ fontSize: '0.85rem' }}>
        <History size={15} /> {t('Saved Projects', 'المشاريع المحفوظة')} ({items.length}) {open ? '▾' : '▸'}
      </button>
      {open && (
        <div style={{ display: 'flex', flexWrap: 'wrap', gap: 8, marginTop: 10 }}>
          {loading && <Loader2 size={16} className="spin" />}
          {items.map((it) => (
            <div key={it.id} 
              style={{
                display: 'flex', alignItems: 'center', gap: 8,
                background: 'var(--bg-primary)', border: '1px solid var(--border-color)',
                borderRadius: 8, padding: '7px 11px',
              }}>
              <span style={{ color: 'var(--text-primary)', fontWeight: 600, fontSize: '0.84rem' }}>{it.name}</span>
              <span style={{ color: 'var(--text-muted)', fontSize: '0.72rem', marginRight: 'auto', marginLeft: isArabic ? 'auto' : 0 }}>{it.date}</span>
              
              {onExport && (
                <Download 
                  size={15} 
                  style={{ color: 'var(--primary)', cursor: 'pointer', margin: '0 4px' }} 
                  onClick={async (e) => {
                    e.stopPropagation();
                    try {
                      const res = await fetch(`/api/modules/${feature}/item/${it.id}`, { headers: { Authorization: `Bearer ${token}` } });
                      if (res.ok) {
                        const data = await res.json();
                        onExport({ id: data.id, config: data.config });
                      }
                    } catch { /* ignore */ }
                  }}
                  title={isArabic ? 'تنزيل التقرير (Excel)' : 'Download Report (Excel)'}
                />
              )}

              <Trash2 size={15} style={{ color: 'var(--error)', cursor: 'pointer' }} onClick={(e) => remove(it.id, e)} />
            </div>
          ))}
        </div>
      )}
    </div>
  );
}