page.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. "use client";
  2. import { useState } 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 } from 'lucide-react';
  6. import { UploadForm } from '@/app/components/uploadForm';
  7. import { createCintasImportRecord, processCintasImportData } from '@/app/actions/cintas-workflow';
  8. interface FileData {
  9. id: string;
  10. filename: string;
  11. mimetype: string;
  12. size: number;
  13. createdAt: string;
  14. updatedAt: string;
  15. }
  16. interface CintasSummary {
  17. id: number;
  18. week: string;
  19. trrTotal: number;
  20. fourWkAverages: number;
  21. trrPlus4Wk: number;
  22. powerAdds: number;
  23. weekId: number;
  24. }
  25. export default function CintasCalendarSummaryPage() {
  26. const [currentStep, setCurrentStep] = useState(1);
  27. const [uploadedFile, setUploadedFile] = useState<FileData | null>(null);
  28. const [isProcessing, setIsProcessing] = useState(false);
  29. const [importRecord, setImportRecord] = useState<any>(null);
  30. const [summaryData, setSummaryData] = useState<CintasSummary[]>([]);
  31. const [error, setError] = useState<string | null>(null);
  32. const handleFileUploaded = (file: FileData) => {
  33. setUploadedFile(file);
  34. setCurrentStep(2);
  35. setError(null);
  36. };
  37. const handleCreateImportRecord = async () => {
  38. if (!uploadedFile) return;
  39. setIsProcessing(true);
  40. setError(null);
  41. try {
  42. const result = await createCintasImportRecord(uploadedFile.id, uploadedFile.filename);
  43. if (result.success) {
  44. setImportRecord(result.data);
  45. setCurrentStep(3);
  46. } else {
  47. setError(result.error || 'Failed to create import record');
  48. }
  49. } catch (err) {
  50. setError(err instanceof Error ? err.message : 'Unknown error occurred');
  51. } finally {
  52. setIsProcessing(false);
  53. }
  54. };
  55. const handleProcessImportData = async () => {
  56. if (!importRecord) return;
  57. setIsProcessing(true);
  58. setError(null);
  59. try {
  60. const result = await processCintasImportData(importRecord.id);
  61. if (result.success) {
  62. setCurrentStep(4);
  63. // After processing, fetch the summary data
  64. await handleGenerateSummary();
  65. } else {
  66. setError(result.error || 'Failed to process import data');
  67. }
  68. } catch (err) {
  69. setError(err instanceof Error ? err.message : 'Unknown error occurred');
  70. } finally {
  71. setIsProcessing(false);
  72. }
  73. };
  74. const handleGenerateSummary = async () => {
  75. if (!importRecord) return;
  76. setIsProcessing(true);
  77. setError(null);
  78. try {
  79. // This would typically call an API endpoint to run the stored procedure
  80. // For now, we'll simulate the summary generation
  81. const response = await fetch(`/api/imports/${importRecord.id}/summary`);
  82. if (response.ok) {
  83. const data = await response.json();
  84. setSummaryData(data);
  85. } else {
  86. throw new Error('Failed to generate summary');
  87. }
  88. } catch (err) {
  89. setError(err instanceof Error ? err.message : 'Failed to generate summary');
  90. } finally {
  91. setIsProcessing(false);
  92. }
  93. };
  94. const steps = [
  95. {
  96. id: 1,
  97. title: 'Upload Excel File',
  98. description: 'Upload the Cintas Install Calendar Excel file to blob storage',
  99. icon: Upload,
  100. status: currentStep >= 1 ? (uploadedFile ? 'completed' : 'pending') : 'pending',
  101. },
  102. {
  103. id: 2,
  104. title: 'Create Import Record',
  105. description: 'Create an import record with Cintas Install Calendar layout configuration',
  106. icon: FileText,
  107. status: currentStep >= 2 ? (importRecord ? 'completed' : 'pending') : 'disabled',
  108. },
  109. {
  110. id: 3,
  111. title: 'Import Data',
  112. description: 'Read the Excel file and import data into PostgreSQL database',
  113. icon: Database,
  114. status: currentStep >= 3 ? (summaryData.length > 0 ? 'completed' : 'pending') : 'disabled',
  115. },
  116. {
  117. id: 4,
  118. title: 'Generate Summary',
  119. description: 'Run summary calculations and display results',
  120. icon: BarChart3,
  121. status: currentStep >= 4 ? (summaryData.length > 0 ? 'completed' : 'pending') : 'disabled',
  122. },
  123. ];
  124. const getStepStatusColor = (status: string) => {
  125. switch (status) {
  126. case 'completed':
  127. return 'text-green-600 bg-green-50 border-green-200';
  128. case 'pending':
  129. return 'text-blue-600 bg-blue-50 border-blue-200';
  130. case 'disabled':
  131. return 'text-gray-400 bg-gray-50 border-gray-200';
  132. default:
  133. return 'text-gray-600 bg-gray-50 border-gray-200';
  134. }
  135. };
  136. return (
  137. <div className="container mx-auto py-6 px-4 max-w-6xl">
  138. <div className="mb-8">
  139. <h1 className="text-3xl font-bold tracking-tight">Cintas Install Calendar Summary</h1>
  140. <p className="text-muted-foreground">
  141. Follow the workflow steps to upload and process the installation calendar data
  142. </p>
  143. </div>
  144. {error && (
  145. <div className="mb-4 p-4 bg-red-50 border border-red-200 rounded-lg">
  146. <p className="text-sm text-red-800">{error}</p>
  147. </div>
  148. )}
  149. {/* Workflow Steps */}
  150. <div className="mb-8">
  151. <div className="grid grid-cols-1 md:grid-cols-4 gap-4">
  152. {steps.map((step) => {
  153. const Icon = step.icon;
  154. return (
  155. <Card
  156. key={step.id}
  157. className={`border-2 ${getStepStatusColor(step.status)}`}
  158. >
  159. <CardHeader className="pb-3">
  160. <div className="flex items-center space-x-2">
  161. <Icon className="h-5 w-5" />
  162. <div>
  163. <CardTitle className="text-sm font-medium">
  164. Step {step.id}: {step.title}
  165. </CardTitle>
  166. </div>
  167. </div>
  168. </CardHeader>
  169. <CardContent>
  170. <CardDescription className="text-xs">
  171. {step.description}
  172. </CardDescription>
  173. </CardContent>
  174. </Card>
  175. );
  176. })}
  177. </div>
  178. </div>
  179. {/* Step Content */}
  180. <div className="grid gap-6">
  181. {currentStep === 1 && (
  182. <Card>
  183. <CardHeader>
  184. <CardTitle>Step 1: Upload Excel File</CardTitle>
  185. <CardDescription>
  186. Upload your Cintas Install Calendar Excel file to begin processing
  187. </CardDescription>
  188. </CardHeader>
  189. <CardContent>
  190. <div className="space-y-4">
  191. <UploadForm onFileUploaded={handleFileUploaded} />
  192. {uploadedFile && (
  193. <div className="mt-4 p-4 bg-green-50 border border-green-200 rounded-lg">
  194. <div className="flex items-center space-x-2">
  195. <CheckCircle className="h-5 w-5 text-green-600" />
  196. <div>
  197. <p className="text-sm font-medium text-green-800">File uploaded successfully!</p>
  198. <p className="text-sm text-green-600">
  199. {uploadedFile.filename} ({(uploadedFile.size / 1024 / 1024).toFixed(2)} MB)
  200. </p>
  201. </div>
  202. </div>
  203. </div>
  204. )}
  205. </div>
  206. </CardContent>
  207. </Card>
  208. )}
  209. {currentStep === 2 && (
  210. <Card>
  211. <CardHeader>
  212. <CardTitle>Step 2: Create Import Record</CardTitle>
  213. <CardDescription>
  214. Creating import record with Cintas Install Calendar layout configuration
  215. </CardDescription>
  216. </CardHeader>
  217. <CardContent>
  218. <div className="space-y-4">
  219. <p className="text-sm text-muted-foreground">
  220. File: {uploadedFile?.filename}
  221. </p>
  222. <Button
  223. onClick={handleCreateImportRecord}
  224. disabled={isProcessing}
  225. className="w-full"
  226. >
  227. {isProcessing ? (
  228. <>
  229. <Loader2 className="mr-2 h-4 w-4 animate-spin" />
  230. Creating Import Record...
  231. </>
  232. ) : (
  233. 'Create Import Record'
  234. )}
  235. </Button>
  236. </div>
  237. </CardContent>
  238. </Card>
  239. )}
  240. {currentStep === 3 && (
  241. <Card>
  242. <CardHeader>
  243. <CardTitle>Step 3: Import Data</CardTitle>
  244. <CardDescription>
  245. Processing Excel file and importing data into database
  246. </CardDescription>
  247. </CardHeader>
  248. <CardContent>
  249. <div className="space-y-4">
  250. <p className="text-sm text-muted-foreground">
  251. Import ID: {importRecord?.id}
  252. </p>
  253. <Button
  254. onClick={handleProcessImportData}
  255. disabled={isProcessing}
  256. className="w-full"
  257. >
  258. {isProcessing ? (
  259. <>
  260. <Loader2 className="mr-2 h-4 w-4 animate-spin" />
  261. Processing Import...
  262. </>
  263. ) : (
  264. 'Process Import'
  265. )}
  266. </Button>
  267. </div>
  268. </CardContent>
  269. </Card>
  270. )}
  271. {currentStep === 4 && (
  272. <Card>
  273. <CardHeader>
  274. <CardTitle>Step 4: Generate Summary</CardTitle>
  275. <CardDescription>
  276. Running summary calculations and displaying results
  277. </CardDescription>
  278. </CardHeader>
  279. <CardContent>
  280. <div className="space-y-4">
  281. <Button
  282. onClick={handleGenerateSummary}
  283. disabled={isProcessing}
  284. className="w-full"
  285. >
  286. {isProcessing ? (
  287. <>
  288. <Loader2 className="mr-2 h-4 w-4 animate-spin" />
  289. Generating Summary...
  290. </>
  291. ) : (
  292. 'Generate Summary'
  293. )}
  294. </Button>
  295. {summaryData.length > 0 && (
  296. <div className="mt-6">
  297. <h3 className="text-lg font-semibold mb-4">Summary Results</h3>
  298. <div className="overflow-x-auto">
  299. <table className="min-w-full divide-y divide-gray-200">
  300. <thead className="bg-gray-50">
  301. <tr>
  302. <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Week</th>
  303. <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">TRR Total</th>
  304. <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">4 Week Avg</th>
  305. <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">TRR + 4Wk</th>
  306. <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Power Adds</th>
  307. </tr>
  308. </thead>
  309. <tbody className="bg-white divide-y divide-gray-200">
  310. {summaryData.map((item) => (
  311. <tr key={item.id}>
  312. <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-900">{item.week}</td>
  313. <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-900">{item.trrTotal}</td>
  314. <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-900">{item.fourWkAverages}</td>
  315. <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-900">{item.trrPlus4Wk}</td>
  316. <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-900">{item.powerAdds}</td>
  317. </tr>
  318. ))}
  319. </tbody>
  320. </table>
  321. </div>
  322. </div>
  323. )}
  324. </div>
  325. </CardContent>
  326. </Card>
  327. )}
  328. {/* Navigation */}
  329. <div className="flex justify-between">
  330. <Button
  331. variant="outline"
  332. onClick={() => setCurrentStep(Math.max(1, currentStep - 1))}
  333. disabled={currentStep === 1}
  334. >
  335. Previous
  336. </Button>
  337. <Button
  338. onClick={() => setCurrentStep(Math.min(4, currentStep + 1))}
  339. disabled={currentStep === 4 || (currentStep === 1 && !uploadedFile)}
  340. >
  341. {currentStep === 4 ? 'Complete' : 'Next'}
  342. </Button>
  343. </div>
  344. </div>
  345. </div>
  346. );
  347. }