page.tsx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. "use client";
  2. import { useState, useEffect } from 'react';
  3. import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
  4. import { Button } from '@/components/ui/button';
  5. import { Upload, FileText, Database, BarChart3, CheckCircle, Loader2, History, PlusCircle } from 'lucide-react';
  6. import { UploadForm } from '@/app/components/uploadForm';
  7. import { createCintasImportRecord, processCintasImportData } from '@/app/actions/cintas-workflow';
  8. import { getImports } from '@/app/actions/imports';
  9. import { useKindeBrowserClient } from "@kinde-oss/kinde-auth-nextjs";
  10. interface FileData {
  11. id: string;
  12. filename: string;
  13. mimetype: string;
  14. size: number;
  15. createdAt: string;
  16. updatedAt: string;
  17. }
  18. interface CintasSummary {
  19. id: number;
  20. week: string;
  21. trrTotal: number;
  22. fourWkAverages: number;
  23. trrPlus4Wk: number;
  24. powerAdds: number;
  25. weekId: number;
  26. }
  27. interface ImportRecord {
  28. id: number;
  29. name: string;
  30. importDate: string;
  31. fileId: string | null;
  32. file?: {
  33. filename: string;
  34. createdAt: string;
  35. };
  36. layout?: {
  37. name: string;
  38. };
  39. cintasSummaries?: CintasSummary[];
  40. }
  41. export default function CintasCalendarSummaryPage() {
  42. const { user } = useKindeBrowserClient();
  43. const [viewMode, setViewMode] = useState<'imports' | 'new-import' | 'summary'>('imports');
  44. const [currentStep, setCurrentStep] = useState(1);
  45. const [uploadedFile, setUploadedFile] = useState<FileData | null>(null);
  46. const [isProcessing, setIsProcessing] = useState(false);
  47. const [importRecord, setImportRecord] = useState<any>(null);
  48. const [summaryData, setSummaryData] = useState<CintasSummary[]>([]);
  49. const [error, setError] = useState<string | null>(null);
  50. const [summaryExists, setSummaryExists] = useState(false);
  51. const [priorImports, setPriorImports] = useState<ImportRecord[]>([]);
  52. const [selectedImport, setSelectedImport] = useState<ImportRecord | null>(null);
  53. const [loadingImports, setLoadingImports] = useState(true);
  54. useEffect(() => {
  55. if (user) {
  56. loadPriorImports();
  57. }
  58. }, [user]);
  59. const loadPriorImports = async () => {
  60. try {
  61. setLoadingImports(true);
  62. setError(null); // Clear any previous errors
  63. if (!user?.id) {
  64. // Don't set error here, just return - user might still be loading
  65. return;
  66. }
  67. const result = await getImports(user.id);
  68. if (result.success && result.data) {
  69. // Map the data to match our ImportRecord interface
  70. const mappedData = result.data.map((item: any) => ({
  71. id: item.id,
  72. name: item.name,
  73. importDate: item.importDate,
  74. fileId: item.fileId,
  75. file: item.file ? {
  76. filename: item.file.filename,
  77. createdAt: item.file.createdAt
  78. } : undefined,
  79. cintasSummaries: item.cintasSummaries || []
  80. }));
  81. setPriorImports(mappedData);
  82. } else {
  83. setError(result.error || 'Failed to load prior imports');
  84. setPriorImports([]);
  85. }
  86. } catch (err) {
  87. setError(err instanceof Error ? err.message : 'Failed to load prior imports');
  88. setPriorImports([]);
  89. } finally {
  90. setLoadingImports(false);
  91. }
  92. };
  93. const handleFileUploaded = (file: FileData) => {
  94. setUploadedFile(file);
  95. setCurrentStep(2);
  96. setError(null);
  97. };
  98. const handleCreateImportRecord = async () => {
  99. if (!uploadedFile) return;
  100. setIsProcessing(true);
  101. setError(null);
  102. try {
  103. const result = await createCintasImportRecord(uploadedFile.id, uploadedFile.filename);
  104. if (result.success) {
  105. setImportRecord(result.data);
  106. setCurrentStep(3);
  107. } else {
  108. setError(result.error || 'Failed to create import record');
  109. }
  110. } catch (err) {
  111. setError(err instanceof Error ? err.message : 'Unknown error occurred');
  112. } finally {
  113. setIsProcessing(false);
  114. }
  115. };
  116. const handleProcessImportData = async () => {
  117. if (!importRecord) return;
  118. setIsProcessing(true);
  119. setError(null);
  120. try {
  121. const result = await processCintasImportData(importRecord.id);
  122. if (result.success) {
  123. setCurrentStep(4);
  124. } else {
  125. setError(result.error || 'Failed to process import data');
  126. }
  127. } catch (err) {
  128. setError(err instanceof Error ? err.message : 'Unknown error occurred');
  129. } finally {
  130. setIsProcessing(false);
  131. }
  132. };
  133. const handleGenerateSummary = async () => {
  134. if (!importRecord) return;
  135. setIsProcessing(true);
  136. setError(null);
  137. try {
  138. const response = await fetch(`/api/imports/${importRecord.id}/summary`, {
  139. method: 'POST',
  140. headers: {
  141. 'Content-Type': 'application/json',
  142. },
  143. });
  144. const data = await response.json();
  145. if (response.ok) {
  146. setSummaryData(data.summary || []);
  147. setSummaryExists(true);
  148. if (data.summaryGenerated) {
  149. setCurrentStep(4);
  150. setViewMode('summary');
  151. }
  152. } else {
  153. throw new Error(data.error || 'Failed to generate summary');
  154. }
  155. } catch (err) {
  156. setError(err instanceof Error ? err.message : 'Failed to generate summary');
  157. } finally {
  158. setIsProcessing(false);
  159. }
  160. };
  161. const handleLoadImportSummary = async (importRecord: ImportRecord) => {
  162. try {
  163. setIsProcessing(true);
  164. setError(null);
  165. const response = await fetch(`/api/imports/${importRecord.id}/summary`);
  166. const data = await response.json();
  167. if (response.ok) {
  168. setSelectedImport(importRecord);
  169. setSummaryData(data.summary?.cintasSummaries || []);
  170. setSummaryExists(data.summaryExists);
  171. setViewMode('summary');
  172. } else {
  173. throw new Error(data.error || 'Failed to load summary');
  174. }
  175. } catch (err) {
  176. setError(err instanceof Error ? err.message : 'Failed to load summary');
  177. } finally {
  178. setIsProcessing(false);
  179. }
  180. };
  181. const handleStartNewImport = () => {
  182. setViewMode('new-import');
  183. setCurrentStep(1);
  184. setUploadedFile(null);
  185. setImportRecord(null);
  186. setSummaryData([]);
  187. setSummaryExists(false);
  188. setError(null);
  189. };
  190. const handleBackToImports = () => {
  191. setViewMode('imports');
  192. setSelectedImport(null);
  193. setSummaryData([]);
  194. loadPriorImports();
  195. };
  196. const formatDate = (dateString: string) => {
  197. return new Date(dateString).toLocaleDateString('en-US', {
  198. year: 'numeric',
  199. month: 'short',
  200. day: 'numeric',
  201. hour: '2-digit',
  202. minute: '2-digit'
  203. });
  204. };
  205. return (
  206. <div className="min-h-screen bg-gradient-to-br from-gray-50 to-gray-100 dark:from-gray-900 dark:to-gray-800">
  207. <div className="container mx-auto py-6 px-4 max-w-6xl">
  208. <div className="mb-8">
  209. <h1 className="text-3xl font-bold tracking-tight text-gray-900 dark:text-white">Cintas Install Calendar Summary</h1>
  210. <p className="text-muted-foreground dark:text-gray-300">
  211. View prior imports or create new ones to process installation calendar data
  212. </p>
  213. </div>
  214. {error && (
  215. <div className="mb-4 p-4 bg-red-50 border border-red-200 rounded-lg">
  216. <p className="text-sm text-red-800">{error}</p>
  217. </div>
  218. )}
  219. {/* Navigation Buttons */}
  220. <div className="mb-6 flex gap-4">
  221. <Button
  222. variant={viewMode === 'imports' ? 'default' : 'outline'}
  223. onClick={handleBackToImports}
  224. className="flex items-center gap-2"
  225. >
  226. <History className="h-4 w-4" />
  227. Prior Imports
  228. </Button>
  229. <Button
  230. variant={viewMode === 'new-import' ? 'default' : 'outline'}
  231. onClick={handleStartNewImport}
  232. className="flex items-center gap-2"
  233. >
  234. <PlusCircle className="h-4 w-4" />
  235. New Import
  236. </Button>
  237. </div>
  238. {/* Prior Imports View */}
  239. {viewMode === 'imports' && (
  240. <Card>
  241. <CardHeader>
  242. <CardTitle>Prior Imports</CardTitle>
  243. <CardDescription>
  244. Select an import to view its summary results
  245. </CardDescription>
  246. </CardHeader>
  247. <CardContent>
  248. {loadingImports ? (
  249. <div className="flex justify-center py-8">
  250. <Loader2 className="h-8 w-8 animate-spin" />
  251. </div>
  252. ) : priorImports.length === 0 ? (
  253. <div className="text-center py-8">
  254. <p className="text-muted-foreground mb-4">
  255. {user ? 'No prior imports found' : 'Please sign in to view imports'}
  256. </p>
  257. {user && (
  258. <Button onClick={handleStartNewImport}>
  259. Create First Import
  260. </Button>
  261. )}
  262. </div>
  263. ) : (
  264. <div className="space-y-4">
  265. {priorImports.map((importRecord) => (
  266. <div
  267. key={importRecord.id}
  268. className="border rounded-lg p-4 hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors cursor-pointer"
  269. onClick={() => handleLoadImportSummary(importRecord)}
  270. >
  271. <div className="flex justify-between items-start">
  272. <div>
  273. <h3 className="font-semibold">{importRecord.name}</h3>
  274. <p className="text-sm text-muted-foreground">
  275. {formatDate(importRecord.importDate)}
  276. </p>
  277. {importRecord.file && (
  278. <p className="text-sm text-muted-foreground">
  279. File: {importRecord.file.filename}
  280. </p>
  281. )}
  282. </div>
  283. </div>
  284. </div>
  285. ))}
  286. </div>
  287. )}
  288. </CardContent>
  289. </Card>
  290. )}
  291. {/* New Import Workflow */}
  292. {viewMode === 'new-import' && (
  293. <div className="space-y-6">
  294. {/* Workflow Steps */}
  295. <div className="grid grid-cols-1 md:grid-cols-4 gap-4">
  296. {[
  297. {
  298. id: 1,
  299. title: 'Upload Excel File',
  300. description: 'Upload the Cintas Install Calendar Excel file',
  301. icon: Upload,
  302. status: currentStep >= 1 ? (uploadedFile ? 'completed' : 'pending') : 'pending',
  303. },
  304. {
  305. id: 2,
  306. title: 'Create Import Record',
  307. description: 'Create import record with layout configuration',
  308. icon: FileText,
  309. status: currentStep >= 2 ? (importRecord ? 'completed' : 'pending') : 'disabled',
  310. },
  311. {
  312. id: 3,
  313. title: 'Import Data',
  314. description: 'Process Excel file and import data',
  315. icon: Database,
  316. status: currentStep > 3 ? 'completed' : (currentStep === 3 ? 'pending' : 'disabled'),
  317. },
  318. {
  319. id: 4,
  320. title: 'Generate Summary',
  321. description: 'Run summary calculations and display results',
  322. icon: BarChart3,
  323. status: currentStep >= 4 ? (summaryData.length > 0 ? 'completed' : 'pending') : 'disabled',
  324. },
  325. ].map((step) => {
  326. const Icon = step.icon;
  327. const getStatusColor = (status: string) => {
  328. switch (status) {
  329. case 'completed':
  330. return 'text-green-600 bg-green-50 border-green-200';
  331. case 'pending':
  332. return 'text-blue-600 bg-blue-50 border-blue-200';
  333. case 'disabled':
  334. return 'text-gray-400 bg-gray-50 border-gray-200';
  335. default:
  336. return 'text-gray-600 bg-gray-50 border-gray-200';
  337. }
  338. };
  339. return (
  340. <Card
  341. key={step.id}
  342. className={`border-2 ${getStatusColor(step.status)}`}
  343. >
  344. <CardHeader className="pb-3">
  345. <div className="flex items-center space-x-2">
  346. <Icon className="h-5 w-5" />
  347. <div>
  348. <CardTitle className="text-sm font-medium">
  349. Step {step.id}: {step.title}
  350. </CardTitle>
  351. </div>
  352. </div>
  353. </CardHeader>
  354. <CardContent>
  355. <CardDescription className="text-xs">
  356. {step.description}
  357. </CardDescription>
  358. </CardContent>
  359. </Card>
  360. );
  361. })}
  362. </div>
  363. {/* Step Content */}
  364. <div className="space-y-6">
  365. {currentStep === 1 && (
  366. <Card>
  367. <CardHeader>
  368. <CardTitle>Step 1: Upload Excel File</CardTitle>
  369. <CardDescription>
  370. Upload your Cintas Install Calendar Excel file to begin processing
  371. </CardDescription>
  372. </CardHeader>
  373. <CardContent>
  374. <div className="space-y-4">
  375. <UploadForm onFileUploaded={handleFileUploaded} />
  376. {uploadedFile && (
  377. <div className="mt-4 p-4 bg-green-50 border border-green-200 rounded-lg">
  378. <div className="flex items-center space-x-2">
  379. <CheckCircle className="h-5 w-5 text-green-600" />
  380. <div>
  381. <p className="text-sm font-medium text-green-800">File uploaded successfully!</p>
  382. <p className="text-sm text-green-600">
  383. {uploadedFile.filename} ({(uploadedFile.size / 1024 / 1024).toFixed(2)} MB)
  384. </p>
  385. </div>
  386. </div>
  387. </div>
  388. )}
  389. </div>
  390. </CardContent>
  391. </Card>
  392. )}
  393. {currentStep === 2 && (
  394. <Card>
  395. <CardHeader>
  396. <CardTitle>Step 2: Create Import Record</CardTitle>
  397. <CardDescription>
  398. Creating import record with Cintas Install Calendar layout configuration
  399. </CardDescription>
  400. </CardHeader>
  401. <CardContent>
  402. <div className="space-y-4">
  403. <p className="text-sm text-muted-foreground">
  404. File: {uploadedFile?.filename}
  405. </p>
  406. <Button
  407. onClick={handleCreateImportRecord}
  408. disabled={isProcessing}
  409. className="w-full"
  410. >
  411. {isProcessing ? (
  412. <>
  413. <Loader2 className="mr-2 h-4 w-4 animate-spin" />
  414. Creating Import Record...
  415. </>
  416. ) : (
  417. 'Create Import Record'
  418. )}
  419. </Button>
  420. </div>
  421. </CardContent>
  422. </Card>
  423. )}
  424. {currentStep === 3 && (
  425. <Card>
  426. <CardHeader>
  427. <CardTitle>Step 3: Import Data</CardTitle>
  428. <CardDescription>
  429. Processing Excel file and importing data into database
  430. </CardDescription>
  431. </CardHeader>
  432. <CardContent>
  433. <div className="space-y-4">
  434. <p className="text-sm text-muted-foreground">
  435. Import ID: {importRecord?.id}
  436. </p>
  437. <Button
  438. onClick={handleProcessImportData}
  439. disabled={isProcessing}
  440. className="w-full"
  441. >
  442. {isProcessing ? (
  443. <>
  444. <Loader2 className="mr-2 h-4 w-4 animate-spin" />
  445. Processing Import...
  446. </>
  447. ) : (
  448. 'Process Import'
  449. )}
  450. </Button>
  451. </div>
  452. </CardContent>
  453. </Card>
  454. )}
  455. {currentStep === 4 && (
  456. <Card>
  457. <CardHeader>
  458. <CardTitle>Step 4: Generate Summary</CardTitle>
  459. <CardDescription>
  460. Running summary calculations and displaying results
  461. </CardDescription>
  462. </CardHeader>
  463. <CardContent>
  464. <div className="space-y-4">
  465. <p className="text-sm text-muted-foreground">
  466. Import ID: {importRecord?.id}
  467. </p>
  468. <Button
  469. onClick={handleGenerateSummary}
  470. disabled={isProcessing}
  471. className="w-full"
  472. >
  473. {isProcessing ? (
  474. <>
  475. <Loader2 className="mr-2 h-4 w-4 animate-spin" />
  476. {summaryExists ? 'Loading Summary...' : 'Generating Summary...'}
  477. </>
  478. ) : (
  479. summaryExists ? 'Load Summary' : 'Generate Summary'
  480. )}
  481. </Button>
  482. </div>
  483. </CardContent>
  484. </Card>
  485. )}
  486. </div>
  487. </div>
  488. )}
  489. {/* Summary Results View */}
  490. {viewMode === 'summary' && selectedImport && (
  491. <Card>
  492. <CardHeader>
  493. <div className="flex justify-between items-start">
  494. <div>
  495. <CardTitle>Summary Results</CardTitle>
  496. <CardDescription>
  497. {selectedImport.name} - {formatDate(selectedImport.importDate)}
  498. </CardDescription>
  499. </div>
  500. <Button
  501. variant="outline"
  502. onClick={handleBackToImports}
  503. className="flex items-center gap-2"
  504. >
  505. <History className="h-4 w-4" />
  506. Back to Imports
  507. </Button>
  508. </div>
  509. </CardHeader>
  510. <CardContent>
  511. {isProcessing ? (
  512. <div className="flex justify-center py-8">
  513. <Loader2 className="h-8 w-8 animate-spin" />
  514. </div>
  515. ) : summaryData.length === 0 ? (
  516. <div className="text-center py-8">
  517. <p className="text-muted-foreground mb-4">No summary data available</p>
  518. <Button onClick={() => handleGenerateSummary()}>
  519. Generate Summary
  520. </Button>
  521. </div>
  522. ) : (
  523. <div className="overflow-x-auto">
  524. <table className="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
  525. <thead className="bg-gray-50 dark:bg-gray-800">
  526. <tr>
  527. <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Week</th>
  528. <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">TRR Total</th>
  529. <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">4 Week Avg</th>
  530. <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">TRR + 4Wk</th>
  531. <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">Power Adds</th>
  532. </tr>
  533. </thead>
  534. <tbody className="bg-white divide-y divide-gray-200 dark:bg-gray-800 dark:divide-gray-700">
  535. {summaryData.map((item, index) => (
  536. <tr
  537. key={`${item.id}-${index}`}
  538. className={index % 2 === 0
  539. ? 'bg-white dark:bg-gray-800'
  540. : 'bg-gray-50 dark:bg-gray-700/50'
  541. }
  542. >
  543. <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-900 dark:text-gray-100">{item.week}</td>
  544. <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-900 dark:text-gray-100">{item.trrTotal}</td>
  545. <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-900 dark:text-gray-100">{item.fourWkAverages}</td>
  546. <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-900 dark:text-gray-100">{item.trrPlus4Wk}</td>
  547. <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-900 dark:text-gray-100">{item.powerAdds}</td>
  548. </tr>
  549. ))}
  550. </tbody>
  551. </table>
  552. </div>
  553. )}
  554. </CardContent>
  555. </Card>
  556. )}
  557. </div>
  558. </div>
  559. );
  560. }