privateuserh's picture
Update script.js
73c463e verified
raw
history blame
4.49 kB
document.addEventListener('DOMContentLoaded', () => {
// --- CONFIGURATION ---
const workerUrl = 'https://form-handler-worker.aiagents.workers.dev/';
// --- ELEMENT SELECTIONS ---
const submissionForm = document.getElementById('vessel-exchange-form');
const lookupForm = document.getElementById('deal-lookup-form');
const adminForm = document.getElementById('admin-action-form');
const showSubmissionButton = document.getElementById('show-submission-button');
const submissionModal = document.getElementById('submission-modal');
const dealModal = document.getElementById('deal-modal');
const lookupContainer = document.getElementById('lookup-container');
const lookupInput = document.getElementById('lookup-deal-id');
const submissionStatus = document.getElementById('status-message');
const adminStatus = document.getElementById('admin-status-message');
// --- EVENT LISTENERS ---
// Show the submission form modal
if (showSubmissionButton) {
showSubmissionButton.addEventListener('click', () => {
submissionModal.classList.remove('hidden');
});
}
// Attach close listeners to both modals
[submissionModal, dealModal].forEach(modal => {
if (modal) {
modal.querySelector('.modal-close').addEventListener('click', () => closeModal(modal));
modal.addEventListener('click', (event) => {
if (event.target === modal) closeModal(modal);
});
}
});
// Handle form submissions
if (submissionForm) submissionForm.addEventListener('submit', handleFormSubmit);
if (lookupForm) lookupForm.addEventListener('submit', handleLookupSubmit);
// Handle Admin Action Clicks
if (adminForm) {
document.getElementById('admin-approve-button').addEventListener('click', () => handleAdminClick('approve'));
document.getElementById('admin-decline-button').addEventListener('click', () => handleAdminClick('decline'));
}
// --- HANDLER FUNCTIONS ---
async function handleFormSubmit(event) {
// ... (This function is already correct from our last version) ...
}
// THIS FUNCTION IS NOW CORRECTED
async function handleLookupSubmit(event) {
event.preventDefault();
if (!lookupInput.value) {
alert('Please enter a Deal ID.');
return;
}
try {
const response = await fetch(`${workerUrl}?deal_id=${lookupInput.value}`);
const result = await response.json();
if (!response.ok) throw new Error(result.error || 'Deal not found.');
// This now calls the correct helper function to open the modal
openDealInfoModal(result);
} catch (error) {
alert(error.message);
}
}
async function handleAdminClick(action) {
// ... (This function is already correct) ...
}
// --- UI HELPER FUNCTIONS ---
// THIS FUNCTION IS NOW RESTORED AND FULLY FEATURED
function openDealInfoModal(dealData) {
// Select all the span elements inside the modal
document.getElementById('modal-deal-id').textContent = dealData.deal_id;
document.getElementById('modal-owner-name').textContent = dealData.owner_name;
document.getElementById('modal-contact-email').textContent = dealData.contact_email;
document.getElementById('modal-contact-phone').textContent = dealData.contact_phone || 'N/A';
document.getElementById('modal-year-built').textContent = dealData.year_built || 'N/A';
document.getElementById('modal-gross-tonnage').textContent = dealData.gross_tonnage || 'N/A';
document.getElementById('modal-token-amount').textContent = dealData.token_amount;
document.getElementById('modal-vessel-description').textContent = dealData.vessel_description || 'No description provided.';
// Handle the status badge
const statusBadge = document.getElementById('modal-status-badge');
statusBadge.textContent = dealData.status;
statusBadge.className = `badge ${dealData.status || 'active'}`; // Set class for color
// Show the modal
dealModal.classList.remove('hidden');
}
function closeModal(modalElement) {
modalElement.classList.add('hidden');
}
// ... all other showStatus, showSuccess, showError functions are correct ...
});