page.tsx 13 KB

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