File size: 8,413 Bytes
509f3c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// PySQL Labs - Main JavaScript

document.addEventListener('DOMContentLoaded', function() {
    // Initialize Feather Icons
    if (typeof feather !== 'undefined') {
        feather.replace();
    }

    // Mobile Menu Toggle (for navbar component)
    document.addEventListener('click', function(e) {
        if (e.target.closest('[data-menu-toggle]')) {
            const menu = document.getElementById('mobile-menu');
            if (menu) {
                menu.classList.toggle('hidden');
            }
    });

    // Code Execution Simulation
    function simulateCodeExecution(editorId, outputId) {
        const runBtn = document.getElementById('run-btn');
        const editor = document.getElementById(editorId);
        const output = document.getElementById(outputId);

        if (runBtn && editor && output) {
            runBtn.addEventListener('click', function() {
                // Show loading state
                runBtn.innerHTML = '<i data-feather="loader" class="w-4 h-4 animate-spin mr-2"></i>Running...';
                feather.replace();
                
                runBtn.disabled = true;
                output.innerHTML = '<div class="text-gray-400 animate-pulse">Executing code...</div>';
                
                // Simulate API call delay
                setTimeout(() => {
                    // Simulated output
                    output.innerHTML = `
                        <div class="text-green-400 font-mono text-sm">
                            > Code executed successfully!<br>
                            > Tests passed: 4/4<br>
                            > Execution time: 0.8s
                        </div>`;
                    
                    // Reset button
                    runBtn.innerHTML = '<i data-feather="play" class="w-4 h-4 mr-2"></i>Run Code';
                    feather.replace();
                    runBtn.disabled = false;
                }, 1500);
            }
        }
    }

    // Carousel functionality for problem cards
    function initProblemCarousel() {
        const carousel = document.querySelector('.problems-carousel');
        if (!carousel) return;

        const cards = carousel.querySelectorAll('.problem-card');
        let currentIndex = 0;

        function showCard(index) {
            cards.forEach((card, i) => {
                card.classList.toggle('active', i === index);
                card.classList.toggle('opacity-50', i !== index);
        }

        // Auto-rotate every 5 seconds
        setInterval(() => {
            currentIndex = (currentIndex + 1) % cards.length;
            showCard(currentIndex);
        }, 5000);

        showCard(0);
    }

    // SQL Query Execution Simulation
    function simulateSQLExecution() {
        const sqlRunBtn = document.getElementById('sql-run-btn');
        const sqlResults = document.getElementById('sql-results');

        if (sqlRunBtn && sqlResults) {
            sqlRunBtn.addEventListener('click', function() {
                sqlRunBtn.innerHTML = '<i data-feather="loader" class="w-4 h-4 animate-spin mr-2"></i>Executing...';
        feather.replace();
                sqlRunBtn.disabled = true;
                
                setTimeout(() => {
                    sqlResults.innerHTML = `
                        <table class="w-full text-sm">
                            <thead>
                                <tr class="border-b border-gray-700">
                                    <th class="py-2 text-left">id</th>
                                    <th class="py-2 text-left">name</th>
                                    <th class="py-2 text-left">department</th>
                                    <th class="py-2 text-left">salary</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr class="border-b border-gray-800">
                                    <td class="py-2">101</td>
                                    <td class="py-2">Alice Chen</td>
                                    <td class="py-2">Engineering</td>
                                    <td class="py-2">$95,000</td>
                                </tr>
                                <tr class="border-b border-gray-800">
                                    <td class="py-2">102</td>
                                    <td class="py-2">Bob Johnson</td>
                                    <td class="py-2">Sales</td>
                                    <td class="py-2">$85,500</td>
                                </tr>
                                <tr>
                                    <td class="py-2">103</td>
                                    <td class="py-2">Charlie Brown</td>
                                    <td class="py-2">Marketing</td>
                                    <td class="py-2">$78,000</td>
                                </tr>
                            </tbody>
                        </table>
                        <div class="mt-3 text-xs text-gray-400">
                            3 rows returned. Execution time: 0.2s
                        </div>`;
                    
                    sqlRunBtn.innerHTML = '<i data-feather="play" class="w-4 h-4 mr-2"></i>Run Query';
                    feather.replace();
                    sqlRunBtn.disabled = false;
                }, 1000);
            });
        }
    }

    // Interview Timer Simulation
    function initInterviewTimer() {
        const timerElement = document.getElementById('interview-timer');
        if (!timerElement) return;

        let timeLeft = 1800; // 30 minutes in seconds

        function updateTimer() {
            const minutes = Math.floor(timeLeft / 60);
            const seconds = timeLeft % 60;
            
            timerElement.textContent = 
                `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}';
            
            if (timeLeft > 0) {
                timeLeft--;
            }
        }

        // Update every second
        setInterval(updateTimer, 1000);
        updateTimer(); // Initial call
    }

    // Initialize all features
    setTimeout(() => {
        simulateCodeExecution('python-editor', 'python-output');
        simulateSQLExecution();
        initProblemCarousel();
        initInterviewTimer();
    }, 500);

    // Dark mode toggle (if needed)
    const darkModeToggle = document.getElementById('dark-mode-toggle');
    if (darkModeToggle) {
        darkModeToggle.addEventListener('click', function() {
            document.documentElement.classList.toggle('dark');
            const icon = this.querySelector('i');
            if (document.documentElement.classList.contains('dark')) {
                icon.setAttribute('data-feather', 'moon');
            feather.replace();
        }
    }

    // Add keyboard shortcuts for better UX
    document.addEventListener('keydown', function(e) {
        // Ctrl+Enter to run code (when editor is focused)
        if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
            const activeRunBtn = document.querySelector('#run-btn:not([disabled]), #sql-run-btn:not([disabled])');
            if (activeRunBtn) {
                e.preventDefault();
                activeRunBtn.click();
            }
        }
    });

    // Tooltip initialization
    const tooltipTriggers = document.querySelectorAll('[data-tooltip]');
    tooltipTriggers.forEach(trigger => {
        trigger.addEventListener('mouseenter', function() {
            const tooltipText = this.getAttribute('data-tooltip');
            if (tooltipText) {
                const tooltip = document.createElement('div');
                tooltip.className = 'absolute z-50 px-3 py-2 text-sm font-medium bg-gray-800 rounded-lg shadow-sm opacity-0 invisible transition-opacity duration-300';
                tooltip.textContent = tooltipText;
                this.appendChild(tooltip);
                
                // Show tooltip
                setTimeout(() => {
                    tooltip.classList.remove('opacity-0', 'invisible');
                    tooltip.classList.add('opacity-100');
                }, 100);
                
                this.addEventListener('mouseleave', function() {
                    tooltip.remove();
                }, { once: true });
            }
        });
    });
});