File size: 17,400 Bytes
b8e6434
 
 
 
8663682
b8e6434
850319d
 
b8e6434
 
 
 
 
cb1e16d
b8e6434
35c7f2f
434a703
850319d
b8e6434
 
 
 
 
 
 
cb1e16d
35c7f2f
850319d
434a703
850319d
b8e6434
850319d
b8e6434
 
8663682
 
 
 
 
 
 
 
 
 
 
 
b8e6434
 
 
 
 
 
 
 
 
 
 
 
c012670
 
850319d
b8e6434
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
850319d
b8e6434
 
850319d
b8e6434
 
 
 
 
 
850319d
b8e6434
 
 
 
 
 
8cbe7e8
850319d
 
cb1e16d
850319d
b8e6434
 
 
 
 
850319d
b8e6434
 
 
35c7f2f
 
 
 
 
 
 
 
b8e6434
35c7f2f
b8e6434
35c7f2f
b8e6434
 
 
35c7f2f
b8e6434
 
 
 
 
 
 
 
 
850319d
b8e6434
 
 
35c7f2f
 
 
 
 
 
 
 
b8e6434
35c7f2f
b8e6434
35c7f2f
b8e6434
 
 
35c7f2f
b8e6434
 
 
 
 
 
 
b143c59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8e6434
 
b143c59
 
 
 
 
 
 
 
b8e6434
b143c59
 
 
 
 
 
 
 
 
 
 
 
 
b8e6434
 
 
 
 
 
 
 
850319d
b8e6434
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8663682
 
 
 
 
 
 
 
b8e6434
d372f15
 
b8e6434
434a703
 
 
 
 
b8e6434
 
 
 
 
434a703
b8e6434
 
 
 
434a703
 
b8e6434
 
 
434a703
b8e6434
 
434a703
 
 
 
 
b8e6434
 
 
 
8663682
 
 
 
 
b8e6434
 
8663682
b8e6434
8663682
b8e6434
 
 
 
 
 
 
 
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import StatCard from "./StatCard";
import { Tender } from "../lib/types";
import { useEffect, useMemo, useState } from "react";
import BrandLoader from "./BrandLoader";
import { searchTenders, fetchDbStatus, syncDatabase, fetchRecommendations } from "../lib/api";

import { translations, Language } from "../lib/translations";

type Props = {
  tendersFound: number;
  recommendedOpportunities: number;
  highRiskItems: number;
  reportsGenerated: number;
  followedTendersCount: number;
  tenders: Tender[];
  onFilterClick?: (type: "sector" | "region", value: string) => void;
  onTenderClick?: (tender: Tender) => void;
  lang: Language;
};

export default function Dashboard({ 
  tendersFound, 
  recommendedOpportunities, 
  highRiskItems, 
  reportsGenerated,
  followedTendersCount,
  tenders,
  onFilterClick,
  onTenderClick,
  lang
}: Props) {
  const t = translations[lang];
  const [isSyncing, setIsSyncing] = useState(false);
  const [dbStatus, setDbStatus] = useState<any>(null);
  const [recommendations, setRecommendations] = useState<Tender[]>([]);
  const [loadingRecs, setLoadingRecs] = useState(true);

  useEffect(() => {
    async function loadRecs() {
      setLoadingRecs(true);
      const recs = await fetchRecommendations();
      setRecommendations(recs);
      setLoadingRecs(false);
    }
    loadRecs();
  }, []);

  useEffect(() => {
    async function loadStatus() {
      const status = await fetchDbStatus();
      setDbStatus(status);
    }
    loadStatus();
  }, [tenders]);

  const handleGlobalSync = async () => {
    setIsSyncing(true);
    try {
      await syncDatabase(); 
      await new Promise(r => setTimeout(r, 1500));
      window.location.reload();
    } catch (e) {
      console.error(e);
    } finally {
      setIsSyncing(false);
    }
  };
  
  const sectorDistribution = useMemo(() => {
    const counts: Record<string, number> = {};
    tenders.forEach(t => {
      const sector = t.sector || "General";
      counts[sector] = (counts[sector] || 0) + 1;
    });
    return Object.entries(counts)
      .sort((a, b) => b[1] - a[1])
      .slice(0, 5);
  }, [tenders]);

  const regionDistribution = useMemo(() => {
    const counts: Record<string, number> = {};
    tenders.forEach(t => {
      const region = t.region || "Sin Regi贸n";
      counts[region] = (counts[region] || 0) + 1;
    });
    return Object.entries(counts)
      .sort((a, b) => b[1] - a[1])
      .slice(0, 5);
  }, [tenders]);

  const deadlineStatus = useMemo(() => {
    const now = new Date();
    const status = {
      urgent: 0,
      near: 0,
      far: 0
    };
    tenders.forEach(t => {
      if (!t.closing_date) return;
      const closing = new Date(t.closing_date);
      const diffDays = Math.ceil((closing.getTime() - now.getTime()) / (1000 * 60 * 60 * 24));
      if (diffDays < 7) status.urgent++;
      else if (diffDays < 21) status.near++;
      else status.far++;
    });
    return status;
  }, [tenders]);

  const totalAmount = useMemo(() => {
    return tenders.reduce((acc, t) => acc + (t.estimated_amount || 0), 0);
  }, [tenders]);

  const formatAmount = (amount: number) => {
    if (amount >= 1_000_000_000) {
      return `$${(amount / 1_000_000_000).toFixed(1)}B`;
    }
    if (amount >= 1_000_000) {
      return `$${(amount / 1_000_000).toFixed(1)}M`;
    }
    return new Intl.NumberFormat("es-CL", { 
      style: "currency", 
      currency: "CLP", 
      maximumFractionDigits: 0 
    }).format(amount);
  };

  return (
    <div className="space-y-8">
      {isSyncing && <BrandLoader />}
      <div className="flex items-center justify-between gap-4">
        <div>
          <p className="text-sm uppercase tracking-[0.35em] text-cyan/80">{t.resumenEjecutivo}</p>
          <h2 className="mt-3 text-4xl font-semibold text-white">AndesOps AI</h2>
          <p className="mt-4 max-w-2xl text-slate-300">
            {t.andesOpsDesc}
          </p>
        </div>
        <button
          onClick={handleGlobalSync}
          className="group relative flex items-center gap-3 overflow-hidden rounded-2xl bg-cyan px-6 py-4 font-bold text-slate-950 transition hover:bg-sky hover:scale-[1.02] active:scale-[0.98]"
        >
          <span className="relative z-10">{t.syncPipeline}</span>
          <span className="text-xl group-hover:rotate-180 transition-transform duration-700">馃攧</span>
          <div className="absolute inset-0 bg-gradient-to-r from-white/20 to-transparent opacity-0 group-hover:opacity-100 transition-opacity" />
        </button>
      </div>

      <div className="grid gap-5 md:grid-cols-2 xl:grid-cols-4">
        <StatCard title={t.tendersFound} value={dbStatus?.total_records || tendersFound} subtitle={t.activeOpps} />
        <StatCard title={t.recommended} value={recommendedOpportunities} subtitle="Fit score > 80%" />
        <StatCard title={t.highRisk} value={highRiskItems} subtitle="Riesgos cr铆ticos" />
        <StatCard title="Portfolio" value={followedTendersCount} subtitle="Opps. guardadas" />
        <StatCard title={t.totalPipeline} value={formatAmount(totalAmount)} subtitle="Monto total proyectado" />
      </div>

      <div className="grid gap-6 lg:grid-cols-3">
        {/* Sector Distribution */}
        <div className="rounded-3xl border border-slate-800 bg-slate-950/80 p-6">
          <h3 className="text-sm uppercase tracking-widest text-slate-400 mb-6 font-semibold">{t.sectors}</h3>
          <div className="space-y-4">
            {sectorDistribution.length > 0 ? (
              sectorDistribution.map(([sector, count]) => (
                <button 
                  key={sector} 
                  onClick={() => onFilterClick?.("sector", sector)}
                  className="w-full text-left group/item focus:outline-none"
                >
                  <div className="flex justify-between text-xs mb-1.5">
                    <span className="text-slate-300 group-hover/item:text-cyan transition-colors">{sector}</span>
                    <span className="text-cyan font-semibold opacity-60 group-hover/item:opacity-100">{count}</span>
                  </div>
                  <div className="h-2 w-full bg-slate-900 rounded-full overflow-hidden border border-white/5">
                    <div 
                      className="h-full bg-cyan transition-all duration-700 group-hover/item:brightness-125" 
                      style={{ width: `${(count / tenders.length) * 100}%` }}
                    />
                  </div>
                </button>
              ))
            ) : (
              <p className="text-slate-500 text-xs italic">Sin datos disponibles.</p>
            )}
          </div>
        </div>

        {/* Region Distribution */}
        <div className="rounded-3xl border border-slate-800 bg-slate-950/80 p-6">
          <h3 className="text-sm uppercase tracking-widest text-slate-400 mb-6 font-semibold">{t.regionalDist}</h3>
          <div className="space-y-4">
            {regionDistribution.length > 0 ? (
              regionDistribution.map(([region, count]) => (
                <button 
                  key={region} 
                  onClick={() => onFilterClick?.("region", region)}
                  className="w-full text-left group/item focus:outline-none"
                >
                  <div className="flex justify-between text-xs mb-1.5">
                    <span className="text-slate-300 group-hover/item:text-sky transition-colors">{region}</span>
                    <span className="text-sky font-semibold opacity-60 group-hover/item:opacity-100">{count}</span>
                  </div>
                  <div className="h-2 w-full bg-slate-900 rounded-full overflow-hidden border border-white/5">
                    <div 
                      className="h-full bg-sky transition-all duration-700 group-hover/item:brightness-125" 
                      style={{ width: `${(count / tenders.length) * 100}%` }}
                    />
                  </div>
                </button>
              ))
            ) : (
              <p className="text-slate-500 text-xs italic">Sin datos disponibles.</p>
            )}
          </div>
        </div>

        {/* Deadline Status - Enhanced Visual */}
        <div className="rounded-3xl border border-white/5 bg-slate-950/80 p-8 relative overflow-hidden group hover:border-purple-500/20 transition-all duration-500">
          <div className="absolute -right-10 -bottom-10 w-40 h-40 bg-purple-500/5 blur-3xl group-hover:bg-purple-500/10 transition-colors" />
          <h3 className="text-[10px] uppercase tracking-[0.2em] text-slate-500 mb-8 font-black">{t.deadlines}</h3>
          
          <div className="flex items-center gap-10">
            <div className="relative w-32 h-32 flex items-center justify-center">
              {/* Complex Radial Background with Multiple Segments via CSS Gradients */}
              <div 
                className="absolute inset-0 rounded-full border-[10px] border-white/5 shadow-inner"
                style={{
                  background: `conic-gradient(
                    #ef4444 0% ${((deadlineStatus.urgent / Math.max(1, tenders.length)) * 100).toFixed(1)}%, 
                    #f59e0b ${((deadlineStatus.urgent / Math.max(1, tenders.length)) * 100).toFixed(1)}% ${(((deadlineStatus.urgent + deadlineStatus.near) / Math.max(1, tenders.length)) * 100).toFixed(1)}%, 
                    #22c55e ${(((deadlineStatus.urgent + deadlineStatus.near) / Math.max(1, tenders.length)) * 100).toFixed(1)}% 100%
                  )`,
                  maskImage: 'radial-gradient(transparent 58%, black 60%)',
                  WebkitMaskImage: 'radial-gradient(transparent 58%, black 60%)'
                }}
              />
              <div className="text-center">
                <div className="text-3xl font-black text-white">{tenders.length}</div>
                <div className="text-[8px] font-bold text-slate-500 uppercase tracking-tighter">Total</div>
              </div>
            </div>

            <div className="flex-1 space-y-4">
              <div className="flex items-center justify-between group/row">
                <div className="flex items-center gap-2">
                  <div className="w-1.5 h-1.5 rounded-full bg-red-500 shadow-[0_0_8px_rgba(239,68,68,0.5)]" />
                  <span className="text-[11px] text-slate-400 group-hover/row:text-red-400 transition-colors">Urgent</span>
                </div>
                <span className="text-xs font-bold text-white">{deadlineStatus.urgent}</span>
              </div>
              <div className="flex items-center justify-between group/row">
                <div className="flex items-center gap-2">
                  <div className="w-1.5 h-1.5 rounded-full bg-amber-500 shadow-[0_0_8px_rgba(245,158,11,0.5)]" />
                  <span className="text-[11px] text-slate-400 group-hover/row:text-amber-400 transition-colors">Near</span>
                </div>
                <span className="text-xs font-bold text-white">{deadlineStatus.near}</span>
              </div>
              <div className="flex items-center justify-between group/row">
                <div className="flex items-center gap-2">
                  <div className="w-1.5 h-1.5 rounded-full bg-green-500 shadow-[0_0_8px_rgba(34,197,94,0.5)]" />
                  <span className="text-[11px] text-slate-400 group-hover/row:text-green-400 transition-colors">Safe</span>
                </div>
                <span className="text-xs font-bold text-white">{deadlineStatus.far}</span>
              </div>
            </div>
          </div>
        </div>

        {/* Database Status Table (New) */}
        <div className="rounded-3xl border border-slate-800 bg-slate-950/80 p-6 flex flex-col justify-between">
          <div>
            <h3 className="text-sm uppercase tracking-widest text-slate-400 mb-6 font-semibold">{t.integrityMonitor}</h3>
            <div className="overflow-hidden rounded-2xl border border-slate-800 bg-slate-900/30">
              <table className="w-full text-left text-[10px]">
                <thead className="bg-slate-800/50 text-slate-500 uppercase font-bold">
                  <tr>
                    <th className="px-4 py-2">Organismo Local</th>
                    <th className="px-4 py-2 text-right">Qty</th>
                  </tr>
                </thead>
                <tbody className="divide-y divide-slate-800/50">
                  {dbStatus?.top_buyers?.map((b: any, i: number) => (
                    <tr key={i} className="hover:bg-slate-800/30">
                      <td className="px-4 py-3 text-slate-300 truncate max-w-[120px]">{b.name}</td>
                      <td className="px-4 py-3 text-right text-cyan font-mono">{b.count}</td>
                    </tr>
                  ))}
                  {!dbStatus?.top_buyers?.length && (
                    <tr>
                      <td colSpan={2} className="px-4 py-6 text-center text-slate-600 italic">No local data found.</td>
                    </tr>
                  )}
                </tbody>
              </table>
            </div>
          </div>
          
          <div className="mt-6 pt-6 border-t border-slate-800/50">
            <div className="flex justify-between items-center text-[10px]">
              <span className="text-slate-500 font-bold uppercase tracking-tighter">Total Local Tenders:</span>
              <span className="text-white font-mono">{dbStatus?.total_records || 0}</span>
            </div>
            <div className="flex justify-between items-center text-[10px] mt-2">
              <span className="text-slate-500 font-bold uppercase tracking-tighter">Last Pulse:</span>
              <span className="text-cyan font-mono">{dbStatus?.last_sync ? new Date(dbStatus.last_sync).toLocaleTimeString() : 'Never'}</span>
            </div>
          </div>
        </div>
      </div>

      <div className="rounded-3xl border border-slate-800 bg-slate-950/80 p-6 relative overflow-hidden group">
        <div className="absolute top-0 right-0 p-4 opacity-5 group-hover:opacity-20 transition-opacity">
          <span className="text-4xl">馃</span>
        </div>
        <h3 className="text-sm uppercase tracking-widest text-indigo-400 mb-6 font-black flex items-center gap-2">
          <span className="w-2 h-2 rounded-full bg-indigo-500 animate-pulse" />
          IA Recommendations for your Company
        </h3>
        <div className="space-y-3">
          {(recommendations.length > 0 || tenders.length > 0) ? (
            (recommendations.length > 0 ? recommendations : tenders).slice(0, 6).map((t) => (
              // ... existing map logic ...
              <div 
                key={t.code} 
                onClick={() => onTenderClick?.(t)}
                className="flex items-center justify-between p-4 rounded-2xl bg-slate-900/40 border border-slate-800/50 hover:bg-slate-900/60 transition group cursor-pointer"
              >
                <div className="flex items-center gap-4 overflow-hidden">
                  <div className="h-10 w-10 flex-shrink-0 rounded-full bg-slate-800 flex items-center justify-center text-cyan group-hover:scale-110 transition">
                    {t.sector?.charAt(0) || "T"}
                  </div>
                  <div className="overflow-hidden">
                    <div className="text-sm font-medium text-white truncate max-w-[200px] md:max-w-[400px]">{t.name}</div>
                    <div className="text-xs text-slate-500 truncate">{t.buyer}</div>
                  </div>
                </div>
                <div className="flex items-center gap-6 text-right">
                  <div className="hidden lg:block min-w-[100px]">
                    <div className="text-[10px] uppercase font-bold text-slate-500">Regi贸n</div>
                    <div className="text-xs text-slate-300">{t.region || "N/A"}</div>
                  </div>
                  <div className="min-w-[80px]">
                    <div className="text-[10px] uppercase font-bold text-slate-500">C贸digo</div>
                    <div className="text-xs font-mono text-cyan">{t.code}</div>
                  </div>
                  <button 
                    className="px-4 py-2 rounded-xl bg-cyan/10 border border-cyan/20 text-[10px] font-black text-cyan uppercase tracking-widest group-hover:bg-cyan group-hover:text-black transition-all"
                  >
                    View
                  </button>
                </div>
              </div>
            ))
          ) : (
            <div className="flex flex-col items-center justify-center py-20 text-center">
              <div className="w-16 h-16 rounded-full bg-slate-900 flex items-center justify-center mb-4 text-2xl">馃摗</div>
              <p className="text-slate-500 text-sm italic mb-6 max-w-xs">
                No local data found yet. Sync with Mercado P煤blico to feed the Intelligence Pipeline.
              </p>
              <button
                onClick={handleGlobalSync}
                className="group relative px-8 py-3 rounded-2xl bg-cyan text-slate-950 font-bold hover:bg-sky transition-all active:scale-95 flex items-center gap-2"
              >
                <span>馃摜 Sync Real Data Now</span>
              </button>
            </div>
          )}
        </div>
      </div>
    </div>
  );
}