File size: 10,799 Bytes
c001f24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
{% extends "base.html" %}

{% block title %}Subjective Question Generator{% endblock %}

{% block head %}
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, maximum-scale=1, viewport-fit=cover">
<style>
    :root {
        --app-bg: #181a1c;
        --card-bg: #212529;
        --border-color: #495057;
        --primary-color: #0d6efd;
        --primary-bg-subtle: rgba(13, 110, 253, 0.1);
    }

    body {
        background-color: var(--app-bg);
        color: #e9ecef;
    }

    /* Centered Layout */
    .generator-wrapper {
        min-height: calc(100vh - 56px);
        min-height: calc(100dvh - 56px);
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 1rem;
        padding-bottom: env(safe-area-inset-bottom);
    }

    .main-card {
        background-color: var(--card-bg);
        border: 1px solid var(--border-color);
        border-radius: 16px;
        box-shadow: 0 4px 24px rgba(0,0,0,0.4);
        width: 100%;
        max-width: 600px;
        overflow: hidden;
    }

    /* Touch-friendly Upload Zone */
    .upload-zone {
        position: relative;
        border: 2px dashed var(--border-color);
        border-radius: 12px;
        padding: 2rem 1rem;
        text-align: center;
        background: rgba(255,255,255,0.02);
        transition: all 0.2s ease;
        cursor: pointer;
        min-height: 250px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
    }

    .upload-zone:active, .upload-zone.drag-over {
        border-color: var(--primary-color);
        background: var(--primary-bg-subtle);
        transform: scale(0.99);
    }

    .upload-zone.has-file {
        border-style: solid;
        border-color: #198754;
        background: rgba(25, 135, 84, 0.05);
        padding: 1rem;
    }

    /* Hidden Input */
    .file-input {
        position: absolute;
        top: 0; left: 0;
        width: 100%; height: 100%;
        opacity: 0;
        cursor: pointer;
        z-index: 10;
    }

    /* Icon & Text */
    .upload-icon {
        font-size: 3rem;
        color: #adb5bd;
        margin-bottom: 1rem;
        transition: color 0.2s;
    }

    .upload-zone.has-file .upload-icon {
        display: none;
    }

    .zone-text {
        font-weight: 500;
        color: #e9ecef;
    }
    
    .zone-subtext {
        font-size: 0.85rem;
        color: #adb5bd;
        margin-top: 0.5rem;
    }

    /* Image Preview */
    #image-preview {
        max-width: 100%;
        max-height: 300px;
        border-radius: 8px;
        box-shadow: 0 4px 12px rgba(0,0,0,0.3);
        display: none; /* Hidden by default */
        margin-bottom: 1rem;
    }

    /* Button Styling */
    .btn-action {
        min-height: 54px;
        font-size: 1.1rem;
        font-weight: 600;
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 10px;
        transition: transform 0.1s;
    }
    
    .btn-action:active {
        transform: scale(0.98);
    }
</style>
{% endblock %}

{% block content %}
<div class="generator-wrapper">
    <div class="main-card">
        
        <!-- Header -->
        <div class="p-4 border-bottom border-secondary" style="border-color: #495057 !important;">
            <div class="d-flex align-items-center justify-content-center mb-2">
                <div class="bg-primary bg-opacity-10 text-primary p-3 rounded-circle d-flex align-items-center justify-content-center" style="width: 60px; height: 60px;">
                    <i class="bi bi-magic fs-3"></i>
                </div>
            </div>
            <div class="text-center">
                <h1 class="h4 mb-1">Subjective Generator</h1>
                <p class="text-secondary small mb-0">AI will transcribe and format your handwritten or printed questions.</p>
            </div>
        </div>

        <div class="card-body p-4">
            <!-- Remote Camera Component -->
            {% include 'camera_receiver_component.html' %}

            <form action="{{ url_for('subjective.generate') }}" method="POST" enctype="multipart/form-data" id="generateForm">
                
                <!-- Upload Zone -->
                <div class="mb-4">
                    <div class="d-flex justify-content-between align-items-center mb-2">
                        <label class="form-label mb-0">Image</label>
                        <button type="button" class="btn btn-sm btn-outline-info" onclick="toggleCameraReceiver()">
                            <i class="bi bi-camera-video me-1"></i> Use Remote Camera
                        </button>
                    </div>
                    <div class="upload-zone" id="drop-zone">
                        <!-- Preview Image -->
                        <img id="image-preview" alt="Preview">
                        
                        <!-- Placeholder Content -->
                        <div id="placeholder-content">
                            <i class="bi bi-cloud-upload upload-icon"></i>
                            <div class="zone-text" id="main-text">Tap to upload image</div>
                            <div class="zone-subtext" id="sub-text">Supports JPG, PNG (Max 10MB)</div>
                        </div>

                        <input type="file" class="file-input" id="images-upload" name="image" accept="image/*">
                    </div>
                </div>

                <!-- Manual JSON Input (Accordion) -->
                <div class="accordion mb-4" id="manualInputAccordion">
                    <div class="accordion-item bg-transparent border-secondary">
                        <h2 class="accordion-header" id="headingManual">
                            <button class="accordion-button collapsed bg-transparent text-secondary shadow-none" type="button" data-bs-toggle="collapse" data-bs-target="#collapseManual" aria-expanded="false" aria-controls="collapseManual">
                                <i class="bi bi-code-square me-2"></i> Or Paste JSON Manually
                            </button>
                        </h2>
                        <div id="collapseManual" class="accordion-collapse collapse" aria-labelledby="headingManual" data-bs-parent="#manualInputAccordion">
                            <div class="accordion-body text-secondary">
                                <p class="small mb-2">If Gemini is unavailable, paste the raw JSON response here.</p>
                                <textarea class="form-control bg-dark text-light border-secondary font-monospace small" name="json_data" id="json_data" rows="5" placeholder='[{"question_number_within_topic": "1", "question_topic": "Topic", "question_html": "..."}]'></textarea>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Action Button -->
                <button type="submit" class="btn btn-primary w-100 btn-action shadow" id="generateBtn">
                    <span id="btnSpinner" class="spinner-border spinner-border-sm d-none" role="status" aria-hidden="true"></span>
                    <span id="btnText">Transcribe & Generate</span>
                    <i class="bi bi-stars" id="btnIcon"></i>
                </button>

            </form>
        </div>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
    const fileInput = document.getElementById('images-upload');
    const dropZone = document.getElementById('drop-zone');
    const previewImg = document.getElementById('image-preview');
    const placeholder = document.getElementById('placeholder-content');
    const mainText = document.getElementById('main-text');
    const subText = document.getElementById('sub-text');
    const form = document.getElementById('generateForm');

    function toggleCameraReceiver() {
        const component = document.getElementById('camera-receiver-component');
        if (component.style.display === 'none') {
            component.style.display = 'block';
            if (typeof initCameraReceiver === 'function') {
                initCameraReceiver();
            }
        } else {
            component.style.display = 'none';
        }
    }

    // --- File Selection & Preview ---
    fileInput.addEventListener('change', function(e) {
        const file = this.files[0];
        
        if (file) {
            // Show file name
            dropZone.classList.add('has-file');
            mainText.innerHTML = `<i class="bi bi-check-circle-fill text-success"></i> Selected`;
            subText.textContent = file.name;

            // Render Preview
            const reader = new FileReader();
            reader.onload = function(e) {
                previewImg.src = e.target.result;
                previewImg.style.display = 'block';
                // Hide icon, keep text below
                document.querySelector('.upload-icon').style.display = 'none';
            }
            reader.readAsDataURL(file);
        } else {
            resetUI();
        }
    });

    function resetUI() {
        dropZone.classList.remove('has-file');
        previewImg.style.display = 'none';
        previewImg.src = '';
        document.querySelector('.upload-icon').style.display = 'block';
        mainText.textContent = "Tap to upload image";
        subText.textContent = "Supports JPG, PNG (Max 10MB)";
    }

    // --- Drag & Drop Visuals ---
    ['dragenter', 'dragover'].forEach(evt => {
        dropZone.addEventListener(evt, (e) => {
            e.preventDefault();
            dropZone.classList.add('drag-over');
        });
    });

    ['dragleave', 'drop'].forEach(evt => {
        dropZone.addEventListener(evt, (e) => {
            e.preventDefault();
            dropZone.classList.remove('drag-over');
        });
    });

    // --- Form Submission State ---
    form.addEventListener('submit', function(e) {
        const hasFile = fileInput.files && fileInput.files.length > 0;
        const hasJson = document.getElementById('json_data').value.trim().length > 0;

        if (!hasFile && !hasJson) {
            e.preventDefault();
            alert("Please select an image or provide JSON data.");
            return;
        }

        const btn = document.getElementById('generateBtn');
        const text = document.getElementById('btnText');
        const icon = document.getElementById('btnIcon');
        const spinner = document.getElementById('btnSpinner');

        btn.disabled = true;
        btn.classList.add('opacity-75');
        icon.classList.add('d-none');
        spinner.classList.remove('d-none');
        text.textContent = 'Processing...';
    });
</script>
{% endblock %}