File size: 25,696 Bytes
96c003e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>PDF Converter Tool</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }

      body {
        font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        min-height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 20px;
      }

      .container {
        background: rgba(255, 255, 255, 0.95);
        backdrop-filter: blur(10px);
        padding: 40px;
        border-radius: 20px;
        box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        max-width: 600px;
        width: 100%;
        animation: slideIn 0.6s ease-out;
      }

      @keyframes slideIn {
        from {
          opacity: 0;
          transform: translateY(30px);
        }
        to {
          opacity: 1;
          transform: translateY(0);
        }
      }

      .header {
        text-align: center;
        margin-bottom: 40px;
      }

      .header h1 {
        color: #333;
        font-size: 2.5em;
        margin-bottom: 10px;
        background: linear-gradient(45deg, #667eea, #764ba2);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        background-clip: text;
      }

      .header p {
        color: #666;
        font-size: 1.1em;
      }

      .status-indicator {
        position: absolute;
        top: 20px;
        right: 20px;
        padding: 8px 16px;
        border-radius: 20px;
        font-size: 0.8em;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.5px;
      }

      .status-online {
        background: #d4edda;
        color: #155724;
        border: 1px solid #c3e6cb;
      }

      .status-offline {
        background: #f8d7da;
        color: #721c24;
        border: 1px solid #f5c6cb;
      }

      .conversion-options {
        display: grid;
        gap: 20px;
        margin-bottom: 30px;
      }

      .option-card {
        background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
        border: none;
        border-radius: 15px;
        padding: 25px;
        cursor: pointer;
        transition: all 0.3s ease;
        color: white;
        text-align: left;
        position: relative;
        overflow: hidden;
      }

      .option-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
      }

      .option-card.html {
        background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
      }

      .option-card.word {
        background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
      }

      .option-card.json {
        background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
      }

      /* New style for Excel option card */
      .option-card.excel {
        background: linear-gradient(135deg, #2ecc71 0%, #27ae60 100%); /* Green shades for Excel */
      }


      .option-card::before {
        content: "";
        position: absolute;
        top: 0;
        left: -100%;
        width: 100%;
        height: 100%;
        background: linear-gradient(
          90deg,
          transparent,
          rgba(255, 255, 255, 0.2),
          transparent
        );
        transition: left 0.5s;
      }

      .option-card:hover::before {
        left: 100%;
      }

      .option-icon {
        font-size: 2em;
        margin-bottom: 10px;
      }

      .option-title {
        font-size: 1.3em;
        font-weight: bold;
        margin-bottom: 5px;
      }

      .option-desc {
        font-size: 0.9em;
        opacity: 0.9;
      }

      .upload-section {
        display: none;
        background: #f8f9fa;
        border-radius: 15px;
        padding: 30px;
        margin-top: 20px;
        border: 2px dashed #ddd;
        transition: all 0.3s ease;
      }

      .upload-section.active {
        display: block;
        animation: fadeIn 0.5s ease-out;
      }

      @keyframes fadeIn {
        from {
          opacity: 0;
        }
        to {
          opacity: 1;
        }
      }

      .file-input-wrapper {
        position: relative;
        display: inline-block;
        width: 100%;
        margin-bottom: 20px;
      }

      .file-input {
        display: none;
      }

      .file-input-label {
        display: block;
        padding: 15px 25px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        border-radius: 10px;
        cursor: pointer;
        text-align: center;
        transition: all 0.3s ease;
        font-weight: 500;
      }

      .file-input-label:hover {
        transform: translateY(-2px);
        box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
      }

      .file-name {
        margin-top: 10px;
        padding: 10px;
        background: #e9ecef;
        border-radius: 8px;
        font-size: 0.9em;
        color: #495057;
        display: none;
      }

      .output-name {
        width: 100%;
        padding: 15px;
        border: 2px solid #e9ecef;
        border-radius: 10px;
        font-size: 1em;
        margin-bottom: 20px;
        transition: border-color 0.3s ease;
      }

      .output-name:focus {
        outline: none;
        border-color: #667eea;
      }

      .convert-btn {
        width: 100%;
        padding: 15px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        border: none;
        border-radius: 10px;
        font-size: 1.1em;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s ease;
        position: relative;
        overflow: hidden;
      }

      .convert-btn:hover {
        transform: translateY(-2px);
        box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
      }

      .convert-btn:disabled {
        opacity: 0.7;
        cursor: not-allowed;
        transform: none;
      }

      .back-btn {
        background: #6c757d;
        color: white;
        border: none;
        padding: 10px 20px;
        border-radius: 8px;
        cursor: pointer;
        margin-bottom: 20px;
        transition: all 0.3s ease;
      }

      .back-btn:hover {
        background: #5a6268;
        transform: translateY(-1px);
      }

      .progress-bar {
        width: 100%;
        height: 6px;
        background: #e9ecef;
        border-radius: 3px;
        margin: 20px 0;
        overflow: hidden;
        display: none;
      }

      .progress-fill {
        height: 100%;
        background: linear-gradient(90deg, #667eea, #764ba2);
        width: 0%;
        transition: width 0.3s ease;
        border-radius: 3px;
      }

      .result-section {
        margin-top: 20px;
        padding: 20px;
        border-radius: 12px;
        display: none;
      }

      .result-success {
        background: linear-gradient(135deg, #d4edda 0%, #c3e6cb 100%);
        border: 1px solid #c3e6cb;
        color: #155724;
      }

      .result-error {
        background: linear-gradient(135deg, #f8d7da 0%, #f5c6cb 100%);
        border: 1px solid #f5c6cb;
        color: #721c24;
      }

      .loading {
        display: none;
        text-align: center;
        margin: 20px 0;
      }

      .spinner {
        border: 4px solid #f3f3f3;
        border-top: 4px solid #667eea;
        border-radius: 50%;
        width: 40px;
        height: 40px;
        animation: spin 1s linear infinite;
        margin: 0 auto 10px;
      }

      @keyframes spin {
        0% {
          transform: rotate(0deg);
        }
        100% {
          transform: rotate(360deg);
        }
      }

      .drag-over {
        border-color: #667eea !important;
        background: rgba(102, 126, 234, 0.1) !important;
      }

      .debug-info {
        margin-top: 20px;
        padding: 15px;
        background: #f8f9fa;
        border-radius: 8px;
        font-size: 0.9em;
        color: #6c757d;
        border-left: 4px solid #007bff;
      }

      .error-details {
        margin-top: 10px;
        padding: 10px;
        background: #fff3cd;
        border: 1px solid #ffeaa7;
        border-radius: 6px;
        font-size: 0.85em;
        color: #856404;
        max-height: 200px;
        overflow-y: auto;
      }

      @media (max-width: 768px) {
        .container {
          padding: 25px;
          margin: 10px;
        }

        .header h1 {
          font-size: 2em;
        }

        .option-card {
          padding: 20px;
        }

        .status-indicator {
          position: relative;
          top: auto;
          right: auto;
          margin-bottom: 20px;
          display: inline-block;
        }
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div id="status-indicator" class="status-indicator status-offline">
        Server Offline
      </div>

      <div class="header">
        <h1>🧠 PDF Converter</h1>
        <p>Convert your PDF files to HTML, Word, JSON, or Excel format</p>
      </div>

      <div id="main-menu">
        <div class="conversion-options">
          <button class="option-card html" onclick="showUploadSection('html')">
            <div class="option-icon">🌐</div>
            <div class="option-title">Convert to HTML</div>
            <div class="option-desc">
              Transform PDF into web-ready HTML format
            </div>
          </button>

          <button class="option-card word" onclick="showUploadSection('word')">
            <div class="option-icon">πŸ“„</div>
            <div class="option-title">Convert to Word</div>
            <div class="option-desc">
              Create editable Word documents from PDF
            </div>
          </button>

          <button class="option-card json" onclick="showUploadSection('json')">
            <div class="option-icon">πŸ“Š</div>
            <div class="option-title">Convert to JSON</div>
            <div class="option-desc">
              Extract structured data in JSON format
            </div>
          </button>

          <button class="option-card excel" onclick="showUploadSection('excel')">
            <div class="option-icon">πŸ“ˆ</div>
            <div class="option-title">Convert to Excel</div>
            <div class="option-desc">
              Organize PDF tables into an Excel spreadsheet
            </div>
          </button>
        </div>
      </div>

      <div id="upload-section" class="upload-section">
        <button class="back-btn" onclick="showMainMenu()">
          ← Back to Menu
        </button>

        <div class="file-input-wrapper">
          <input
            type="file"
            id="pdf-file"
            class="file-input"
            accept=".pdf"
            onchange="handleFileSelect(event)"
          />
          <label for="pdf-file" class="file-input-label" id="file-label">
            πŸ“„ Choose PDF File or Drag & Drop Here
          </label>
          <div id="file-name" class="file-name"></div>
        </div>

        <div class="loading" id="loading">
          <div class="spinner"></div>
          <p>Converting your PDF file...</p>
        </div>

        <div class="progress-bar" id="progress-bar">
          <div class="progress-fill" id="progress-fill"></div>
        </div>

        <button
          class="convert-btn"
          id="convert-btn"
          onclick="convertFile()"
          disabled
        >
          πŸš€ Start Conversion
        </button>

        <div id="result-section" class="result-section">
          <div id="result-message"></div>
        </div>

        <div id="debug-info" class="debug-info" style="display: none">
          <strong>Debug Information:</strong>
          <div id="debug-content"></div>
        </div>
      </div>
    </div>

    <script>
      let currentFormat = "";
      let selectedFile = null;
      let serverOnline = false;

      // Check server status on page load
      document.addEventListener("DOMContentLoaded", function () {
        checkServerStatus();
        // Check server status every 30 seconds
        setInterval(checkServerStatus, 30000);
      });

      async function checkServerStatus() {
        try {
          const response = await fetch("/health", {
            method: "GET",
            mode: "cors",
            headers: {
              Accept: "application/json",
            },
            signal: AbortSignal.timeout(5000), // 5 second timeout
          });

          if (response.ok) {
            const data = await response.json();
            updateServerStatus(true, data.message || "Server is online");
          } else {
            updateServerStatus(false, `Server returned ${response.status}`);
          }
        } catch (error) {
          updateServerStatus(false, error.message);
        }
      }

      function updateServerStatus(online, message) {
        serverOnline = online;
        const indicator = document.getElementById("status-indicator");

        if (online) {
          indicator.className = "status-indicator status-online";
          indicator.textContent = "Server Online";
          indicator.title = message;
        } else {
          indicator.className = "status-indicator status-offline";
          indicator.textContent = "Server Offline";
          indicator.title = `Error: ${message}`;
        }
      }

     function showUploadSection(format) {
  if (!serverOnline) {
    alert("Server is offline. Please start the Flask server first.");
    return;
  }

  currentFormat = format;
  document.getElementById("main-menu").style.display = "none";
  document.getElementById("upload-section").classList.add("active");

  resetForm(); // βœ… Always reset when entering upload

  const outputInput = document.getElementById("output-name");
  const extensions = { html: ".html", word: ".docx", json: ".json", excel: ".xlsx" };
  outputInput.placeholder = `Enter output filename (e.g., converted_file${extensions[format]})`;
}


     function showMainMenu() {
        window.location.reload();
  document.getElementById("main-menu").style.display = "block";
  document.getElementById("upload-section").classList.remove("active");
  resetForm();

  selectedFile = null;
}


      function resetForm() {
  selectedFile = null;

  const pdfInput = document.getElementById("pdf-file");
  const outputInput = document.getElementById("output-name");
  const fileName = document.getElementById("file-name");
  const fileLabel = document.getElementById("file-label");

  // Clear inputs
  pdfInput.value = "";


  // Hide filename display
  fileName.style.display = "none";
  fileName.textContent = "";

  // Reset label text
  fileLabel.textContent = "πŸ“„ Choose PDF File or Drag & Drop Here";

  // Reset buttons and sections
  document.getElementById("convert-btn").disabled = true;
  document.getElementById("result-section").style.display = "none";
  document.getElementById("loading").style.display = "none";
  document.getElementById("progress-bar").style.display = "none";
  document.getElementById("debug-info").style.display = "none";

  // Also reset drag-over styling if stuck
  document.getElementById("upload-section").classList.remove("drag-over");
}


      function handleFileSelect(event) {
        const file = event.target.files[0];
        if (file && file.type === "application/pdf") {
          selectedFile = file;
          document.getElementById("file-name").textContent = `Selected: ${
            file.name
          } (${(file.size / 1024 / 1024).toFixed(2)} MB)`;
          document.getElementById("file-name").style.display = "block";
          document.getElementById(
            "file-label"
          ).textContent = `βœ… ${file.name} selected`;
          checkFormValidity();
        } else {
          alert("Please select a valid PDF file.");
          resetFileInput();
        }
      }

      function resetFileInput() {
        selectedFile = null;
        document.getElementById("pdf-file").value = "";
        document.getElementById("file-name").style.display = "none";
        document.getElementById("file-label").textContent =
          "πŸ“„ Choose PDF File or Drag & Drop Here";
        checkFormValidity();
      }

      function checkFormValidity() {
        const outputName = document.getElementById("output-name").value.trim();
        const convertBtn = document.getElementById("convert-btn");

        if (selectedFile && outputName && serverOnline) {
          convertBtn.disabled = false;
          convertBtn.textContent = "πŸš€ Start Conversion";
        } else {
          convertBtn.disabled = true;
          convertBtn.textContent = serverOnline
            ? "πŸš€ Start Conversion"
            : "❌ Server Offline";
        }
      }

      // Add event listener for output name input
      document
        .getElementById("output-name")
        .addEventListener("input", checkFormValidity);

      // Drag and drop functionality
      const uploadSection = document.getElementById("upload-section");

      ["dragenter", "dragover", "dragleave", "drop"].forEach((eventName) => {
        uploadSection.addEventListener(eventName, preventDefaults, false);
      });

      function preventDefaults(e) {
        e.preventDefault();
        e.stopPropagation();
      }

      ["dragenter", "dragover"].forEach((eventName) => {
        uploadSection.addEventListener(eventName, highlight, false);
      });

      ["dragleave", "drop"].forEach((eventName) => {
        uploadSection.addEventListener(eventName, unhighlight, false);
      });

      function highlight() {
        uploadSection.classList.add("drag-over");
      }

      function unhighlight() {
        uploadSection.classList.remove("drag-over");
      }

      uploadSection.addEventListener("drop", handleDrop, false);

      function handleDrop(e) {
        const dt = e.dataTransfer;
        const files = dt.files;

        if (files.length > 0) {
          const file = files[0];
          if (file.type === "application/pdf") {
            selectedFile = file;
            document.getElementById("file-name").textContent = `Selected: ${
              file.name
            } (${(file.size / 1024 / 1024).toFixed(2)} MB)`;
            document.getElementById("file-name").style.display = "block";
            document.getElementById(
              "file-label"
            ).textContent = `βœ… ${file.name} selected`;
            checkFormValidity();
          } else {
            alert("Please drop a valid PDF file.");
          }
        }
      }

      function checkFormValidity() {
  const convertBtn = document.getElementById("convert-btn");
  if (selectedFile && serverOnline) {
    convertBtn.disabled = false;
    convertBtn.textContent = "πŸš€ Start Conversion";
  } else {
    convertBtn.disabled = true;
    convertBtn.textContent = serverOnline
      ? "πŸš€ Start Conversion"
      : "❌ Server Offline";
  }
}

async function convertFile() {
  if (!selectedFile || !currentFormat) {
    alert("Please select a file and format.");
    return;
  }

  if (!serverOnline) {
    alert("Server is offline. Please start the Flask server first.");
    return;
  }

  const outputName = selectedFile.name.replace(/\.[^/.]+$/, "");

  document.getElementById("loading").style.display = "block";
  document.getElementById("progress-bar").style.display = "block";
  document.getElementById("convert-btn").disabled = true;
  document.getElementById("result-section").style.display = "none";
  document.getElementById("debug-info").style.display = "none";

  simulateProgress();

  const formData = new FormData();
  formData.append("file", selectedFile);
  formData.append("format", currentFormat);
  formData.append("output_name", outputName);

  const debugInfo = {
    fileName: selectedFile.name,
    fileSize: selectedFile.size,
    format: currentFormat,
    outputName: outputName,
    timestamp: new Date().toISOString(),
  };

        try {
          console.log("πŸ”„ Starting conversion...", debugInfo);

          const controller = new AbortController();
          const timeoutId = setTimeout(() => controller.abort(), 420000); // 60 second timeout

          const response = await fetch("/convert", {
            method: "POST",
            body: formData,
            headers: {
              Accept: "application/json",
            },
            mode: "cors",
            signal: controller.signal,
          });

          clearTimeout(timeoutId);
          console.log("πŸ“‘ Response status:", response.status);

          if (!response.ok) {
            const errorText = await response.text();
            throw new Error(`Server returned ${response.status}: ${errorText}`);
          }

          const result = await response.json();
          console.log("βœ… Conversion result:", result);

          // Hide loading
          document.getElementById("loading").style.display = "none";
          document.getElementById("progress-bar").style.display = "none";

          // Show result
          const resultSection = document.getElementById("result-section");
          const resultMessage = document.getElementById("result-message");

          if (result.success) {
            resultSection.className = "result-section result-success";
            resultMessage.innerHTML = `<h3>βœ… Conversion Successful!</h3>
        <p>Your PDF has been converted to ${currentFormat.toUpperCase()} format.</p>
        <p><strong>Output file:</strong> ${
          result.output_path || "Generated successfully"
        }</p>`;

            if (result.download_url) {
              const downloadUrl = `${window.location.origin}${result.download_url}`;

              // Add link for user
              resultMessage.innerHTML += `<p><a href="${downloadUrl}" target="_blank" style="color: #155724; text-decoration: none; font-weight: bold;">πŸ“₯ Download File</a></p>`;

              // ⬇️ Auto-download
              const a = document.createElement("a");
              a.href = downloadUrl;
              a.download = result.output_path || "converted_file";
              document.body.appendChild(a);
              a.click();
              document.body.removeChild(a);
            }
          } else {
            resultSection.className = "result-section result-error";
            resultMessage.innerHTML = `
                        <h3>❌ Conversion Failed</h3>
                        <p>${
                          result.error || "An unexpected error occurred."
                        }</p>
                    `;
          }

          resultSection.style.display = "block";
        } catch (error) {
          console.error("❌ Error during conversion:", error);

          // Hide loading
          document.getElementById("loading").style.display = "none";
          document.getElementById("progress-bar").style.display = "none";

          // Show error
          const resultSection = document.getElementById("result-section");
          const resultMessage = document.getElementById("result-message");

          resultSection.className = "result-section result-error";

          let errorMessage = "An unexpected error occurred.";
          if (error.name === "AbortError") {
            errorMessage =
              "Request timed out. The file might be too large or the server is taking too long to respond.";
          } else if (error.message.includes("Failed to fetch")) {
            errorMessage =
              "Cannot connect to server. Please ensure the Flask server is running on http://localhost:5000";
          } else {
            errorMessage = error.message;
          }

          resultMessage.innerHTML = `
                    <h3>❌ Conversion Error</h3>
                    <p>${errorMessage}</p>
                `;

          resultSection.style.display = "block";

          // Show debug information
          const debugElement = document.getElementById("debug-info");
          const debugContent = document.getElementById("debug-content");
          debugContent.innerHTML = `
                    <div class="error-details">
                        <strong>Error Details:</strong><br>
                        Type: ${error.name}<br>
                        Message: ${error.message}<br>
                        <br>
                        <strong>Request Details:</strong><br>
                        ${JSON.stringify(debugInfo, null, 2)}
                        <br>
                        <strong>Troubleshooting:</strong><br>
                        1. Ensure Flask server is running: python app.py<br>
                        2. Check server logs for errors<br>
                        3. Verify file size is under 100MB<br>
                        4. Check browser console for additional errors
                    </div>
                `;
          debugElement.style.display = "block";
        }

        document.getElementById("convert-btn").disabled = false;
        checkFormValidity(); // Update button state
      }

      function simulateProgress() {
        const progressFill = document.getElementById("progress-fill");
        let progress = 0;

        const interval = setInterval(() => {
          progress += Math.random() * 15;
          if (progress > 90) progress = 90;

          progressFill.style.width = progress + "%";

          if (progress >= 90) {
            clearInterval(interval);
          }
        }, 200);

        // Reset progress after animation
        setTimeout(() => {
          progressFill.style.width = "0%";
        }, 5000);
      }
    </script>
  </body>
</html>