File size: 48,666 Bytes
0a032b3
 
 
454e2e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abba06c
454e2e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a032b3
454e2e1
 
 
 
 
 
 
 
 
 
 
 
 
 
0a032b3
454e2e1
 
 
 
 
 
 
 
abba06c
454e2e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abba06c
454e2e1
 
 
 
 
abba06c
 
 
 
70da030
454e2e1
 
 
 
 
 
 
abba06c
454e2e1
 
 
0a032b3
454e2e1
 
 
 
8c002de
454e2e1
 
 
abba06c
454e2e1
abba06c
454e2e1
 
 
 
 
 
 
 
 
abba06c
454e2e1
 
 
 
 
 
 
 
 
abba06c
454e2e1
 
 
 
 
 
 
 
 
abba06c
454e2e1
 
 
 
 
 
 
 
 
abba06c
454e2e1
 
 
 
 
 
 
abba06c
454e2e1
abba06c
454e2e1
 
 
 
 
 
 
8c002de
454e2e1
abba06c
454e2e1
abba06c
 
 
 
 
 
 
 
 
454e2e1
 
 
 
8c002de
454e2e1
 
 
 
abba06c
454e2e1
 
 
 
abba06c
454e2e1
 
abba06c
 
 
 
 
 
454e2e1
 
 
 
8c002de
454e2e1
 
abba06c
 
 
 
 
 
 
 
 
 
454e2e1
 
 
 
8c002de
454e2e1
 
abba06c
 
 
 
 
 
454e2e1
abba06c
454e2e1
 
 
abba06c
454e2e1
 
 
 
abba06c
454e2e1
 
 
 
 
8c002de
454e2e1
 
 
 
abba06c
454e2e1
 
 
 
 
 
 
 
 
 
 
 
 
 
0a032b3
454e2e1
 
 
0a032b3
454e2e1
 
 
 
0a032b3
454e2e1
 
 
0a032b3
454e2e1
 
 
0a032b3
454e2e1
 
 
0a032b3
454e2e1
 
 
0a032b3
454e2e1
 
 
 
0a032b3
454e2e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a032b3
454e2e1
 
 
 
0a032b3
454e2e1
 
 
 
 
 
 
 
0a032b3
454e2e1
 
 
 
 
 
 
 
 
 
 
 
 
 
abba06c
ff98949
 
8c002de
abba06c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c002de
abba06c
8c002de
 
 
 
 
abba06c
8c002de
abba06c
8c002de
 
 
 
 
 
 
 
454e2e1
 
 
 
 
 
8c002de
 
 
 
 
 
 
454e2e1
 
 
 
 
 
 
 
 
 
 
 
0a032b3
454e2e1
 
 
 
 
 
 
 
8c002de
454e2e1
 
 
 
 
 
 
 
 
 
 
 
 
 
abba06c
 
8c002de
abba06c
 
8c002de
abba06c
8c002de
abba06c
 
 
8c002de
abba06c
 
8c002de
 
 
 
 
abba06c
8c002de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abba06c
8c002de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abba06c
 
8c002de
 
abba06c
 
8c002de
abba06c
8c002de
 
abba06c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c002de
abba06c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c002de
 
 
 
abba06c
8c002de
 
 
 
 
 
 
abba06c
8c002de
454e2e1
8c002de
 
454e2e1
8c002de
454e2e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c002de
454e2e1
 
 
8c002de
454e2e1
 
 
 
 
 
 
 
abba06c
454e2e1
 
 
abba06c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c002de
454e2e1
 
 
 
 
 
 
 
 
 
 
 
 
abba06c
 
 
 
 
 
 
 
 
 
 
 
 
 
8c002de
abba06c
8c002de
 
 
abba06c
8c002de
 
 
 
 
 
 
 
 
 
 
abba06c
8c002de
 
 
 
 
 
 
 
abba06c
8c002de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abba06c
 
 
 
 
 
 
 
 
 
 
 
 
70da030
abba06c
 
70da030
abba06c
 
70da030
abba06c
70da030
 
 
abba06c
454e2e1
 
 
 
0a032b3
454e2e1
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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
<!DOCTYPE html>
<html dir="rtl" lang="ar">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>المنصة القانونية الذكية - وإنشاء العقود</title>
  <!-- ربط مكتبات Tailwind وFont Awesome -->
  <link href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet" />
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet" />
  <!-- ربط مكتبة jsPDF لتحويل النص إلى PDF -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
  <style>
    @import url("https://fonts.googleapis.com/css2?family=Tajawal:wght@400;500;700&display=swap");

    :root {
      --primary-color: #2563eb;
      --primary-hover: #1d4ed8;
      --background-light: #f8fafc;
      --card-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    }

    body {
      font-family: "Tajawal", sans-serif;
      background: var(--background-light);
    }

    /* الشريط الجانبي */
    .sidebar {
      background: white;
      width: 280px;
      position: fixed;
      right: 0;
      top: 0;
      height: 100vh;
      z-index: 40;
      transition: transform 0.3s ease;
      box-shadow: var(--card-shadow);
    }
    .main-content {
      margin-right: 280px;
      transition: margin 0.3s ease;
      min-height: 100vh;
      padding: 2rem;
      background: var(--background-light);
    }
    .nav-link {
      display: flex;
      align-items: center;
      padding: 1rem 1.5rem;
      color: #374151;
      transition: all 0.3s ease;
      border-radius: 0.5rem;
      margin: 0.25rem 0.5rem;
      cursor: pointer;
    }
    .nav-link:hover {
      background: #f3f4f6;
      color: var(--primary-color);
    }
    .nav-link.active {
      background: var(--primary-color);
      color: white;
    }
    .feature-card {
      background: white;
      border-radius: 1rem;
      box-shadow: var(--card-shadow);
      transition: all 0.3s ease;
      overflow: hidden;
      height: 100%;
    }
    .feature-card:hover {
      transform: translateY(-4px);
      box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    }
    .action-button {
      background: var(--primary-color);
      color: white;
      padding: 0.75rem 1.5rem;
      border-radius: 0.5rem;
      transition: all 0.3s ease;
      border: none;
      font-weight: 500;
      width: 100%;
      text-align: center;
    }
    .action-button:hover {
      background: var(--primary-hover);
      transform: translateY(-1px);
    }
    .input-group {
      position: relative;
    }
    .input-group.focused input,
    .input-group.focused textarea {
      border-color: var(--primary-color);
      transform: translateY(-2px);
    }
    .loading-spinner {
      border: 3px solid #f3f3f3;
      border-radius: 50%;
      border-top: 3px solid var(--primary-color);
      width: 24px;
      height: 24px;
      animation: spin 1s linear infinite;
      margin: 0 auto;
    }
    @keyframes spin {
      0% { transform: rotate(0deg); }
      100% { transform: rotate(360deg); }
    }
    .response-container {
      white-space: pre-wrap;
      font-family: "Tajawal", sans-serif;
      line-height: 1.6;
      max-height: 400px;
      overflow-y: auto;
    }
    /* تنسيق قسم إنشاء العقود */
    .split-screen {
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: 2rem;
      margin-top: 1rem;
    }
    .contract-card {
      background: white;
      border-radius: 1.5rem;
      box-shadow: var(--card-shadow);
      padding: 2rem;
      border: 1px solid #f3f4f6;
    }
    .contract-output {
      background: #fafafa;
      border-radius: 1rem;
      padding: 2rem;
      min-height: 300px;
      line-height: 1.8;
      font-size: 1.1rem;
      border: 1px solid #f3f4f6;
      white-space: pre-wrap;
    }
    .copy-btn {
      position: absolute;
      top: 1rem;
      left: 1rem;
      padding: 0.5rem 1rem;
      background: #f3f4f6;
      border-radius: 0.75rem;
      cursor: pointer;
      transition: all 0.3s ease;
      display: flex;
      align-items: center;
      gap: 0.5rem;
      font-size: 0.9rem;
      color: #4b5563;
    }
    .copy-btn:hover {
      background: #e5e7eb;
    }
    .success-message {
      position: fixed;
      bottom: 2rem;
      right: 2rem;
      background: #10b981;
      color: white;
      padding: 1rem 2rem;
      border-radius: 1rem;
      animation: slideIn 0.3s ease;
    }
    @keyframes slideIn {
      from { transform: translateX(100%); opacity: 0; }
      to { transform: translateX(0); opacity: 1; }
    }
    .section-title {
      font-size: 1.25rem;
      color: #1f2937;
      margin-bottom: 1.5rem;
      padding-bottom: 0.75rem;
      border-bottom: 2px solid #e5e7eb;
      font-weight: 600;
    }
    /* تحسين التنسيق على الأجهزة المحمولة */
    @media (max-width: 768px) {
      .sidebar { transform: translateX(100%); }
      .sidebar.open { transform: translateX(0); }
      .main-content { margin-right: 0; }
      .split-screen { grid-template-columns: 1fr; }
    }
  </style>
</head>
<body>
  <!-- زر إظهار/إخفاء الشريط الجانبي -->
  <button id="sidebarToggle" class="fixed top-4 right-4 z-50 p-2 rounded-lg bg-white shadow-lg md:hidden">
    <i class="fas fa-bars text-xl"></i>
  </button>

  <!-- الشريط الجانبي -->
  <aside class="sidebar">
    <div class="p-6">
      <div class="flex items-center gap-4 mb-8">
        <img src="https://ufastpro.com/wp-content/uploads/2025/02/تصميم-بدون-عنوان.png" alt="شعار المنصة" class="w-12 h-12 rounded-full" />
        <div>
          <h1 class="text-xl font-bold text-gray-800">المنصة القانونية</h1>
          <p class="text-sm text-gray-500">خدمات قانونية ذكية</p>
        </div>
      </div>
      <nav class="space-y-1">
        <a href="#" class="nav-link active" data-section="home">
          <i class="fas fa-home ml-3"></i>
          <span>الرئيسية</span>
        </a>
        <a href="#" class="nav-link" data-section="analysis">
          <i class="fas fa-balance-scale ml-3"></i>
          <span>تحليل القضايا</span>
        </a>
        <a href="#" class="nav-link" data-section="mindMap">
          <i class="fas fa-project-diagram ml-3"></i>
          <span>خريطة التفكير</span>
        </a>
        <a href="#" class="nav-link" data-section="search">
          <i class="fas fa-search ml-3"></i>
          <span>البحث القانوني</span>
        </a>
        <a href="#" class="nav-link" data-section="documents">
          <i class="fas fa-file-alt ml-3"></i>
          <span>المستندات</span>
        </a>
        <a href="#" class="nav-link" data-section="translation">
          <i class="fas fa-language ml-3"></i>
          <span>الترجمة القانونية</span>
        </a>
        <a href="#" class="nav-link" data-section="consultation">
          <i class="fas fa-comments ml-3"></i>
          <span>الاستشارة القانونية</span>
        </a>
        <a href="#" class="nav-link" data-section="contracts">
          <i class="fas fa-file-contract ml-3"></i>
          <span>العقود</span>
        </a>
        <!-- زر الأعمال الادارية: رابط خارجي -->
        <a href="https://example.com/administrative" target="_blank" class="nav-link">
          <i class="fas fa-briefcase ml-3"></i>
          <span>الأعمال الادارية</span>
        </a>
      </nav>
    </div>
  </aside>

  <!-- المحتوى الرئيسي -->
  <main class="main-content">
    <!-- بحث عالمي -->
    <div class="search-container mb-6 relative">
      <input type="text" class="search-input w-full p-3 rounded border" placeholder="ابحث في المنصة..." id="globalSearch" />
      <i class="fas fa-search absolute top-1/2 right-4 transform -translate-y-1/2 text-gray-400"></i>
    </div>

    <!-- قسم الرئيسية -->
    <section id="home" class="section">
      <h2 class="text-3xl font-bold text-gray-800 mb-8">الخدمات القانونية</h2>
      <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
        <!-- بطاقات الخدمة -->
        <div class="feature-card p-6">
          <div class="text-center">
            <i class="fas fa-balance-scale text-3xl mb-4 text-blue-600"></i>
            <h3 class="text-xl font-bold text-gray-800 mb-3">تحليل القضايا</h3>
            <p class="text-gray-600 mb-4">
              تحليل متكامل للقضايا والمستندات القانونية باستخدام الذكاء الاصطناعي.
            </p>
            <button class="action-button" onclick="showSection('analysis')">ابدأ التحليل</button>
          </div>
        </div>
        <div class="feature-card p-6">
          <div class="text-center">
            <i class="fas fa-project-diagram text-3xl mb-4 text-blue-600"></i>
            <h3 class="text-xl font-bold text-gray-800 mb-3">خريطة التفكير</h3>
            <p class="text-gray-600 mb-4">
              إنشاء خرائط ذهنية للقضايا القانونية وتحليل العلاقات.
            </p>
            <button class="action-button" onclick="showSection('mindMap')">إنشاء خريطة</button>
          </div>
        </div>
        <div class="feature-card p-6">
          <div class="text-center">
            <i class="fas fa-search text-3xl mb-4 text-blue-600"></i>
            <h3 class="text-xl font-bold text-gray-800 mb-3">البحث القانوني</h3>
            <p class="text-gray-600 mb-4">
              بحث متقدم في السوابق والقضايا القانونية مع عرض المصادر.
            </p>
            <button class="action-button" onclick="showSection('search')">ابدأ البحث</button>
          </div>
        </div>
        <div class="feature-card p-6">
          <div class="text-center">
            <i class="fas fa-file-alt text-3xl mb-4 text-blue-600"></i>
            <h3 class="text-xl font-bold text-gray-800 mb-3">المستندات</h3>
            <p class="text-gray-600 mb-4">
              إنشاء وتحرير المستندات القانونية بمساعدة الذكاء الاصطناعي.
            </p>
            <button class="action-button" onclick="showSection('documents')">إنشاء مستند</button>
          </div>
        </div>
        <div class="feature-card p-6">
          <div class="text-center">
            <i class="fas fa-language text-3xl mb-4 text-blue-600"></i>
            <h3 class="text-xl font-bold text-gray-800 mb-3">الترجمة القانونية</h3>
            <p class="text-gray-600 mb-4">
              ترجمة احترافية للوثائق القانونية مع إمكانية تنزيلها كملف PDF.
            </p>
            <button class="action-button" onclick="showSection('translation')">ترجم الآن</button>
          </div>
        </div>
        <div class="feature-card p-6">
          <div class="text-center">
            <i class="fas fa-comments text-3xl mb-4 text-blue-600"></i>
            <h3 class="text-xl font-bold text-gray-800 mb-3">الاستشارة القانونية</h3>
            <p class="text-gray-600 mb-4">
              احصل على استشارة قانونية فورية من خبرائنا.
            </p>
            <button class="action-button" onclick="showSection('consultation')">استشر الآن</button>
          </div>
        </div>
      </div>
    </section>

    <!-- قسم التحليل القانوني -->
    <section id="analysis" class="section hidden">
      <h2 class="text-3xl font-bold text-gray-800 mb-8">تحليل القضايا</h2>
      <div class="input-group bg-white rounded p-6 shadow">
        <!-- زر اختيار الصورة لتحليلها عبر OCR -->
        <button id="chooseImageButton" class="action-button">اختر صورة للرفع (OCR)</button>
        <!-- حقل رفع صورة مخفي -->
        <input type="file" id="ocrFile" accept="image/*" class="hidden">
        <!-- عرض النص المستخرج من الصورة -->
        <div id="ocrResult" class="mt-4 p-3 border rounded bg-gray-50" style="min-height: 50px;"></div>
        <!-- زر لإرسال النص المُستخرج للتحليل -->
        <button id="analyzeOCRButton" class="action-button mt-4" disabled>تحليل النص</button>
        <!-- عرض نتيجة تحليل النص مع كشف النية والحكم الطبيعي والقضايا المشابهة -->
        <div id="analysisResult" class="mt-4 response-container"></div>
      </div>
    </section>

    <!-- قسم إنشاء المستندات القانونية -->
    <section id="documents" class="section hidden">
      <h2 class="text-3xl font-bold text-gray-800 mb-8">إنشاء مستند قانوني</h2>
      <div class="input-group bg-white rounded p-6 shadow">
        <textarea class="w-full p-3 border rounded" placeholder="اكتب وصف المستند المطلوب إنشاؤه..."></textarea>
        <button class="action-button mt-4" onclick="handleSubmit('documents')">إنشاء المستند</button>
        <div id="documentsResult" class="mt-4 response-container"></div>
      </div>
    </section>

    <!-- قسم خريطة التفكير -->
    <section id="mindMap" class="section hidden">
      <h2 class="text-3xl font-bold text-gray-800 mb-8">خريطة التفكير القانونية</h2>
      <div class="input-group bg-white rounded p-6 shadow space-y-4">
        <textarea id="mindMapDescription" class="w-full p-3 border rounded" placeholder="اكتب وصف الموضوع الذي تريد إنشاء خريطة ذهنية له..."></textarea>
        <!-- حاوية الحقول الإضافية -->
        <div id="mindMapFields" class="space-y-4"></div>
        <button id="addMindMapField" class="action-button">أضف حقل فكرة</button>
        <button class="action-button mt-4" onclick="handleMindMap()">إنشاء خريطة التفكير</button>
        <div id="mindMapResult" class="mt-4 response-container"></div>
      </div>
    </section>

    <!-- قسم البحث القانوني -->
    <section id="search" class="section hidden">
      <h2 class="text-3xl font-bold text-gray-800 mb-8">البحث القانوني</h2>
      <div class="input-group bg-white rounded p-6 shadow space-y-4">
        <textarea id="searchQuery" class="w-full p-3 border rounded" placeholder="اكتب استعلام البحث القانوني..."></textarea>
        <!-- قائمة المصادر القانونية القابلة للتعديل -->
        <select id="legalSources" multiple class="p-3 rounded border w-full">
          <option value="المحكمة العليا">المحكمة العليا</option>
          <option value="مجلس القضاء">مجلس القضاء</option>
          <option value="وزارة العدل">وزارة العدل</option>
          <option value="الهيئة الوطنية للقضاء">الهيئة الوطنية للقضاء</option>
        </select>
        <button class="action-button" onclick="handleSubmit('search')">بحث</button>
        <div id="searchResult" class="mt-4 response-container"></div>
      </div>
    </section>

    <!-- قسم الترجمة القانونية -->
    <section id="translation" class="section hidden">
      <h2 class="text-3xl font-bold text-gray-800 mb-8">الترجمة القانونية</h2>
      <div class="input-group bg-white rounded p-6 shadow space-y-4">
        <textarea id="translationText" class="w-full p-3 border rounded" placeholder="اكتب النص المراد ترجمته..."></textarea>
        <!-- زر OCR ملف للترجمة -->
        <button id="chooseImageButtonTranslation" class="action-button">OCR ملف</button>
        <!-- حقل رفع صورة مخفي للترجمة -->
        <input type="file" id="translationOCRFile" accept="image/*" class="hidden">
        <div class="flex gap-4 my-4">
          <select id="fromLang" class="p-3 rounded border w-full">
            <option value="ar">العربية</option>
            <option value="en">الإنجليزية</option>
          </select>
          <select id="toLang" class="p-3 rounded border w-full">
            <option value="en">الإنجليزية</option>
            <option value="ar">العربية</option>
          </select>
        </div>
        <button class="action-button" onclick="handleSubmit('translation')">ترجمة</button>
        <div id="translationResult" class="mt-4 response-container"></div>
        <button id="downloadPDFButton" class="action-button mt-4">تنزيل الترجمة كملف PDF</button>
      </div>
    </section>

    <!-- قسم الاستشارة القانونية -->
    <section id="consultation" class="section hidden">
      <h2 class="text-3xl font-bold text-gray-800 mb-8">استشارة قانونية</h2>
      <div class="input-group bg-white rounded p-6 shadow">
        <textarea class="w-full p-3 border rounded" placeholder="اكتب سؤالك القانوني هنا..."></textarea>
        <button class="action-button mt-4" onclick="handleSubmit('consultation')">احصل على استشارة</button>
        <div id="consultationResult" class="mt-4 response-container"></div>
      </div>
    </section>

    <!-- قسم إنشاء العقود -->
    <section id="contracts" class="section hidden">
      <h2 class="text-3xl font-bold text-gray-800 mb-8">منشئ العقود المصري الذكي</h2>
      <div class="split-screen">
        <!-- نموذج بيانات العقد -->
        <div class="contract-card">
          <div class="space-y-6">
            <div class="mb-4 input-group">
              <label class="block text-gray-700 font-bold mb-2">عنوان العقد:</label>
              <input type="text" id="contractTitle" placeholder="عنوان العقد" class="w-full p-2 border rounded" />
            </div>
            <div class="mb-4 input-group">
              <label class="block text-gray-700 font-bold mb-2">تاريخ تحرير العقد:</label>
              <input type="date" id="contractDate" class="w-full p-2 border rounded" />
            </div>
            <h3 class="section-title">بيانات الطرف الأول</h3>
            <div class="mb-4 input-group">
              <label class="block text-gray-700 font-bold mb-2">اسم الطرف الأول:</label>
              <input type="text" id="firstPartyName" placeholder="الاسم الكامل" class="w-full p-2 border rounded" />
            </div>
            <div class="mb-4 input-group">
              <label class="block text-gray-700 font-bold mb-2">الرقم القومي للطرف الأول:</label>
              <input type="text" id="firstPartyID" placeholder="14 رقم" maxlength="14" class="w-full p-2 border rounded" />
            </div>
            <div class="mb-4 input-group">
              <label class="block text-gray-700 font-bold mb-2">الصفة:</label>
              <input type="text" id="firstPartyTitle" placeholder="مثال: مالك، مدير شركة" class="w-full p-2 border rounded" />
            </div>
            <div class="mb-4 input-group">
              <label class="block text-gray-700 font-bold mb-2">جنسية الطرف الأول:</label>
              <input type="text" id="firstPartyNationality" placeholder="الجنسية" value="مصري" class="w-full p-2 border rounded" />
            </div>
            <div class="mb-4 input-group">
              <label class="block text-gray-700 font-bold mb-2">عنوان الطرف الأول:</label>
              <input type="text" id="firstPartyAddress" placeholder="العنوان التفصيلي" class="w-full p-2 border rounded" />
            </div>
            <h3 class="section-title">بيانات الطرف الثاني</h3>
            <div class="mb-4 input-group">
              <label class="block text-gray-700 font-bold mb-2">اسم الطرف الثاني:</label>
              <input type="text" id="secondPartyName" placeholder="الاسم الكامل" class="w-full p-2 border rounded" />
            </div>
            <div class="mb-4 input-group">
              <label class="block text-gray-700 font-bold mb-2">الرقم القومي للطرف الثاني:</label>
              <input type="text" id="secondPartyID" placeholder="14 رقم" maxlength="14" class="w-full p-2 border rounded" />
            </div>
            <div class="mb-4 input-group">
              <label class="block text-gray-700 font-bold mb-2">الصفة:</label>
              <input type="text" id="secondPartyTitle" placeholder="مثال: مستأجر، مشتري" class="w-full p-2 border rounded" />
            </div>
            <div class="mb-4 input-group">
              <label class="block text-gray-700 font-bold mb-2">جنسية الطرف الثاني:</label>
              <input type="text" id="secondPartyNationality" placeholder="الجنسية" value="مصري" class="w-full p-2 border rounded" />
            </div>
            <div class="mb-4 input-group">
              <label class="block text-gray-700 font-bold mb-2">عنوان الطرف الثاني:</label>
              <input type="text" id="secondPartyAddress" placeholder="العنوان التفصيلي" class="w-full p-2 border rounded" />
            </div>
            <h3 class="section-title">تفاصيل العقد</h3>
            <div class="mb-4 input-group">
              <label class="block text-gray-700 font-bold mb-2">البند التمهيدي:</label>
              <textarea id="contractPreamble" placeholder="اكتب مضمون العقد والغرض منه" class="w-full p-2 border rounded h-24"></textarea>
            </div>
            <div class="mb-4 input-group">
              <label class="block text-gray-700 font-bold mb-2">بنود العقد:</label>
              <textarea id="contractTerms" placeholder="اكتب بنود العقد الأساسية" class="w-full p-2 border rounded h-48"></textarea>
            </div>
          </div>
          <div class="mt-4 text-center">
            <button id="generateButton" class="action-button">إنشاء العقد</button>
          </div>
        </div>
        <!-- عرض العقد المنشأ مع إمكانية النسخ -->
        <div class="contract-card relative">
          <h3 class="text-2xl font-bold text-gray-800 mb-4">العقد المنشأ</h3>
          <button class="copy-btn" id="copyButton">
            <i class="fas fa-copy"></i> نسخ العقد
          </button>
          <div id="contractOutput" class="contract-output"></div>
        </div>
      </div>
    </section>
  </main>

  <!-- سكربتات الموقع -->
  <script>
    // تعريف API الأساسي (للنصوص وطلبات التحليل)
    const API_URL = 'https://cprh6kne8u0lps-7777.proxy.runpod.net/proxy/8000/chat';
    const UPLOAD_URL = 'https://cprh6kne8u0lps-7777.proxy.runpod.net/proxy/8000/upload';

    // إعدادات OCR باستخدام API4AI (الوضع التجريبي)
    const OCR_MODE = 'demo';
    const OCR_OPTIONS = {
      demo: {
        url: 'https://demo.api4ai.cloud/ocr/v1/results',
        headers: { 'A4A-CLIENT-APP-ID': 'sample' }
      },
      rapidapi: {
        url: 'https://ocr43.p.rapidapi.com/v1/results',
        headers: { 'X-RapidAPI-Key': '' }
      }
    };

    // دالة استخراج النص من الصورة باستخدام OCR
    function doOCR(file) {
      return new Promise((resolve, reject) => {
        let form = new FormData();
        form.append('image', file);
        fetch(OCR_OPTIONS[OCR_MODE].url, {
          method: 'POST',
          body: form,
          headers: OCR_OPTIONS[OCR_MODE].headers
        })
        .then(response => response.json())
        .then(data => {
          try {
            const extractedText = data.results[0].entities[0].objects[0].entities[0].text;
            resolve(extractedText);
          } catch (e) {
            reject('خطأ في استخراج النص من الصورة');
          }
        })
        .catch(err => reject(err));
      });
    }

    // دالة الحصول على البادئة (prompt prefix) بناءً على القسم
    function getPromptPrefix(section) {
      switch(section) {
        case 'analysis':
          return 'قم بتحليل هذه القضية: ';
        case 'documents':
          return 'قم بإنشاء مستند قانوني بناءً على الوصف التالي: ';
        case 'mindMap':
          return 'قم بإنشاء خريطة ذهنية لحل القضية: ';
        case 'search':
          return 'ابحث عن معلومات قانونية حول: ';
        case 'translation':
          return 'قم بترجمة هذا النص القانوني: ';
        case 'consultation':
          return 'أحتاج استشارة قانونية في الموضوع التالي: ';
        default:
          return '';
      }
    }

    // تفعيل التنقل بين الأقسام
    document.querySelectorAll(".nav-link").forEach(link => {
      link.addEventListener("click", (e) => {
        e.preventDefault();
        const section = e.currentTarget.getAttribute("data-section");
        if(section) {
          showSection(section);
          // إخفاء الشريط الجانبي على الأجهزة المحمولة بعد النقر
          if(window.innerWidth < 768) {
            document.querySelector(".sidebar").classList.remove("open");
          }
        }
      });
    });

    function showSection(sectionId) {
      document.querySelectorAll(".section").forEach(section => {
        section.classList.add("hidden");
      });
      document.getElementById(sectionId).classList.remove("hidden");
      document.querySelectorAll(".nav-link").forEach(link => {
        link.classList.remove("active");
        if (link.getAttribute("data-section") === sectionId) {
          link.classList.add("active");
        }
      });
    }

    // زر القائمة على الهاتف
    document.getElementById("sidebarToggle").addEventListener("click", () => {
      document.querySelector(".sidebar").classList.toggle("open");
    });

    // تحسين تأثير التركيز على حقول الإدخال
    document.querySelectorAll("input, textarea").forEach(element => {
      element.addEventListener("focus", function() {
        const group = this.closest(".input-group");
        if(group) group.classList.add("focused");
        this.style.transform = 'translateY(-2px)';
      });
      
      element.addEventListener("blur", function() {
        const group = this.closest(".input-group");
        if(group) group.classList.remove("focused");
        this.style.transform = 'translateY(0)';
      });
    });

    // دالة إرسال الطلبات للأقسام المختلفة
    async function handleSubmit(section) {
      const container = document.getElementById(section);
      let resultDiv;
      let payload = { history: [] };
      
      // خاص للتعامل مع الأقسام التي تحتوي على ملف أو نص
      if (section === 'analysis') {
        resultDiv = container.querySelector(".response-container");
        // التعامل مع رفع ملف (PDF أو صورة)
        const fileInput = container.querySelector("#ocrFile");
        const textArea = container.querySelector("textarea");
        const text = textArea ? textArea.value.trim() : "";
        resultDiv.innerHTML = '';
        let filePromise = null, textPromise = null;
        
        if (fileInput && fileInput.files && fileInput.files.length > 0) {
          const file = fileInput.files[0];
          let formData = new FormData();
          formData.append("file", file);
          resultDiv.innerHTML += '<div id="fileAnalysisSpinner" class="loading-spinner"></div>';
          filePromise = fetch(UPLOAD_URL, {
            method: "POST",
            body: formData
          })
          .then(response => {
            if (!response.ok) {
              throw new Error("فشل رفع الملف");
            }
            return response.json();
          })
          .then(data => data.response || "تم تحليل الملف بنجاح")
          .catch(error => {
            console.error("File analysis error:", error);
            return '<div class="text-red-500">حدث خطأ في رفع الملف. يرجى المحاولة مرة أخرى.</div>';
          });
        }
        
        if (text) {
          resultDiv.innerHTML += '<div id="textAnalysisSpinner" class="loading-spinner"></div>';
          payload.message = getPromptPrefix(section) + text;
          textPromise = fetch(API_URL, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(payload)
          })
          .then(response => {
            if (!response.ok) {
              throw new Error("فشل الاستجابة من الخادم");
            }
            return response.json();
          })
          .then(data => data.response || "")
          .catch(error => {
            console.error("Text analysis error:", error);
            return '<div class="text-red-500">حدث خطأ في تحليل النص. يرجى المحاولة مرة أخرى.</div>';
          });
        }
        
        const fileResult = filePromise ? await filePromise : "";
        const textResult = textPromise ? await textPromise : "";
        
        resultDiv.innerHTML = '';
        if (fileResult) resultDiv.innerHTML += `<div><strong>نتيجة تحليل الملف:</strong><br>${fileResult}</div><br>`;
        if (textResult) resultDiv.innerHTML += `<div><strong>نتيجة تحليل النص:</strong><br>${textResult}</div>`;
        if (!fileResult && !textResult) {
          alert("الرجاء إدخال نص أو إرفاق ملف");
          return;
        }
        
        // دمج النتائج للتحليل
        let combinedText = "";
        if (typeof fileResult === "string") combinedText += fileResult + "\n";
        if (typeof textResult === "string") combinedText += textResult + "\n";
        
        // خطوة كشف نية القضية
        resultDiv.innerHTML += '<div id="intentSpinner" class="loading-spinner"></div>';
        try {
          const intentPayload = {
            message: "كشف نية لهذه القضية (مثل: قتل، سرقة) بناءً على النص التالي:\n" + combinedText,
            history: []
          };
          const intentResponse = await fetch(API_URL, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(intentPayload)
          });
          if (!intentResponse.ok) throw new Error("فشل الكشف عن نية القضية");
          const intentData = await intentResponse.json();
          const caseNature = intentData.response || "غير محدد";
          resultDiv.innerHTML += `<br><div><strong>كشف نية القضية:</strong> ${caseNature}</div>`;
          
          // البحث عن الحكم الطبيعي للقضية
          resultDiv.innerHTML += '<div id="rulingSpinner" class="loading-spinner"></div>';
          const searchPayload = {
            message: "ابحث عن حكم طبيعي لقضية " + caseNature,
            history: []
          };
          const rulingResponse = await fetch(API_URL, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(searchPayload)
          });
          if (!rulingResponse.ok) throw new Error("فشل الحصول على الحكم الطبيعي");
          const rulingData = await rulingResponse.json();
          const ruling = rulingData.response || "";
          resultDiv.innerHTML += `<br><div><strong>الحكم الطبيعي:</strong><br>${ruling}</div>`;
          
          // البحث عن قضايا مشابهة
          resultDiv.innerHTML += '<div id="similarSpinner" class="loading-spinner"></div>';
          const similarPayload = {
            message: "ابحث عن قضايا مشابهة لقضية " + caseNature,
            history: []
          };
          const similarResponse = await fetch(API_URL, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(similarPayload)
          });
          if (!similarResponse.ok) throw new Error("فشل البحث عن القضايا المشابهة");
          const similarData = await similarResponse.json();
          const similarCases = similarData.response || "";
          resultDiv.innerHTML += `<br><div><strong>القضايا المشابهة:</strong><br>${similarCases}</div>`;
        } catch (error) {
          console.error("Error in intent/ruling analysis:", error);
          resultDiv.innerHTML += `<br><div class="text-red-500">حدث خطأ أثناء الكشف عن نية القضية أو البحث عن الحكم الطبيعي/القضايا المشابهة.</div>`;
        }
      }
      else if (section === 'translation') {
        resultDiv = document.getElementById("translationResult");
        const textArea = document.getElementById("translationText");
        const text = textArea.value.trim();
        if (!text) {
          alert("الرجاء إدخال نص للترجمة");
          return;
        }
        // إذا كان البحث في الترجمة لا يحتاج لملفات، نقوم بإرسال النص فقط
        payload.message = getPromptPrefix(section) + text;
        resultDiv.innerHTML = '<div class="loading-spinner"></div>';
        try {
          const response = await fetch(API_URL, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(payload)
          });
          if (!response.ok) throw new Error("فشل الاستجابة من الخادم");
          const data = await response.json();
          resultDiv.innerHTML = data.response || "";
        } catch (error) {
          console.error("Error:", error);
          resultDiv.innerHTML = '<div class="text-red-500">حدث خطأ. يرجى المحاولة مرة أخرى.</div>';
        }
      }
      else if (section === 'search') {
        resultDiv = document.getElementById("searchResult");
        const queryText = document.getElementById("searchQuery").value.trim();
        if (!queryText) {
          alert("الرجاء إدخال استعلام البحث");
          return;
        }
        // الحصول على المصادر القانونية المختارة
        const sourcesSelect = document.getElementById("legalSources");
        const selectedSources = Array.from(sourcesSelect.selectedOptions).map(opt => opt.value).join(", ");
        let extra = selectedSources ? " المصادر المستخدمة: " + selectedSources : "";
        payload.message = getPromptPrefix(section) + queryText + extra;
        resultDiv.innerHTML = '<div class="loading-spinner"></div>';
        try {
          const response = await fetch(API_URL, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(payload)
          });
          if (!response.ok) throw new Error("فشل الاستجابة من الخادم");
          const data = await response.json();
          resultDiv.innerHTML = data.response || "";
        } catch (error) {
          console.error("Error:", error);
          resultDiv.innerHTML = '<div class="text-red-500">حدث خطأ. يرجى المحاولة مرة أخرى.</div>';
        }
      }
      else {
        // للأقسام الأخرى (documents, consultation، الخ)
        resultDiv = container.querySelector(".response-container") || container.querySelector("#consultationResult");
        const textarea = container.querySelector("textarea");
        const text = textarea.value.trim();
        if (!text) {
          alert("الرجاء إدخال نص");
          return;
        }
        payload.message = getPromptPrefix(section) + text;
        resultDiv.innerHTML = '<div class="loading-spinner"></div>';
        try {
          const response = await fetch(API_URL, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(payload)
          });
          if (!response.ok) throw new Error("فشل الاستجابة من الخادم");
          const data = await response.json();
          resultDiv.innerHTML = data.response || "";
        } catch (error) {
          console.error("Error:", error);
          resultDiv.innerHTML = '<div class="text-red-500">حدث خطأ. يرجى المحاولة مرة أخرى.</div>';
        }
      }
    }

    // وظيفة زر النسخ في قسم إنشاء العقود
    document.getElementById("copyButton").addEventListener("click", async function() {
      const contractText = document.getElementById("contractOutput").innerText;
      try {
        await navigator.clipboard.writeText(contractText);
        showSuccessMessage("تم نسخ العقد بنجاح!");
      } catch (err) {
        console.error("فشل نسخ النص:", err);
      }
    });

    // وظيفة إظهار رسالة النجاح عند النسخ
    function showSuccessMessage(message) {
      const successMessage = document.createElement("div");
      successMessage.className = "success-message";
      successMessage.textContent = message;
      document.body.appendChild(successMessage);
      setTimeout(() => {
        successMessage.style.opacity = "0";
        setTimeout(() => successMessage.remove(), 300);
      }, 2000);
    }

    // التحقق من صحة الرقم القومي للطرف الأول والثاني
    document.querySelectorAll("#firstPartyID, #secondPartyID").forEach(input => {
      input.addEventListener("input", function() {
        this.value = this.value.replace(/[^0-9]/g, '');
        if (this.value.length > 14) this.value = this.value.slice(0, 14);
      });
    });

    // وظيفة زر إنشاء العقد
    document.getElementById("generateButton").addEventListener("click", async function() {
      const button = this;
      const outputDiv = document.getElementById("contractOutput");
      const formData = {
        contractTitle: document.getElementById("contractTitle").value.trim(),
        contractDate: document.getElementById("contractDate").value,
        firstPartyName: document.getElementById("firstPartyName").value.trim(),
        firstPartyID: document.getElementById("firstPartyID").value.trim(),
        firstPartyTitle: document.getElementById("firstPartyTitle").value.trim(),
        firstPartyNationality: document.getElementById("firstPartyNationality").value.trim(),
        firstPartyAddress: document.getElementById("firstPartyAddress").value.trim(),
        secondPartyName: document.getElementById("secondPartyName").value.trim(),
        secondPartyID: document.getElementById("secondPartyID").value.trim(),
        secondPartyTitle: document.getElementById("secondPartyTitle").value.trim(),
        secondPartyNationality: document.getElementById("secondPartyNationality").value.trim(),
        secondPartyAddress: document.getElementById("secondPartyAddress").value.trim(),
        contractPreamble: document.getElementById("contractPreamble").value.trim(),
        contractTerms: document.getElementById("contractTerms").value.trim()
      };

      const requiredFields = Object.keys(formData);
      const missingFields = requiredFields.filter(field => !formData[field]);
      if (missingFields.length > 0) {
        outputDiv.innerHTML = `
          <div class="text-red-500 font-bold mb-4">يرجى ملء جميع الحقول المطلوبة</div>
          <ul class="list-disc list-inside text-red-500">
            ${missingFields.map(field => `<li>${field}</li>`).join('')}
          </ul>
        `;
        return;
      }

      button.textContent = 'جاري إنشاء العقد...';
      button.disabled = true;
      outputDiv.innerHTML = `
        <div class="flex items-center justify-center space-x-4 text-gray-600">
          <div class="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600"></div>
          <span>جاري إنشاء العقد باستخدام الذكاء الاصطناعي...</span>
        </div>
      `;

      try {
        const dateObj = new Date(formData.contractDate);
        const arabicDate = new Intl.DateTimeFormat('ar-EG', {
          year: 'numeric',
          month: 'long',
          day: 'numeric'
        }).format(dateObj);

        const prompt = `قم بإنشاء عقد قانوني مصري رسمي باللغة العربية يتضمن البيانات التالية:

عنوان العقد: ${formData.contractTitle}
تاريخ العقد: ${arabicDate}

الطرف الأول:
- الاسم: ${formData.firstPartyName}
- الرقم القومي: ${formData.firstPartyID}
- الصفة: ${formData.firstPartyTitle}
- الجنسية: ${formData.firstPartyNationality}
- العنوان: ${formData.firstPartyAddress}

الطرف الثاني:
- الاسم: ${formData.secondPartyName}
- الرقم القومي: ${formData.secondPartyID}
- الصفة: ${formData.secondPartyTitle}
- الجنسية: ${formData.secondPartyNationality}
- العنوان: ${formData.secondPartyAddress}

التمهيد:
${formData.contractPreamble}

بنود العقد:
${formData.contractTerms}

يجب أن يتضمن العقد:
1. النص القانوني: "وبعد أن أقر طرفي هذا العقد بكامل أهليتهما المعتبرة قانوناً وشرعاً وبخلو إرادتهما من أي عيب من عيوب الإرادة وبأنهما غير ممنوعين من التصرف وغير محجور عليهما لأي سبب من الأسباب وعدم خضوعهم لقانون الحراسات أو الكسب غير المشروع وبخلوهما من كافة الموانع القانونية والشرعية اللازمة للتعاقد اتفقوا على الآتي"
2. بند حكم التمهيد يوضح أن التمهيد جزء لا يتجزأ من العقد
3. تنسيق قانوني رسمي
4. مكان للتوقيع في النهاية مع كتابة "طرف أول" و "طرف ثاني"`;

        const response = await fetch(API_URL, {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ message: prompt, history: [] })
        });
        if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
        const data = await response.json();
        outputDiv.innerHTML = `<div dir="rtl" style="white-space: pre-wrap;">${data.response}</div>`;
      } catch (error) {
        outputDiv.innerHTML = `
          <div class="text-red-600 font-bold">
            عذراً، حدث خطأ في إنشاء العقد. يرجى المحاولة مرة أخرى.
            <br>
            تفاصيل الخطأ: ${error.message}
          </div>
        `;
        console.error('Error:', error);
      } finally {
        button.textContent = 'إنشاء العقد';
        button.disabled = false;
      }
    });

    // وظيفة تنزيل الترجمة كملف PDF باستخدام مكتبة jsPDF
    document.getElementById("downloadPDFButton").addEventListener("click", function() {
      const { jsPDF } = window.jspdf;
      const doc = new jsPDF({ orientation: "portrait", unit: "pt", format: "a4" });
      const content = document.getElementById("translationResult").innerText;
      if (!content.trim()) {
        alert("لا يوجد نص للترجمة لتنزيله");
        return;
      }
      doc.setFont("Tajawal", "normal");
      doc.text(content, 40, 60, { maxWidth: 515 });
      doc.save("translation.pdf");
    });

    // وظيفة تنزيل تحليل القضايا كملف PDF باستخدام مكتبة jsPDF
    document.getElementById("downloadAnalysisPDFButton")?.addEventListener("click", function() {
      const { jsPDF } = window.jspdf;
      const doc = new jsPDF({ orientation: "portrait", unit: "pt", format: "a4" });
      const content = document.getElementById("analysisResult").innerText;
      if (!content.trim()) {
        alert("لا يوجد نص للتحليل لتنزيله");
        return;
      }
      doc.setFont("Tajawal", "normal");
      doc.text(content, 40, 60, { maxWidth: 515 });
      doc.save("analysis.pdf");
    });

    // وظيفة إضافة حقل جديد في قسم خريطة التفكير
    document.getElementById("addMindMapField").addEventListener("click", function() {
      const container = document.getElementById("mindMapFields");
      const newFieldDiv = document.createElement("div");
      newFieldDiv.className = "mb-4";
      newFieldDiv.innerHTML = `<label class="block text-gray-700 font-bold mb-2">حقل فكرة:</label>
         <input type="text" class="w-full p-3 border rounded" placeholder="اكتب فكرة جديدة" />`;
      container.appendChild(newFieldDiv);
    });

    // دالة إنشاء خريطة التفكير باستخدام الحقول المتعددة
    function handleMindMap() {
      const inputs = document.querySelectorAll("#mindMapFields input");
      let collectedText = "";
      inputs.forEach((input, index) => {
        const value = input.value.trim();
        if(value) {
          collectedText += `الفكرة ${index + 1}: ${value}\n`;
        }
      });
      if (!collectedText.trim()) {
        alert("الرجاء إدخال بعض الأفكار لإنشاء خريطة التفكير");
        return;
      }
      const resultDiv = document.getElementById("mindMapResult");
      const prompt = getPromptPrefix("mindMap") + "\n" + collectedText +
                     "\nاستخدم منهجيات مثل تحليل SWOT و5 خطوات لتحليل الأسباب الجذرية لتنظيم القضية بشكل احترافي.";
      resultDiv.innerHTML = '<div class="loading-spinner"></div>';
      fetch(API_URL, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ message: prompt, history: [] })
      })
      .then(response => response.json())
      .then(data => {
        resultDiv.innerHTML = data.response || "";
      })
      .catch(error => {
        console.error("Error:", error);
        resultDiv.innerHTML = '<div class="text-red-500">حدث خطأ. يرجى المحاولة مرة أخرى.</div>';
      });
    }

    // وظيفة رفع ملف OCR في قسم الترجمة: عند الضغط على زر "OCR ملف"
    document.getElementById("chooseImageButtonTranslation").addEventListener("click", function() {
      document.getElementById("translationOCRFile").click();
    });
    // عند اختيار ملف في قسم الترجمة يتم استخراج النص ووضعه في حقل الترجمة
    document.getElementById("translationOCRFile").addEventListener("change", async function() {
      const file = this.files[0];
      if (!file) return;
      const textArea = document.getElementById("translationText");
      textArea.value = "";
      // إظهار مؤشر التحميل مؤقتاً
      const originalPlaceholder = textArea.placeholder;
      textArea.placeholder = "جارٍ استخراج النص من الصورة...";
      try {
        const ocrText = await doOCR(file);
        textArea.value = ocrText;
      } catch (error) {
        console.error("Error in translation OCR:", error);
        alert("حدث خطأ أثناء استخراج النص من الصورة");
      } finally {
        textArea.placeholder = originalPlaceholder;
      }
    });

    // تهيئة الصفحة بالقسم الافتراضي "home"
    document.addEventListener("DOMContentLoaded", () => {
      showSection("home");
    });
  </script>
</body>
</html>