'use client'; import React, { useState } from 'react'; import Image from 'next/image'; const SubmissionOptions = () => { const [isProcessing, setIsProcessing] = useState(false); const handleFillManually = () => { window.location.href = '/samples-form'; }; const handleFileUpload = (event) => { const file = event.target.files[0]; if (file) { const validTypes = ['.xlsx', '.xls']; const fileExtension = '.' + file.name.split('.').pop().toLowerCase(); if (validTypes.includes(fileExtension)) { processExcelFile(file); } else { alert('Please select a valid Excel file (.xlsx or .xls)'); event.target.value = ''; } } }; const processExcelFile = async (file) => { setIsProcessing(true); try { // Import XLSX dynamically since it's a client-side library const XLSX = await import('xlsx'); const reader = new FileReader(); reader.onload = (e) => { try { const data = new Uint8Array(e.target.result); const workbook = XLSX.read(data, { type: 'array' }); const sheetName = workbook.SheetNames[0]; const worksheet = workbook.Sheets[sheetName]; const jsonData = XLSX.utils.sheet_to_json(worksheet, { defval: '' }); if (jsonData.length > 0) { // Store the Excel data in sessionStorage sessionStorage.setItem('excelData', JSON.stringify(jsonData)); sessionStorage.setItem('uploadedFileName', file.name); // Redirect to samples_form after a short delay setTimeout(() => { window.location.href = '/samples-form'; }, 1000); } else { alert('No valid data found in the Excel file.'); setIsProcessing(false); } } catch (error) { console.error('Excel processing error:', error); alert('Error processing Excel file. Please check the file format and try again.'); setIsProcessing(false); } }; reader.readAsArrayBuffer(file); } catch (error) { console.error('Error loading XLSX library:', error); alert('Error loading file processor. Please try again.'); setIsProcessing(false); } }; const handleDownloadTemplate = async () => { try { // Import XLSX dynamically const XLSX = await import('xlsx'); // Create template data structure with only column headers const templateData = [ { // Customer Information 'Principal Investigator': '', 'Email': '', 'Company/Institution': '', 'Contact Number': '', 'Address': '', 'City': '', 'State': '', 'Pin': '', 'Secondary Contact': '', 'Secondary Email': '', 'Secondary Company/Institution': '', 'Secondary Contact Number': '', // Sample Information 'Project Title': '', 'Number of Samples': '', 'Sample Type': '', 'Sample Type Other': '', 'Sample Source': '', 'Sample Source Other': '', 'Pathogenicity': '', 'Sample Remarks': '', // Service Information 'Service Requested': '', 'Service Requested Other': '', 'Type of Library': '', 'Type of Library Other': '', 'Required Library Size': '', 'Required Library Size Other': '', 'Index Information': '', 'Kit Information': '', 'Sequencing Platform': '', 'Sequencing Platform Other': '', 'Sequencing Read Length': '', 'Sequencing Read Length Other': '', 'Total Data Requirement': '', 'Service Remarks': '', // Bioinformatics Information 'Analysis Requested': '', 'Analysis Details': '', 'Reference Genome Available': '', 'Genome Size': '', 'Special Consideration': '', // Sample Details 'Serial Number': '', 'Sample Name': '', 'Storage Temp': '', 'Preservative Reagent': '', 'Temp Information': '', 'Comments': '' } ]; // Create workbook and worksheet const workbook = XLSX.utils.book_new(); const worksheet = XLSX.utils.json_to_sheet(templateData); // Set column widths for better readability const colWidths = Object.keys(templateData[0]).map(() => ({ wch: 20 })); worksheet['!cols'] = colWidths; // Add worksheet to workbook XLSX.utils.book_append_sheet(workbook, worksheet, 'Sample Initiation Form'); // Generate Excel file and download const today = new Date().toISOString().split('T')[0]; const filename = `Sample_Initiation_Form_Template_${today}.xlsx`; XLSX.writeFile(workbook, filename); // Show success message showMessage('Excel template downloaded successfully!', 'success'); } catch (error) { console.error('Error downloading template:', error); alert('Error downloading template. Please try again.'); } }; const showMessage = (message, type) => { // Create and show a toast message const messageDiv = document.createElement('div'); messageDiv.className = `fixed top-5 right-5 z-50 max-w-sm p-4 rounded-lg shadow-lg font-medium ${ type === 'success' ? 'bg-green-100 text-green-800 border border-green-200' : 'bg-red-100 text-red-800 border border-red-200' }`; messageDiv.textContent = message; document.body.appendChild(messageDiv); // Auto remove after 5 seconds setTimeout(() => { if (messageDiv.parentNode) { messageDiv.remove(); } }, 5000); }; return (
{/* Title */}

Based on the convenience, please select one option:

{/* Process Image */}
Sample Submission Process
{/* Action Buttons */}
{/* Main Action Buttons */}
{/* Hidden File Input */} {/* Processing Indicator */} {isProcessing && (

Processing Excel file and redirecting...

)} {/* Or Divider */}
or
{/* Download Template Button */}
); }; export default SubmissionOptions;