-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdong.html
More file actions
1445 lines (1248 loc) · 53.7 KB
/
Copy pathdong.html
File metadata and controls
1445 lines (1248 loc) · 53.7 KB
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
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>دنگگیری دورهمی</title>
<style>
:root {
direction: rtl;
}
body {
font-family: sans-serif;
margin: 0;
padding: 0;
background-color: #f5f5f5;
direction: rtl;
text-align: right;
}
header {
background-color: #28a745;
color: white;
padding: 1rem;
text-align: center;
position: relative;
/* Added padding-bottom to give space for sokhaneRooz */
padding-bottom: 2.5rem;
}
header h1 {
font-size: 1.8rem;
margin: 0;
padding-top: 10px;
}
#clock {
color: white;
position: absolute;
top: 0.1rem;
left: 50%;
transform: translateX(-50%);
font-size: 0.8rem;
width: 90%;
text-align: center; /* Ensure clock is centered */
}
/* New style for Sokhane Rooz */
#sokhaneRooz {
color: white;
text-align: center;
font-size: 0.9rem;
margin-top: 1.5rem; /* Space between clock and proverb */
width: 100%;
position: absolute; /* Position relative to header */
bottom: 0.5rem; /* Place it at the bottom of the header */
left: 50%;
transform: translateX(-50%);
}
main {
padding: 1rem;
}
.form-group {
display: flex;
flex-direction: column;
gap: 0.5rem;
margin-bottom: 1rem;
}
input[type="text"],
input[type="number"] {
padding: 0.5rem;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
padding: 0.5rem;
border: none;
border-radius: 5px;
background-color: #28a745;
color: white;
cursor: pointer;
margin-top: 0.5rem;
}
button:hover {
background-color: #218838;
}
.button-row {
display: flex;
gap: 0.5rem;
/* Space between buttons */
margin-top: 1rem;
justify-content: flex-start;
/* Align buttons to the right (due to rtl) */
flex-wrap: wrap;
/* Allow wrapping for smaller screens */
}
.button-row button {
flex-grow: 1;
/* Allow buttons to grow and fill space */
max-width: 150px;
/* Limit max width for larger screens */
margin-top: 0;
/* Override default margin-top from general button style */
}
.person-list {
margin-top: 1rem;
}
.person {
padding: 1rem;
margin-bottom: 1rem;
border-radius: 5px;
}
.item {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 0.5rem;
padding: 0.5rem 0;
border-bottom: 1px dashed #eee;
}
.item:last-child {
border-bottom: none;
}
.total {
font-weight: bold;
margin-top: 1rem;
}
/* Style for individual person's total */
.person-total {
font-size: 0.8em;
font-weight: bold;
margin-top: 1rem;
padding-top: 0.5rem;
border-top: 1px solid #ccc;
text-align: right;
padding-right: 0.5rem;
}
.result {
margin-top: 2rem;
padding: 1rem;
background-color: white;
border-radius: 5px;
}
/* Toast Message Styles */
#toast-container {
position: fixed;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
z-index: 1000;
display: flex;
flex-direction: column;
align-items: center;
}
.toast-message {
background-color: rgba(0, 0, 0, 0.7);
color: white;
padding: 10px 20px;
border-radius: 5px;
margin-bottom: 10px;
opacity: 1;
transition: opacity 0.5s ease-out;
text-align: center;
min-width: 200px;
}
.toast-message.fade-out {
opacity: 0;
}
/* Editable Spans */
.person-name-display,
.item-name-display,
.item-price-display {
cursor: pointer;
text-decoration: underline dotted #666;
}
.person-name-display:hover,
.item-name-display:hover,
.item-price-display:hover {
color: #007bff;
}
/* Specific styles for inputs replacing spans */
.item input {
flex-grow: 1;
}
/* Responsive styles */
@media (max-width: 600px) {
.item {
flex-direction: column;
align-items: stretch;
}
.item span {
margin-bottom: 0.2rem;
}
.item button {
width: 100%;
margin-top: 0.5rem;
}
.button-row {
flex-direction: column;
align-items: stretch;
}
.button-row button {
max-width: none;
}
}
/* Print styles */
@media print {
body {
color: black !important;
}
/* Make header visible and style its contents for print */
header {
display: block !important; /* Ensure header itself is visible */
padding-bottom: 1rem !important;
background-color: transparent !important; /* No background in print */
color: black !important; /* Ensure text is black */
text-align: center !important; /* Center content in header */
position: relative !important; /* Maintain positioning context */
}
header h1 {
display: none !important; /* Hide the main H1 title */
}
#clock {
display: block !important;
text-align: left !important;
font-size: 10pt !important;
margin-bottom: 0.5cm !important;
color: black !important;
position: static !important; /* Reset absolute positioning */
transform: none !important;
width: auto !important;
}
#sokhaneRooz {
display: block !important; /* Ensure Sokhane Rooz is visible */
text-align: center !important;
font-size: 0.9rem !important;
margin-top: 0.5rem !important;
margin-bottom: 1rem !important; /* Add bottom margin */
color: black !important;
position: static !important;
transform: none !important;
width: auto !important;
}
footer,
.button-row,
button,
.form-group label:not([for="eventNameInput"]),
.form-group input:not(#eventNameInput),
#toast-container,
#historyPanel,
/* Hide history panel during print */
#modalOverlay,
/* Hide custom modals during print */
#customModal
{
display: none !important;
}
/* For editable spans, remove interactive styles, ensure they are visible and black */
.person-name-display,
.item-name-display,
.item-price-display {
text-decoration: none !important;
cursor: default !important;
color: black !important;
display: inline !important;
}
/* Show event name in print */
#eventNameInput {
display: block !important;
border: none !important;
padding: 0 !important;
margin-bottom: 0.5cm !important;
font-size: 14pt !important;
font-weight: bold !important;
color: black !important;
text-align: center !important;
}
#eventNameInput::placeholder {
color: black;
}
label[for="eventNameInput"] {
display: none !important;
}
/* Adjust person/item display for print */
.person {
background-color: #f0f0f0 !important;
-webkit-print-color-adjust: exact;
color-adjust: exact;
border: 1px solid #ccc;
margin-bottom: 0.5cm;
}
.item {
border-bottom: 1px dashed #ddd;
}
body {
margin: 0;
padding: 0;
font-size: 12pt;
}
main {
padding: 0.5cm;
}
/* Ensure the "no items purchased" message is visible in print */
.person div[id^="itemList-"] p {
display: block !important;
margin: 0.5rem 0;
color: black !important;
}
/* Ensure person total is visible and styled correctly in print */
.person-total {
display: block !important;
color: black !important;
}
}
/* History Panel Styles */
#historyPanel {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
border: 1px solid #ccc;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
z-index: 1001;
width: 90%;
max-width: 500px;
max-height: 80%;
overflow-y: auto;
display: none;
/* Hidden by default */
padding: 1rem;
}
#historyPanel h2 {
margin-top: 0;
margin-bottom: 1rem;
color: #28a745;
text-align: center;
}
#historyPanel .history-list {
list-style: none;
padding: 0;
margin: 0;
}
#historyPanel .history-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0.75rem;
border-bottom: 1px solid #eee;
}
#historyPanel .history-item:last-child {
border-bottom: none;
}
#historyPanel .history-item div {
flex-grow: 1;
}
#historyPanel .history-item span {
display: block;
}
#historyPanel .history-item .item-date {
font-size: 0.8em;
color: #666;
margin-top: 0.2rem;
}
#historyPanel .history-item .item-buttons {
display: flex;
gap: 0.5rem;
}
#historyPanel .history-item button {
padding: 0.4rem 0.8rem;
font-size: 0.9em;
margin-top: 0;
/* Reset general button margin-top */
}
#historyPanel .history-item button.load-btn {
background-color: #007bff;
}
#historyPanel .history-item button.load-btn:hover {
background-color: #0056b3;
}
#historyPanel .history-item button.delete-btn {
background-color: #dc3545;
}
#historyPanel .history-item button.delete-btn:hover {
background-color: #c82333;
}
#historyPanel .close-btn {
background-color: #6c757d;
margin-top: 1rem;
width: 100%;
}
#historyPanel .close-btn:hover {
background-color: #5a6268;
}
#historyPanel p.no-history {
text-align: center;
color: #888;
margin-top: 1rem;
}
/* Custom Modal Styles (for alert/confirm) */
#modalOverlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: none;
/* Hidden by default */
justify-content: center;
align-items: center;
z-index: 2000;
}
#customModal {
background-color: white;
padding: 2rem;
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
max-width: 400px;
width: 90%;
text-align: center;
display: none;
/* Hidden by default, controlled by JS along with overlay */
}
#customModal h3 {
margin-top: 0;
color: #28a745;
margin-bottom: 1rem;
}
#customModal p {
margin-bottom: 1.5rem;
line-height: 1.5;
}
#customModal .modal-buttons {
display: flex;
justify-content: center;
gap: 1rem;
}
#customModal .modal-buttons button {
padding: 0.6rem 1.5rem;
margin-top: 0;
/* Reset general button margin-top */
flex-grow: 1;
/* Allow buttons to grow */
max-width: 120px;
/* Limit max width */
}
#customModal .modal-buttons button.confirm-btn {
background-color: #28a745;
}
#customModal .modal-buttons button.confirm-btn:hover {
background-color: #218838;
}
#customModal .modal-buttons button.cancel-btn {
background-color: #6c757d;
}
#customModal .modal-buttons button.cancel-btn:hover {
background-color: #5a6268;
}
#customModal .modal-buttons button.alert-ok-btn {
background-color: #007bff;
}
#customModal .modal-buttons button.alert-ok-btn:hover {
background-color: #0056b3;
}
/* New button colors for main actions */
.save-btn {
background-color: #ffc107;
/* Orange */
color: #343a40;
/* Darker text for contrast */
}
.save-btn:hover {
background-color: #e0a800;
}
.history-toggle-btn {
background-color: #17a2b8;
/* Cyan/Info blue */
}
.history-toggle-btn:hover {
background-color: #138496;
}
/* Ensure deleteAllData maintains its red color */
.delete-all-btn {
background-color: #dc3545;
}
.delete-all-btn:hover {
background-color: #c82333;
}
.quantity-controls {
display: flex;
align-items: center;
gap: 0.5rem;
margin-left: 1rem;
}
.quantity-controls button {
padding: 0.2rem 0.5rem;
margin: 0;
font-size: 0.9rem;
min-width: 30px;
}
.quantity-display {
min-width: 30px;
text-align: center;
}
.item-details {
display: flex;
align-items: center;
gap: 1rem;
}
</style>
</head>
<body>
<header>
<h1>دنگگیری دورهمی</h1>
<div id="clock"></div>
<div id="sokhaneRooz">
<span style="font-weight: bold;">سخن روز:</span> <span id="sokhaneRoozText"></span>
</div>
</header>
<main>
<div class="form-group">
<label for="eventNameInput">نام دورهمی:</label>
<input type="text" id="eventNameInput" placeholder="دنگ دورهمی">
</div>
<div class="form-group">
<label for="personName">نام شخص:</label>
<input type="text" id="personName" placeholder="نام شخص">
<button onclick="addPerson()" style="background-color: #23917d; color: white; cursor: pointer;">افزودن شخص</button>
</div>
<div class="person-list" id="personList">
<!-- افراد اینجا اضافه میشوند -->
</div>
<div class="button-row">
<button onclick="calculateDebts()" style="background-color: #003366; color: white; cursor: pointer;">محاسبه دنگ</button>
<button onclick="saveGathering()" class="save-btn">ذخیره دورهمی</button>
<button onclick="showHistoryPanel()" class="history-toggle-btn">مشاهده تاریخچه</button>
<button onclick="window.print()" style="background-color: purple; color: white; cursor: pointer;" >چاپ</button>
<button onclick="deleteAllData()" class="delete-all-btn">حذف همه</button>
</div>
<div class="result" id="result">
<!-- نتایج محاسبات اینجا نمایش داده میشوند -->
</div>
</main>
<!-- Toast Container -->
<div id="toast-container"></div>
<!-- History Panel -->
<div id="historyPanel">
<h2>تاریخچه دورهمیها</h2>
<ul class="history-list" id="historyList">
<!-- History items will be inserted here -->
</ul>
<button onclick="hideHistoryPanel()" class="close-btn">بستن</button>
</div>
<!-- Custom Modal for Alert/Confirm -->
<div id="modalOverlay">
<div id="customModal">
<h3 id="modalTitle"></h3>
<p id="modalMessage"></p>
<div class="modal-buttons">
<!-- Buttons will be dynamically added here -->
</div>
</div>
</div>
<script>
let people = [];
const STORAGE_KEY_HISTORY = 'dongGatheringsHistory';
// آرایه سخنان حکیمانه فارسی
const persianSayings = [
"آب از آب تکان نمیخوره.",
"آش نخورده و دهن سوخته.",
"از کاه کوه ساختن.",
"از ماست که بر ماست.",
"باد آورده رو باد میبره.",
"با یه دست نمیشه دو تا هندونه برداشت.",
"با یه گل بهار نمیشه.",
"بار کج به منزل نمیرسه.",
"به در بگو دیوار بشنوه.",
"پز عالی، جیب خالی.",
"تا تنور داغه، نون رو بچسبون.",
"چوب لای چرخ گذاشتن.",
"خروس بیمحل.",
"خود کرده را تدبیر نیست.",
"دبه درآوردن.",
"دیوار حاشا بلنده.",
"رطب خورده، منع رطب کی کند؟",
"سنگ مفت، گنجشک مفت.",
"شتر دیدی ندیدی.",
"کاچی بهتر از هیچی.",
"بنی آدم اعضای یکدیگرند که در آفرینش ز یک گوهرند. (سعدی)",
"تو نیکی کن و در دجله انداز که ایزد در بیابانت دهد باز. (سعدی)",
"گر صبر کنی ز غوره حلوا سازی.",
"هر که بامش بیش، برفش بیشتر.",
"کمال همنشین در من اثر کرد وگرنه من همان خاکم که هستم. (سعدی)",
"آنچه نپاید دلبستگی را نشاید. (سعدی)",
"از محبت خارها گل میشود. (مولوی)",
"جوانی نوبهار زندگانیست.",
"قطره قطره جمع گردد وانگهی دریا شود.",
"دو صد گفته چون نیم کردار نیست.",
"دست بالای دست بسیار است.",
"کار نیکو کردن از پر کردن است.",
"آب از سرچشمه گل آلود است.",
"خواهی نشوی رسوا، همرنگ جماعت شو.",
"عاقبت جوینده یابنده بود.",
"هر سخن جایی و هر نکته مکانی دارد.",
"چو دانی و پرسی، سزای تو نیست. (فردوسی)",
"مشت نمونه خروار است.",
"نابرده رنج گنج میسر نمیشود مزد آن گرفت جان برادر که کار کرد. (سعدی)",
"هر که را جامه پارسا بینی پارسا دان و نیک مرد انگار. (سعدی)",
"بخت نیکان را گهی بد میشود.",
"هر چه بگندد نمکش میزنند وای به روزی که بگندد نمک.",
"چراغی که به خانه رواست، به مسجد حرام است.",
"در ناامیدی بسی امید است پایان شب سیه سپید است.",
"ماهی را هر وقت از آب بگیری تازه است.",
"سنگی که به مقصد نرسد، باری برمیدارد.",
"آب رفته به جوی باز نمیگردد.",
"از دل برود هر آنکه از دیده برفت.",
"گربه دستش به گوشت نمیرسید، میگفت پیف پیف بو میدهد.",
"نان را به نرخ روز خوردن.",
"گرگ زاده عاقبت گرگ شود گرچه با آدمی بزرگ شود. (سعدی)",
"آنقدر آب نیست که کوزه بشکند.",
"ادب از که آموختی؟ از بیادبان.",
"چاه کن همیشه ته چاه است.",
"شنونده باید عاقل باشد.",
"دیوار موش داره، موش گوش داره.",
"هر چه را خوار شماری خوار شود.",
"صبر تلخ است ولیکن میوه شیرین دهد.",
"با دوستان مروت، با دشمنان مدارا. (سعدی)",
"دوری و دوستی.",
"از این ستون به آن ستون فرج است.",
"هر که تنها قاضی رفت، راضی برگشت.",
"شتر در خواب بیند پنبه دانه.",
"هر گردی گردو نیست.",
"باد در قفس نگه داشتن.",
"از تو حرکت، از خدا برکت.",
"کار از محکمکاری عیب نمیکند.",
"صلاح کار کجا و من خراب کجا. (حافظ)",
"سرکه نقد، حلوای نسیه.",
"آب که یکجا ماند، میگندد.",
"تا نباشد چوب تر، فرمان نبرند گاو و خر.",
"هر کبوتر با کبوتر، باز با باز.",
"گنج در ویرانه پنهان است.",
"خواهی که بدانی چه کسی هستی، بنگر که با که همنشینی.",
"هر چیزی که در جستن آنی، آنی. (مولوی)",
"هر که بداندیشد، رنجور است.",
"دیگ به دیگ میگوید رویت سیاه.",
"قسم حضرت عباس، دم خروس باور کنیم؟",
"هر دم از این باغ بری میرسد تازهتر از تازهتری میرسد.",
"مرغ همسایه غاز است.",
"سر تا پا گوش بودن.",
"از کوزه همان برون تراود که در اوست. (باباطاهر)",
"گر به دولت برسی مست نگردی مردی.",
"صبر و ظفر هر دو دوستان قدیماند.",
"هر کسی را بهر کاری ساختند مهر او را در دلش انداختند. (مولوی)",
"عیب کسان منگر و احسان خویش. (حافظ)",
"آب دریا را نتوان خشک کرد با سنگ ریزه.",
"هر که را یک هنر باشد، او را بس.",
"به دعای گربه سیاه باران نمیبارد.",
"درخت هر چه پربارتر باشد، سر به زیرتر است.",
"این نیز بگذرد.",
"باد کاشتن و توفان درو کردن.",
"شکر نعمت، نعمتت افزون کند.",
"از گهواره تا گور دانش بجوی.",
"چشم تنگ دنیا دوست را یا قناعت پر کند یا خاک گور. (سعدی)",
"سنگ بزرگ علامت نزدن است.",
"عاقل کند کاری که باز آرد پشیمانی.",
"دست پنهان بیش از آشکار توانایی دارد.",
"هرچه پیش آید خوش آید.",
"گندم از گندم بروید، جو از جو.",
"دم خروسو باور کنیم یا قسم حضرت عباس رو؟"
];
function getRandomColor() {
const h = Math.floor(Math.random() * 360);
const s = Math.floor(Math.random() * 30) + 30;
const l = Math.floor(Math.random() * 20) + 70;
return `hsl(${h}, ${s}%, ${l}%)`;
}
// Global toast message handler
function showToast(message) {
let toastContainer = document.getElementById('toast-container');
if (!toastContainer) {
toastContainer = document.createElement('div');
toastContainer.id = 'toast-container';
document.body.appendChild(toastContainer);
}
const toast = document.createElement('div');
toast.className = 'toast-message';
toast.textContent = message;
toastContainer.appendChild(toast);
setTimeout(() => {
toast.classList.add('fade-out');
toast.addEventListener('transitionend', () => toast.remove());
}, 1000); // Display for 1 second, then fade out over 0.5 seconds
}
// Custom Modal Functions
let currentOnConfirm = null;
let currentOnCancel = null;
/**
* Displays a custom modal for alert or confirmation.
* @param {string} message - The message to display in the modal.
* @param {'alert'|'confirm'} type - The type of modal ('alert' for OK button, 'confirm' for Yes/No buttons).
* @param {function} [onConfirmCallback=null] - Callback function to execute when 'Yes' or 'OK' is clicked.
* @param {function} [onCancelCallback=null] - Callback function to execute when 'No' is clicked (only for 'confirm' type).
* @param {string} [title=""] - Optional title for the modal.
*/
function showCustomModal(message, type, onConfirmCallback = null, onCancelCallback = null, title = "") {
const modalOverlay = document.getElementById('modalOverlay');
const customModal = document.getElementById('customModal');
const modalTitle = document.getElementById('modalTitle');
const modalMessage = document.getElementById('modalMessage');
const modalButtons = customModal.querySelector('.modal-buttons');
// Clear previous content
modalButtons.innerHTML = '';
modalTitle.textContent = title || (type === 'confirm' ? 'تایید' : 'اطلاع');
modalMessage.textContent = message;
currentOnConfirm = onConfirmCallback;
currentOnCancel = onCancelCallback;
if (type === 'confirm') {
const confirmBtn = document.createElement('button');
confirmBtn.textContent = 'بله';
confirmBtn.className = 'confirm-btn';
confirmBtn.onclick = () => {
if (currentOnConfirm) currentOnConfirm();
hideCustomModal();
};
const cancelBtn = document.createElement('button');
cancelBtn.textContent = 'خیر';
cancelBtn.className = 'cancel-btn';
cancelBtn.onclick = () => {
if (currentOnCancel) currentOnCancel();
hideCustomModal();
};
modalButtons.appendChild(confirmBtn);
modalButtons.appendChild(cancelBtn);
} else if (type === 'alert') {
const okBtn = document.createElement('button');
okBtn.textContent = 'باشه';
okBtn.className = 'alert-ok-btn';
okBtn.onclick = () => {
if (currentOnConfirm) currentOnConfirm(); // Use onConfirm for OK callback
hideCustomModal();
};
modalButtons.appendChild(okBtn);
}
modalOverlay.style.display = 'flex';
customModal.style.display = 'block';
}
function hideCustomModal() {
document.getElementById('modalOverlay').style.display = 'none';
document.getElementById('customModal').style.display = 'none';
currentOnConfirm = null;
currentOnCancel = null;
}
// End Custom Modal Functions
function addPerson() {
const personNameInput = document.getElementById('personName');
let personName = personNameInput.value.trim();
if (personName === "") {
const nextPersonNumber = people.length + 1;
personName = `فرد ${nextPersonNumber}`;
showToast(`نام وارد نشد. شخص به عنوان "${personName}" اضافه شد.`);
} else {
showToast(`شخص "${personName}" اضافه شد.`);
}
const person = {
name: personName,
items: [],
backgroundColor: getRandomColor(),
};
people.push(person);
renderPeople();
personNameInput.value = "";
}
// Function to format number for display
// decimalPlaces: Number of decimal places to show.
// forResultDisplay: If true, formats with ' as thousands separator and / as decimal separator, and ensures LTR rendering.
function formatNumber(number, decimalPlaces = 0, forResultDisplay = false) {
const num = parseFloat(number);
if (isNaN(num)) return '0';
// Step 1: Round to specified decimal places and convert to string.
// This will produce a string with '.' as decimal separator and no thousands separators initially.
let numStr = num.toFixed(decimalPlaces); // e.g., "12345.678" or "12345"
// Step 2: Add thousands separators and handle decimal part based on 'forResultDisplay'
if (forResultDisplay) {
// Split into integer and fractional parts
const parts = numStr.split('.');
let integerPart = parts[0];
let fractionalPart = parts.length > 1 ? parts[1] : '';
// Add standard comma thousands separators first (temporarily)
integerPart = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
// Combine and then replace standard separators with custom ones
let finalFormattedStr = integerPart;
if (fractionalPart) {
finalFormattedStr += '.' + fractionalPart;
}
// Replace commas with single quotes and dots with slashes
// And importantly, prepend with LTR mark to ensure correct directionality in RTL context
return '\u200E' + finalFormattedStr.replace(/,/g, "'").replace(/\./g, "/");
} else {
// Original logic for general display (e.g., item prices, person totals)
const parts = numStr.split('.');
let integerPart = parts[0];
let fractionalPart = parts.length > 1 ? parts[1] : '';
// Add commas as thousands separators
integerPart = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
// Combine
if (fractionalPart) {
return `${integerPart}.${fractionalPart}`;
} else {
return integerPart;
}
}
}
// Function to format price input field dynamically
function formatPriceInput(input) {
let value = input.value.replace(/[^0-9]/g, ''); // Remove non-digits
if (value === "") {
input.value = "";
return;
}
// Use toLocaleString for input formatting (standard comma/dot)
input.value = Number(value).toLocaleString('en-US');
}
function addItem(personIndex) {
const person = people[personIndex];
const itemNameInput = document.getElementById(`itemName-${personIndex}`);
const itemPriceInput = document.getElementById(`itemPrice-${personIndex}`);
let itemName = itemNameInput.value.trim();
const itemPriceStr = itemPriceInput.value.replace(/,/g, '');
const itemPrice = parseFloat(itemPriceStr);
if (isNaN(itemPrice) || itemPrice < 0) {
showToast("لطفا قیمت معتبر (بیشتر از صفر) را وارد کنید.");
return;
}
if (itemName === "") {
const nextItemNumber = person.items.length + 1;
itemName = `قلم ${nextItemNumber}`;
showToast(`نام جنس وارد نشد. جنس به عنوان "${itemName}" اضافه شد.`);
} else {
showToast(`جنس "${itemName}" برای ${person.name} اضافه شد.`);
}
person.items.push({
name: itemName,
price: itemPrice,
quantity: 1, // Add initial quantity
unitPrice: itemPrice // Store the unit price
});
renderPeople();
itemNameInput.value = "";
itemPriceInput.value = "";
}
function updateItemQuantity(personIndex, itemIndex, change) {
const item = people[personIndex].items[itemIndex];
const newQuantity = item.quantity + change;
if (newQuantity < 1) {
showCustomModal(
"آیا میخواهید این جنس را حذف کنید؟",
"confirm",
() => removeItem(personIndex, itemIndex),
null,
"حذف جنس"
);
return;
}