File size: 3,810 Bytes
46505d1
 
 
 
 
 
 
 
b44aa74
46505d1
 
 
 
 
 
 
 
 
 
 
 
 
b44aa74
46505d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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');
    
    // Modals and Buttons
    const submissionModal = document.getElementById('submission-modal');
    const dealModal = document.getElementById('deal-modal');
    const showSubmissionButton = document.getElementById('show-submission-button');
    const submissionModalCloseButton = document.getElementById('submission-modal-close-button');
    const dealModalCloseButton = document.getElementById('modal-close-button');

    // --- EVENT LISTENERS ---

    // Show the submission form modal
    showSubmissionButton.addEventListener('click', () => {
        submissionModal.classList.remove('hidden');
    });
    
    // Close the submission form modal
    submissionModalCloseButton.addEventListener('click', () => closeModal(submissionModal));
    submissionModal.addEventListener('click', (event) => {
        if (event.target === submissionModal) closeModal(submissionModal);
    });

    // Handle New Deal Submissions
    if (submissionForm) {
        submissionForm.addEventListener('submit', handleFormSubmit);
    }

    // --- All other event listeners and functions from the previous version remain the same ---
    // (lookupForm, adminForm, dealModal closing, etc.)
    
    // --- HANDLER FUNCTIONS ---
    async function handleFormSubmit(event) {
        event.preventDefault();
        const submitButton = document.getElementById('submit-button');
        const statusMessage = document.getElementById('status-message');
        
        submitButton.disabled = true;
        showStatus(statusMessage, 'Submitting...');
        
        try {
            const formData = new FormData(submissionForm);
            const response = await fetch(workerUrl, { method: 'POST', body: formData });
            const result = await response.json();
            if (!response.ok) throw new Error(result.error || `Server responded with status ${response.status}`);
            
            showSuccess(statusMessage, `Success! Your Deal ID is: ${result.deal_id}`);
            submissionForm.reset();
            
            // Close the modal after a short delay to allow user to see the success message
            setTimeout(() => closeModal(submissionModal), 2000);

        } catch (error) {
            showError(statusMessage, error.message);
        } finally {
            submitButton.disabled = false;
        }
    }

    // --- All other handler functions (handleLookupSubmit, handleAdminClick) remain the same ---

    // --- MODAL & UI FUNCTIONS ---
    function closeModal(modalElement) {
        modalElement.classList.add('hidden');
    }

    function showStatus(element, message) {
        element.className = 'status';
        element.textContent = message;
        element.style.display = 'block';
    }

    function showSuccess(element, message) {
        element.className = 'status success';
        element.innerHTML = `<strong>${message}</strong>`; // Use innerHTML to render bold tag
        element.style.display = 'block';
    }

    function showError(element, message) {
        element.className = 'status error';
        element.innerHTML = `<strong>Error:</strong> ${message}`;
        element.style.display = 'block';
    }
    
    // --- The openModal function for the deal lookup remains the same ---
    function openModal(dealData) {
        // ... (existing code to populate deal info modal)
        dealModal.classList.remove('hidden');
    }
});