File size: 67,980 Bytes
999b7fa |
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 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 |
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sistema de Pendientes de Pago</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/localforage/1.10.0/localforage.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
body {
font-family: 'Inter', sans-serif;
background-color: #f8fafc;
color: #1e293b;
}
.sidebar {
transition: all 0.3s ease;
}
.card {
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
transition: all 0.3s ease;
}
.card:hover {
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
transform: translateY(-2px);
}
.tab-content {
animation: fadeIn 0.3s ease;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.table-container {
max-height: 500px;
scrollbar-width: thin;
scrollbar-color: #cbd5e1 #f1f5f9;
}
.table-container::-webkit-scrollbar {
width: 8px;
height: 8px;
}
.table-container::-webkit-scrollbar-track {
background: #f1f5f9;
}
.table-container::-webkit-scrollbar-thumb {
background-color: #cbd5e1;
border-radius: 4px;
}
.badge {
font-size: 0.75rem;
padding: 0.25rem 0.5rem;
}
.action-btn {
transition: all 0.2s ease;
}
.action-btn:hover {
transform: scale(1.05);
}
.floating-btn {
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
transition: all 0.3s ease;
}
.floating-btn:hover {
box-shadow: 0 6px 8px rgba(0,0,0,0.15);
transform: translateY(-2px);
}
.no-records {
background-image: linear-gradient(to right, #f8fafc, #f1f5f9, #f8fafc);
background-size: 200% auto;
animation: shimmer 1.5s infinite linear;
}
@keyframes shimmer {
0% { background-position: -200% 0; }
100% { background-position: 200% 0; }
}
</style>
</head>
<body class="min-h-screen bg-gray-50">
<!-- Contenedor principal -->
<div class="flex h-screen bg-gray-50" id="app">
<!-- Sidebar -->
<div class="sidebar bg-white w-64 border-r border-gray-200 flex flex-col">
<div class="p-4 border-b border-gray-200 flex items-center">
<i class="fas fa-wallet text-2xl text-blue-600 mr-3"></i>
<h1 class="text-xl font-bold text-gray-800">Pendientes de Pago</h1>
</div>
<div class="flex-1 overflow-y-auto">
<nav class="p-4 space-y-1">
<a href="#" class="tab-link flex items-center px-4 py-2 text-sm font-medium rounded-lg bg-blue-50 text-blue-700" data-tab="dashboard">
<i class="fas fa-tachometer-alt mr-3 text-blue-600"></i> Dashboard
</a>
<a href="#" class="tab-link flex items-center px-4 py-2 text-sm font-medium rounded-lg text-gray-700 hover:bg-gray-100" data-tab="pendientes">
<i class="fas fa-list-ul mr-3 text-gray-500"></i> Pendientes
</a>
<a href="#" class="tab-link flex items-center px-4 py-2 text-sm font-medium rounded-lg text-gray-700 hover:bg-gray-100" data-tab="historial">
<i class="fas fa-history mr-3 text-gray-500"></i> Historial
</a>
<a href="#" class="tab-link flex items-center px-4 py-2 text-sm font-medium rounded-lg text-gray-700 hover:bg-gray-100" data-tab="reportes">
<i class="fas fa-chart-bar mr-3 text-gray-500"></i> Reportes
</a>
<a href="#" class="tab-link flex items-center px-4 py-2 text-sm font-medium rounded-lg text-gray-700 hover:bg-gray-100" data-tab="configuracion">
<i class="fas fa-cog mr-3 text-gray-500"></i> Configuraci贸n
</a>
</nav>
</div>
<div class="p-4 border-t border-gray-200">
<button onclick="logout()" class="w-full flex items-center justify-center px-4 py-2 text-sm font-medium text-red-600 hover:bg-red-50 rounded-lg">
<i class="fas fa-sign-out-alt mr-2"></i> Cerrar Sesi贸n
</button>
</div>
</div>
<!-- Contenido principal -->
<div class="flex-1 overflow-y-auto">
<div class="p-6">
<!-- Header -->
<div class="flex justify-between items-center mb-6">
<h2 class="text-2xl font-bold text-gray-800" id="current-tab-title">Dashboard</h2>
<div class="flex items-center space-x-2">
<span id="current-date" class="text-sm text-gray-500"></span>
<div class="relative">
<button id="notifications-btn" class="p-2 rounded-full hover:bg-gray-100">
<i class="fas fa-bell text-gray-500"></i>
<span class="absolute top-0 right-0 h-2 w-2 rounded-full bg-red-500"></span>
</button>
</div>
</div>
</div>
<!-- Contenido de las pesta帽as -->
<div class="tab-content" id="dashboard-content">
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-6">
<div class="card bg-white rounded-xl p-6">
<div class="flex items-center justify-between">
<div>
<p class="text-sm font-medium text-gray-500">Total Pendiente</p>
<h3 class="text-2xl font-bold text-gray-800 mt-1" id="total-pendiente">$0.00</h3>
</div>
<div class="p-3 rounded-full bg-blue-100 text-blue-600">
<i class="fas fa-clock text-xl"></i>
</div>
</div>
<div class="mt-4 pt-4 border-t border-gray-100">
<p class="text-xs text-gray-500">脷ltima actualizaci贸n: <span id="last-update">hoy</span></p>
</div>
</div>
<div class="card bg-white rounded-xl p-6">
<div class="flex items-center justify-between">
<div>
<p class="text-sm font-medium text-gray-500">Total Pagado</p>
<h3 class="text-2xl font-bold text-gray-800 mt-1" id="total-pagado">$0.00</h3>
</div>
<div class="p-3 rounded-full bg-green-100 text-green-600">
<i class="fas fa-check-circle text-xl"></i>
</div>
</div>
<div class="mt-4 pt-4 border-t border-gray-100">
<p class="text-xs text-gray-500">Este mes: <span id="month-pagado">$0.00</span></p>
</div>
</div>
<div class="card bg-white rounded-xl p-6">
<div class="flex items-center justify-between">
<div>
<p class="text-sm font-medium text-gray-500">Registros Pendientes</p>
<h3 class="text-2xl font-bold text-gray-800 mt-1" id="count-pendientes">0</h3>
</div>
<div class="p-3 rounded-full bg-yellow-100 text-yellow-600">
<i class="fas fa-file-invoice text-xl"></i>
</div>
</div>
<div class="mt-4 pt-4 border-t border-gray-100">
<p class="text-xs text-gray-500">Vencidos: <span id="count-vencidos">0</span></p>
</div>
</div>
<div class="card bg-white rounded-xl p-6">
<div class="flex items-center justify-between">
<div>
<p class="text-sm font-medium text-gray-500">Registros Historial</p>
<h3 class="text-2xl font-bold text-gray-800 mt-1" id="count-historial">0</h3>
</div>
<div class="p-3 rounded-full bg-purple-100 text-purple-600">
<i class="fas fa-archive text-xl"></i>
</div>
</div>
<div class="mt-4 pt-4 border-t border-gray-100">
<p class="text-xs text-gray-500">Este mes: <span id="month-historial">0</span></p>
</div>
</div>
</div>
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-6">
<div class="card bg-white rounded-xl p-6">
<div class="flex items-center justify-between mb-4">
<h3 class="text-lg font-semibold text-gray-800">Evoluci贸n de Pagos</h3>
<div class="flex space-x-2">
<button class="text-xs px-2 py-1 bg-gray-100 rounded hover:bg-gray-200" data-period="week">Semana</button>
<button class="text-xs px-2 py-1 bg-blue-100 rounded hover:bg-blue-200" data-period="month">Mes</button>
<button class="text-xs px-2 py-1 bg-gray-100 rounded hover:bg-gray-200" data-period="year">A帽o</button>
</div>
</div>
<div class="h-64">
<canvas id="payments-chart"></canvas>
</div>
</div>
<div class="card bg-white rounded-xl p-6">
<div class="flex items-center justify-between mb-4">
<h3 class="text-lg font-semibold text-gray-800">Distribuci贸n por Categor铆a</h3>
<div class="flex space-x-2">
<button class="text-xs px-2 py-1 bg-gray-100 rounded hover:bg-gray-200" data-type="pendientes">Pendientes</button>
<button class="text-xs px-2 py-1 bg-blue-100 rounded hover:bg-blue-200" data-type="historial">Historial</button>
</div>
</div>
<div class="h-64">
<canvas id="categories-chart"></canvas>
</div>
</div>
</div>
<div class="card bg-white rounded-xl p-6">
<div class="flex items-center justify-between mb-4">
<h3 class="text-lg font-semibold text-gray-800">Pr贸ximos Vencimientos</h3>
<a href="#" class="text-sm text-blue-600 hover:text-blue-800" onclick="activarTab('pendientes')">Ver todos</a>
</div>
<div class="overflow-x-auto">
<table class="min-w-full divide-y divide-gray-200">
<thead class="bg-gray-50">
<tr>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Fecha</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Nombre</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Descripci贸n</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Total</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Estado</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200" id="upcoming-payments">
<tr>
<td colspan="5" class="px-6 py-4 text-center text-sm text-gray-500">Cargando...</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<!-- Pesta帽a Pendientes -->
<div class="tab-content hidden" id="pendientes-content">
<div class="grid grid-cols-1 lg:grid-cols-3 gap-6 mb-6">
<div class="lg:col-span-2">
<div class="card bg-white rounded-xl p-6">
<h3 class="text-lg font-semibold text-gray-800 mb-4">Nuevo Registro</h3>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label for="fecha" class="block text-sm font-medium text-gray-700 mb-1">Fecha</label>
<input type="date" id="fecha" required
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition">
</div>
<div>
<label for="nombre" class="block text-sm font-medium text-gray-700 mb-1">Nombre</label>
<input type="text" id="nombre" placeholder="Ingrese nombre" required
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition">
</div>
<div class="md:col-span-2">
<label for="descripcion" class="block text-sm font-medium text-gray-700 mb-1">Descripci贸n</label>
<textarea id="descripcion" placeholder="Ingrese descripci贸n" required rows="3"
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition"></textarea>
</div>
<div>
<label for="total" class="block text-sm font-medium text-gray-700 mb-1">Total</label>
<input type="number" id="total" placeholder="0.00" step="0.01" required
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition">
</div>
<div class="flex items-end">
<button id="btn-agregar"
class="bg-blue-600 text-white py-2 px-6 rounded-lg hover:bg-blue-700 transition flex items-center">
<i class="fas fa-plus mr-2"></i> Agregar
</button>
<button id="btn-cancelar-edicion"
class="ml-2 bg-gray-200 text-gray-700 py-2 px-4 rounded-lg hover:bg-gray-300 transition hidden">
Cancelar
</button>
</div>
</div>
</div>
</div>
<div class="card bg-white rounded-xl p-6">
<h3 class="text-lg font-semibold text-gray-800 mb-4">Resumen</h3>
<div class="space-y-4">
<div>
<p class="text-sm font-medium text-gray-500">Total Pendiente</p>
<h3 class="text-2xl font-bold text-gray-800" id="pendientes-summary-total">$0.00</h3>
</div>
<div>
<p class="text-sm font-medium text-gray-500">Registros</p>
<h3 class="text-xl font-bold text-gray-800" id="pendientes-summary-count">0</h3>
</div>
<div class="pt-4 border-t border-gray-100">
<label for="buscar" class="block text-sm font-medium text-gray-700 mb-2">Buscar registros</label>
<div class="relative">
<input type="search" id="buscar" placeholder="Buscar por nombre, fecha o descripci贸n"
class="w-full pl-10 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition">
<div class="absolute left-3 top-2.5 text-gray-400">
<i class="fas fa-search"></i>
</div>
</div>
</div>
<div class="pt-4 border-t border-gray-100">
<button id="exportar-json"
class="w-full bg-green-600 text-white py-2 px-4 rounded-lg hover:bg-green-700 transition flex items-center justify-center mb-2">
<i class="fas fa-file-export mr-2"></i> Exportar JSON
</button>
<button id="exportar-excel"
class="w-full bg-green-600 text-white py-2 px-4 rounded-lg hover:bg-green-700 transition flex items-center justify-center mb-2">
<i class="fas fa-file-excel mr-2"></i> Exportar CSV
</button>
<button id="importar-json"
class="w-full bg-blue-600 text-white py-2 px-4 rounded-lg hover:bg-blue-700 transition flex items-center justify-center mb-2">
<i class="fas fa-file-import mr-2"></i> Importar JSON
</button>
<button id="importar-excel"
class="w-full bg-blue-600 text-white py-2 px-4 rounded-lg hover:bg-blue-700 transition flex items-center justify-center">
<i class="fas fa-file-csv mr-2"></i> Importar CSV
</button>
</div>
</div>
</div>
</div>
<div class="card bg-white rounded-xl p-6 mb-6">
<div class="flex justify-between items-center mb-4">
<h3 class="text-lg font-semibold text-gray-800">Registros Pendientes</h3>
<div class="flex space-x-2">
<button id="descargar-busqueda"
class="bg-gray-100 text-gray-700 py-1.5 px-3 rounded-lg hover:bg-gray-200 transition flex items-center text-sm">
<i class="fas fa-image mr-1"></i> Imagen
</button>
<button id="limpiar-datos"
class="bg-red-100 text-red-700 py-1.5 px-3 rounded-lg hover:bg-red-200 transition flex items-center text-sm">
<i class="fas fa-trash mr-1"></i> Limpiar
</button>
</div>
</div>
<div class="table-container overflow-auto rounded-lg border border-gray-200">
<table class="min-w-full divide-y divide-gray-200" id="tabla-pendientes">
<thead class="bg-gray-50">
<tr>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Fecha</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Nombre</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Descripci贸n</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Total</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Acciones</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200" id="pendientes-body">
<tr>
<td colspan="5" class="px-6 py-4 text-center text-sm text-gray-500">Cargando datos...</td>
</tr>
</tbody>
<tfoot class="bg-gray-50">
<tr>
<td colspan="3" class="px-6 py-3 text-right text-sm font-medium text-gray-500">TOTAL PENDIENTE:</td>
<td class="px-6 py-3 text-left text-sm font-medium text-gray-900" id="suma-total">$0.00</td>
<td></td>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
<!-- Pesta帽a Historial -->
<div class="tab-content hidden" id="historial-content">
<div class="grid grid-cols-1 lg:grid-cols-3 gap-6 mb-6">
<div class="lg:col-span-2">
<div class="card bg-white rounded-xl p-6">
<h3 class="text-lg font-semibold text-gray-800 mb-4">Filtros</h3>
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div>
<label for="fecha-inicio" class="block text-sm font-medium text-gray-700 mb-1">Fecha Inicio</label>
<input type="date" id="fecha-inicio"
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition">
</div>
<div>
<label for="fecha-fin" class="block text-sm font-medium text-gray-700 mb-1">Fecha Fin</label>
<input type="date" id="fecha-fin"
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition">
</div>
<div>
<label for="buscar-historial" class="block text-sm font-medium text-gray-700 mb-1">Buscar</label>
<div class="relative">
<input type="search" id="buscar-historial" placeholder="Buscar en historial"
class="w-full pl-10 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition">
<div class="absolute left-3 top-2.5 text-gray-400">
<i class="fas fa-search"></i>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="card bg-white rounded-xl p-6">
<h3 class="text-lg font-semibold text-gray-800 mb-4">Resumen</h3>
<div class="space-y-4">
<div>
<p class="text-sm font-medium text-gray-500">Total Pagado</p>
<h3 class="text-2xl font-bold text-gray-800" id="historial-summary-total">$0.00</h3>
</div>
<div>
<p class="text-sm font-medium text-gray-500">Registros</p>
<h3 class="text-xl font-bold text-gray-800" id="historial-summary-count">0</h3>
</div>
<div class="pt-4 border-t border-gray-100">
<button id="exportar-historial-json"
class="w-full bg-green-600 text-white py-2 px-4 rounded-lg hover:bg-green-700 transition flex items-center justify-center mb-2">
<i class="fas fa-file-export mr-2"></i> Exportar JSON
</button>
<button id="exportar-historial-excel"
class="w-full bg-green-600 text-white py-2 px-4 rounded-lg hover:bg-green-700 transition flex items-center justify-center">
<i class="fas fa-file-excel mr-2"></i> Exportar CSV
</button>
</div>
</div>
</div>
</div>
<div class="card bg-white rounded-xl p-6 mb-6">
<div class="flex justify-between items-center mb-4">
<h3 class="text-lg font-semibold text-gray-800">Historial de Pagos</h3>
<div class="flex items-center space-x-2">
<span class="text-sm text-gray-500" id="historial-filter-info">Mostrando todos los registros</span>
</div>
</div>
<div class="table-container overflow-auto rounded-lg border border-gray-200">
<table class="min-w-full divide-y divide-gray-200" id="tabla-historial">
<thead class="bg-gray-50">
<tr>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Fecha Pago</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Fecha Registro</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Nombre</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Descripci贸n</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Total</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200" id="historial-body">
<tr>
<td colspan="5" class="px-6 py-4 text-center text-sm text-gray-500">Cargando historial...</td>
</tr>
</tbody>
<tfoot class="bg-gray-50">
<tr>
<td colspan="4" class="px-6 py-3 text-right text-sm font-medium text-gray-500">TOTAL PAGADO:</td>
<td class="px-6 py-3 text-left text-sm font-medium text-gray-900" id="suma-total-historial">$0.00</td>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
<!-- Pesta帽a Reportes -->
<div class="tab-content hidden" id="reportes-content">
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-6">
<div class="card bg-white rounded-xl p-6">
<div class="flex items-center justify-between mb-4">
<h3 class="text-lg font-semibold text-gray-800">Pagos por Mes</h3>
<select id="report-year" class="text-sm border border-gray-300 rounded px-3 py-1 focus:ring-blue-500 focus:border-blue-500">
<option value="2023">2023</option>
<option value="2024" selected>2024</option>
</select>
</div>
<div class="h-80">
<canvas id="monthly-payments-chart"></canvas>
</div>
</div>
<div class="card bg-white rounded-xl p-6">
<div class="flex items-center justify-between mb-4">
<h3 class="text-lg font-semibold text-gray-800">Top Clientes</h3>
<select id="report-type" class="text-sm border border-gray-300 rounded px 3 py-1 focus:ring-blue-500 focus:border-blue-500">
<option value="pendientes">Pendientes</option>
<option value="historial" selected>Historial</option>
</select>
</div>
<div class="h-80">
<canvas id="top-clients-chart"></canvas>
</div>
</div>
</div>
<div class="card bg-white rounded-xl p-6 mb-6">
<div class="flex items-center justify-between mb-4">
<h3 class="text-lg font-semibold text-gray-800">Resumen Anual</h3>
<div class="flex space-x-2">
<button id="export-report-pdf" class="text-sm bg-red-600 text-white px-4 py-1.5 rounded hover:bg-red-700 flex items-center">
<i class="fas fa-file-pdf mr-2"></i> PDF
</button>
<button id="export-report-excel" class="text-sm bg-green-600 text-white px-4 py-1.5 rounded hover:bg-green-700 flex items-center">
<i class="fas fa-file-excel mr-2"></i> Excel
</button>
</div>
</div>
<div class="overflow-x-auto">
<table class="min-w-full divide-y divide-gray-200">
<thead class="bg-gray-50">
<tr>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">A帽o</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Ene</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Feb</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Mar</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Abr</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">May</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Jun</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Jul</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Ago</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Sep</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Oct</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Nov</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Dic</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Total</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200" id="annual-summary">
<tr>
<td colspan="14" class="px-6 py-4 text-center text-sm text-gray-500">Cargando datos...</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<!-- Pesta帽a Configuraci贸n -->
<div class="tab-content hidden" id="configuracion-content">
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-6">
<div class="card bg-white rounded-xl p-6">
<h3 class="text-lg font-semibold text-gray-800 mb-4">Respaldo Autom谩tico</h3>
<p class="text-sm text-gray-600 mb-4">Selecciona una carpeta donde se guardar谩n autom谩ticamente los archivos de respaldo cada vez que agregues o actualices un registro.</p>
<div class="mb-4">
<label class="block text-sm font-medium text-gray-700 mb-1">Carpeta de Respaldo</label>
<div class="flex">
<input type="text" id="backup-folder" placeholder="Ninguna carpeta seleccionada" readonly
class="flex-1 px-4 py-2 border border-gray-300 rounded-l-lg bg-gray-50">
<button onclick="seleccionarCarpeta()"
class="bg-blue-600 text-white px-4 py-2 rounded-r-lg hover:bg-blue-700 transition">
<i class="fas fa-folder-open mr-1"></i> Seleccionar
</button>
</div>
</div>
<div class="p-4 bg-blue-50 rounded-lg mb-4" id="backup-status">
<p class="text-sm text-blue-800">Selecciona una carpeta para activar backups.</p>
</div>
</div>
<div class="card bg-white rounded-xl p-6">
<h3 class="text-lg font-semibold text-gray-800 mb-4">Respaldo Programado</h3>
<p class="text-sm text-gray-600 mb-4">Configura respaldos peri贸dicos que incluyan pendientes e historial con marca de tiempo.</p>
<div class="mb-4">
<label for="backup-interval" class="block text-sm font-medium text-gray-700 mb-1">Intervalo</label>
<select id="backup-interval" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-blue-500 focus:border-blue-500">
<option value="0">Desactivado</option>
<option value="5">Cada 5 minutos</option>
<option value="15">Cada 15 minutos</option>
<option value="30">Cada 30 minutos</option>
<option value="60">Cada 1 hora</option>
<option value="120">Cada 2 horas</option>
<option value="1440">Diario</option>
</select>
</div>
<div class="mb 4">
<label for="backup-format" class="block text-sm font-medium text-gray-700 mb-1">Formato</label>
<select id="backup-format" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-blue-500 focus:border-blue-500">
<option value="json">JSON</option>
<option value="csv">CSV</option>
</select>
</div>
<button onclick="guardarConfiguracionProgramada()"
class="w-full bg-blue-600 text-white py-2 px-4 rounded-lg hover:bg-blue-700 transition flex items-center justify-center">
<i class="fas fa-save mr-2"></i> Guardar Configuraci贸n
</button>
</div>
</div>
<div class="card bg-white rounded-xl p-6">
<h3 class="text-lg font-semibold text-gray-800 mb-4">Integrar C贸digo</h3>
<p class="text-sm text-gray-600 mb-4">Pega aqu铆 el c贸digo JavaScript que deseas ejecutar en el sistema.</p>
<textarea id="code-area" rows="8" placeholder="Pega aqu铆 el c贸digo JavaScript que deseas ejecutar..."
class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition mb-4 font-mono text-sm"></textarea>
<div class="flex justify-end">
<button onclick="integrarCodigo()"
class="bg-purple-600 text-white py-2 px-6 rounded-lg hover:bg-purple-700 transition flex items-center">
<i class="fas fa-code mr-2"></i> Ejecutar C贸digo
</button>
</div>
<div class="mt-4 p-3 bg-red-50 rounded-lg">
<p class="text-sm text-red-700"><i class="fas fa-exclamation-triangle mr-2"></i> ADVERTENCIA: Ejecutar c贸digo desconocido puede ser peligroso. Solo usa c贸digo de fuentes confiables.</p>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
// --- Strict mode ---
"use strict";
/*******************************
* Variables Globales
*******************************/
let modoEdicion = false;
let idEdicion = null;
let backupFolderHandle = null;
let paymentsChart = null;
let categoriesChart = null;
let monthlyPaymentsChart = null;
let topClientsChart = null;
let btnAgregar = document.getElementById('btn-agregar');
let btnCancelarEdicion = document.getElementById('btn-cancelar-edicion');
/*******************************
* App Initialization
*******************************/
async function initializeApp() {
document.getElementById('fecha').valueAsDate = new Date();
configurarEventosGenerales();
// Set default tab to dashboard
activarTab('dashboard');
// Load initial data
try {
const pendientesStored = await memoryStorage.getPendientes();
if (!pendientesStored || pendientesStored.length === 0) {
await cargarDatosEjemplo();
}
await cargarDatos();
await cargarHistorial();
await updateDashboard();
await loadReports();
} catch (error) {
console.error("Error during initial data loading:", error);
alert("Error al cargar los datos iniciales: " + error.message);
}
cargarConfiguracionProgramada();
}
/*******************************
* Almacenamiento (localForage)
*******************************/
const memoryStorage = {
async getPendientes() {
const pendientes = await localforage.getItem('pendientes');
return pendientes || [];
},
async setPendientes(newData) {
if (!Array.isArray(newData)) {
return Promise.reject(new Error("Invalid data format for pendientes"));
}
return localforage.setItem('pendientes', newData);
},
async getHistorial() {
const historial = await localforage.getItem('historial');
return historial || [];
},
async setHistorial(newData) {
if (!Array.isArray(newData)) {
return Promise.reject(new Error("Invalid data format for historial"));
}
return localforage.setItem('historial', newData);
},
async addToHistorial(pendiente) {
if (!pendiente || typeof pendiente !== 'object') {
return Promise.reject(new Error("Invalid item format for historial"));
}
let historial = await this.getHistorial();
const pagoRegistro = {
...pendiente,
fechaPago: new Date().toISOString().split('T')[0]
};
historial.push(pagoRegistro);
return this.setHistorial(historial);
},
async clearData() {
await localforage.removeItem('pendientes');
await localforage.removeItem('historial');
}
};
/*******************************
* Configuraci贸n Eventos Generales
*******************************/
function configurarEventosGenerales() {
// Tabs
document.querySelectorAll('.tab-link').forEach(tab => {
tab.addEventListener('click', (e) => {
e.preventDefault();
const tab = tab.dataset.tab;
activarTab(tab);
});
});
// Bot贸n Agregar/Actualizar
btnAgregar?.addEventListener('click', guardarRegistro);
// Bot贸n Cancelar Edici贸n
btnCancelarEdicion?.addEventListener('click', resetearFormulario);
// Filtros de b煤squeda
document.getElementById('buscar')?.addEventListener('input', cargarDatos);
document.getElementById('buscar-historial')?.addEventListener('input', cargarHistorial);
document.getElementById('fecha-inicio')?.addEventListener('change', cargarHistorial);
document.getElementById('fecha-fin')?.addEventListener('change', cargarHistorial);
// Descargar imagen
document.getElementById('descargar-busqueda')?.addEventListener('click', descargarResultadosComoImagen);
// Exportar/Importar
document.getElementById('exportar-json')?.addEventListener('click', exportarPendientesJSON);
document.getElementById('exportar-excel')?.addEventListener('click', exportarPendientesCSV);
document.getElementById('exportar-historial-json')?.addEventListener('click', exportarHistorialJSON);
document.getElementById('exportar-historial-excel')?.addEventListener('click', exportarHistorialCSV);
document.getElementById('importar-json')?.addEventListener('click', importarPendientesJSON);
document.getElementById('importar-excel')?.addEventListener('click', importarPendientesCSV);
// Limpiar datos
document.getElementById('limpiar-datos')?.addEventListener('click', async () => {
if (confirm('驴Est谩 seguro de eliminar todos los datos (pendientes e historial)? Esta acci贸n no se puede deshacer.')) {
try {
await memoryStorage.clearData();
await cargarDatos();
await cargarHistorial();
await updateDashboard();
await loadReports();
alert('Todos los datos han sido eliminados.');
await eliminarBackupsInternos();
resetearFormulario();
} catch (error) {
alert("Error al limpiar los datos: " + error.message);
}
}
});
// Reportes
document.getElementById('report-year')?.addEventListener('change', loadReports);
document.getElementById('report-type')?.addEventListener('change', loadReports);
}
function activarTab(tab) {
// Update active tab in sidebar
document.querySelectorAll('.tab-link').forEach(t => {
t.classList.remove('bg-blue-50', 'text-blue-700');
t.classList.add('text-gray-700', 'hover:bg-gray-100');
// Update icon colors
const icon = t.querySelector('i');
if (icon) {
icon.classList.remove('text-blue-600');
icon.classList.add('text-gray-500');
}
});
// Set active tab
const activeTab = document.querySelector(`.tab-link[data-tab="${tab}"]`);
if (activeTab) {
activeTab.classList.add('bg-blue-50', 'text-blue-700');
activeTab.classList.remove('text-gray-700', 'hover:bg-gray-100');
// Update icon color
const icon = activeTab.querySelector('i');
if (icon) {
icon.classList.add('text-blue-600');
icon.classList.remove('text-gray-500');
}
}
// Hide all tab contents
document.querySelectorAll('.tab-content').forEach(t => t.classList.add('hidden'));
// Show selected tab content
const contentElement = document.getElementById(`${tab}-content`);
if (contentElement) {
contentElement.classList.remove('hidden');
}
// Update title
const tabTitles = {
'dashboard': 'Dashboard',
'pendientes': 'Pendientes',
'historial': 'Historial',
'reportes': 'Reportes',
'configuracion': 'Configuraci贸n'
};
document.getElementById('current-tab-title').textContent = tabTitles[tab] || tab;
// Load specific tab data if needed
if (tab === 'dashboard') {
updateDashboard();
} else if (tab === 'reportes') {
loadReports();
}
}
/*******************************
* Funciones principales
*******************************/
async function guardarRegistro() {
const fecha = document.getElementById('fecha').value;
const nombre = document.getElementById('nombre').value.trim();
const descripcion = document.getElementById('descripcion').value.trim();
const totalInput = document.getElementById('total').value;
if (!fecha || !nombre || !descripcion || totalInput === '') {
alert('Por favor, complete todos los campos.');
return;
}
const total = parseFloat(totalInput);
if (isNaN(total)) {
alert('El valor total debe ser un n煤mero v谩lido.');
return;
}
try {
let pendientes = await memoryStorage.getPendientes();
let mensajeExito = '';
if (modoEdicion && idEdicion !== null) {
const index = pendientes.findIndex(item => item.id == idEdicion);
if (index !== -1) {
pendientes[index] = { id: parseInt(idEdicion), fecha, nombre, descripcion, total };
mensajeExito = 'Registro actualizado correctamente.';
} else {
alert("Error al actualizar: No se encontr贸 el registro.");
resetearFormulario();
return;
}
} else {
const nuevoId = Date.now();
const nuevoRegistro = { id: nuevoId, fecha, nombre, descripcion, total };
pendientes.push(nuevoRegistro);
mensajeExito = 'Registro agregado correctamente.';
}
await memoryStorage.setPendientes(pendientes);
await generarBackupInterno().catch(err => console.error("Error during internal backup:", err));
resetearFormulario();
await cargarDatos();
await updateDashboard();
await loadReports();
} catch (error) {
alert(`Error al guardar el registro: ${error.message}`);
}
}
async function cargarDatos() {
const tbody = document.getElementById('pendientes-body');
const busqueda = document.getElementById('buscar').value.toLowerCase();
tbody.innerHTML = '<tr><td colspan="5" class="px-6 py-4 text-center text-sm text-gray-500">Cargando...</td></tr>';
try {
let pendientes = await memoryStorage.getPendientes();
// Filtrar
let filtrados = pendientes.filter(item =>
!busqueda ||
(item.nombre && item.nombre.toLowerCase().includes(busqueda)) ||
(item.descripcion && item.descripcion.toLowerCase().includes(busqueda)) ||
(item.fecha && item.fecha.includes(busqueda))
);
// Ordenar por fecha (ascendente)
filtrados.sort((a, b) => {
try { return new Date(a.fecha) - new Date(b.fecha); }
catch(e) { return 0; }
});
tbody.innerHTML = "";
let sumaTotal = 0;
if (filtrados.length === 0) {
const msg = busqueda ? "No se encontraron registros con ese criterio." : "No hay registros pendientes.";
tbody.innerHTML = `<tr><td colspan="5" class="px-6 py-4 text-center text-sm text-gray-500">${msg}</td></tr>`;
} else {
filtrados.forEach(item => {
let fechaFormateada = item.fecha;
try {
const dateParts = item.fecha.split('-');
if (dateParts.length === 3) {
const year = parseInt(dateParts[0]);
const month = parseInt(dateParts[1]) - 1;
const day = parseInt(dateParts[2]);
fechaFormateada = new Date(year, month, day).toLocaleDateString();
}
} catch (e) { console.warn(`Fecha inv谩lida: ${item.fecha}`); }
const row = tbody.insertRow();
row.className = 'hover:bg-gray-50';
// Fecha
const cellFecha = row.insertCell();
cellFecha.className = 'px-6 py-4 whitespace-nowrap text-sm text-gray-900';
cellFecha.textContent = fechaFormateada;
// Nombre
const cellNombre = row.insertCell();
cellNombre.className = 'px-6 py-4 whitespace-nowrap text-sm text-gray-900';
cellNombre.textContent = item.nombre;
// Descripci贸n
const cellDesc = row.insertCell();
cellDesc.className = 'px-6 py-4 text-sm text-gray-900';
cellDesc.textContent = item.descripcion;
// Total
const cellTotal = row.insertCell();
cellTotal.className = 'px-6 py-4 whitespace-nowrap text-sm text-right font-medium';
cellTotal.textContent = `$${item.total?.toFixed(2) ?? '0.00'}`;
cellTotal.style.color = item.total < 0 ? '#dc2626' : '#1f2937';
// Acciones
const cellAcciones = row.insertCell();
cellAcciones.className = 'px-6 py-4 whitespace-nowrap text-right text-sm font-medium';
const btnContainer = document.createElement('div');
btnContainer.className = 'flex space-x-2 justify-end';
// Bot贸n Editar
const btnEditar = document.createElement('button');
btnEditar.className = 'action-btn text-blue-600 hover:text-blue-900';
btnEditar.title = 'Editar este registro';
btnEditar.innerHTML = '<i class="fas fa-edit"></i>';
btnEditar.addEventListener('click', () => editarRegistro(item.id));
// Bot贸n Eliminar
const btnEliminar = document.createElement('button');
btnEliminar.className = 'action-btn text-red-600 hover:text-red-900';
btnEliminar.title = 'Eliminar este registro';
btnEliminar.innerHTML = '<i class="fas fa-trash"></i>';
btnEliminar.addEventListener('click', () => eliminarRegistro(item.id));
// Bot贸n Pagado
const btnPagado = document.createElement('button');
btnPagado.className = 'action-btn text-green-600 hover:text-green-900';
btnPagado.title = 'Marcar como pagado';
btnPagado.innerHTML = '<i class="fas fa-check-circle"></i>';
btnPagado.addEventListener('click', () => marcarComoPagado(item.id));
btnContainer.appendChild(btnEditar);
btnContainer.appendChild(btnEliminar);
btnContainer.appendChild(btnPagado);
cellAcciones.appendChild(btnContainer);
// Sum total
if (typeof item.total === 'number' && !isNaN(item.total)) {
sumaTotal += item.total;
}
});
}
document.getElementById('suma-total').textContent = `$${sumaTotal.toFixed(2)}`;
document.getElementById('pendientes-summary-total').textContent = `$${sumaTotal.toFixed(2)}`;
document.getElementById('pendientes-summary-count').textContent = filtrados.length;
} catch (error) {
tbody.innerHTML = `<tr><td colspan="5" class="px-6 py-4 text-center text-sm text-red-500">Error al cargar datos: ${error.message}</td></tr>`;
}
}
async function cargarHistorial() {
const tbody = document.getElementById('historial-body');
const busqueda = document.getElementById('buscar-historial').value.toLowerCase();
const fechaInicio = document.getElementById('fecha-inicio')?.value;
const fechaFin = document.getElementById('fecha-fin')?.value;
tbody.innerHTML = '<tr><td colspan="5" class="px-6 py-4 text-center text-sm text-gray-500">Cargando...</td></tr>';
try {
let historial = await memoryStorage.getHistorial();
// Filtrar
historial = historial.filter(item => {
// Filtro de b煤squeda
const matchSearch = !busqueda ||
(item.nombre && item.nombre.toLowerCase().includes(busqueda)) ||
(item.descripcion && item.descripcion.toLowerCase().includes(busqueda)) ||
(item.fecha && item.fecha.includes(busqueda)) ||
(item.fechaPago && item.fechaPago.includes(busqueda));
// Filtro por fecha
let matchDate = true;
if (fechaInicio) {
matchDate = matchDate && item.fechaPago >= fechaInicio;
}
if (fechaFin) {
matchDate = matchDate && item.fechaPago <= fechaFin;
}
return matchSearch && matchDate;
});
// Ordenar por fecha de pago (descendente)
historial.sort((a, b) => {
try { return new Date(b.fechaPago) - new Date(a.fechaPago); }
catch(e) { return 0; }
});
tbody.innerHTML = "";
let sumaTotalHistorial = 0;
if (historial.length === 0) {
const msg = busqueda ? "No se encontraron registros con ese criterio." : "El historial de pagos est谩 vac铆o.";
tbody.innerHTML = `<tr><td colspan="5" class="px-6 py-4 text-center text-sm text-gray-500">${msg}</td></tr>`;
} else {
historial.forEach(item => {
let fechaPagoF = item.fechaPago;
let fechaRegF = item.fecha;
try {
const dpParts = item.fechaPago?.split('-');
if (dpParts?.length === 3) fechaPagoF = new Date(parseInt(dpParts[0]), parseInt(dpParts[1]) - 1, parseInt(dpParts[2])).toLocaleDateString();
const drParts = item.fecha?.split('-');
if (drParts?.length === 3) fechaRegF = new Date(parseInt(drParts[0]), parseInt(drParts[1]) - 1, parseInt(drParts[2])).toLocaleDateString();
} catch (e) { /* Ignore date formatting errors silently */ }
const row = tbody.insertRow();
row.className = 'hover:bg-gray-50';
// Fecha Pago
const cellFechaPago = row.insertCell();
cellFechaPago.className = 'px-6 py-4 whitespace-nowrap text-sm text-gray-900';
cellFechaPago.textContent = fechaPagoF;
// Fecha Registro
const cellFechaReg = row.insertCell();
cellFechaReg.className = 'px-6 py-4 whitespace-nowrap text-sm text-gray-900';
cellFechaReg.textContent = fechaRegF;
// Nombre
const cellNombre = row.insertCell();
cellNombre.className = 'px-6 py-4 whitespace-nowrap text-sm text-gray-900';
cellNombre.textContent = item.nombre;
// Descripci贸n
const cellDesc = row.insertCell();
cellDesc.className = 'px-6 py-4 text-sm text-gray-900';
cellDesc.textContent = item.descripcion;
// Total
const cellTotal = row.insertCell();
cellTotal.className = 'px-6 py-4 whitespace-nowrap text-sm text-right font-medium';
cellTotal.textContent = `$${item.total?.toFixed(2) ?? '0.00'}`;
cellTotal.style.color = item.total < 0 ? '#dc2626' : '#1f2937';
if (typeof item.total === 'number' && !isNaN(item.total)) {
sumaTotalHistorial += item.total;
}
});
}
document.getElementById('suma-total-historial').textContent = `$${sumaTotalHistorial.toFixed(2)}`;
document.getElementById('historial-summary-total').textContent = `$${sumaTotalHistorial.toFixed(2)}`;
document.getElementById('historial-summary-count').textContent = historial.length;
// Update filter info
let filterInfo = "Mostrando todos los registros";
if (fechaInicio || fechaFin) {
filterInfo = `Filtrado por fecha: ${fechaInicio || 'inicio'} - ${fechaFin || 'fin'}`;
}
document.getElementById('historial-filter-info').textContent = filterInfo;
} catch (error) {
tbody.innerHTML = `<tr><td colspan="5, class="px-6 py-4 text-center text-sm text-red-500">Error al cargar historial: ${error.message}</td></tr>`;
}
}
/*******************************
* Funciones de edici贸n/eliminaci贸n
*******************************/
async function eliminarRegistro(id) {
if (!id) return;
if (confirm(`驴Est谩 seguro de eliminar permanentemente el registro pendiente?`)) {
try {
let pendientes = await memoryStorage.getPendientes();
const originalLength = pendientes.length;
pendientes = pendientes.filter(item => item.id != id);
if (pendientes.length < originalLength) {
await memoryStorage.setPendientes(pendientes);
await cargarDatos();
await updateDashboard();
await loadReports();
await generarBackupInterno().catch(err => console.error("Backup failed after delete:", err));
if (modoEdicion && idEdicion == id) {
resetearFormulario();
}
} else {
alert('Error: No se encontr贸 el registro para eliminar.');
}
} catch (error) {
alert(`Error al eliminar el registro: ${error.message}`);
}
}
}
async function marcarComoPagado(id) {
if (!id) return;
if (confirm(`驴Marcar este registro como pagado y moverlo al historial?`)) {
try {
let pendientes = await memoryStorage.getPendientes();
const index = pendientes.findIndex(item => item.id == id);
if (index !== -1) {
const itemPagado = pendientes[index];
await memoryStorage.addToHistorial(itemPagado);
pendientes.splice(index, 1);
await memoryStorage.setPendientes(pendientes);
await cargarDatos();
await cargarHistorial();
await updateDashboard();
await loadReports();
await generarBackupInterno().catch(err => console.error("Backup failed after mark paid:", err));
if (modoEdicion && idEdicion == id) {
resetearFormulario();
}
} else {
alert('Error: No se encontr贸 el registro para marcar como pagado.');
}
} catch(error) {
alert(`Error al marcar como pagado: ${error.message}`);
}
}
}
async function editarRegistro(id) {
if (!id) return;
try {
let pendientes = await memoryStorage.getPendientes();
const registro = pendientes.find(item => item.id == id);
if (registro) {
document.getElementById('fecha').value = registro.fecha;
document.getElementById('nombre').value = registro.nombre;
document.getElementById('descripcion').value = registro.descripcion;
document.getElementById('total').value = registro.total;
modoEdicion = true;
idEdicion = id;
btnAgregar.innerHTML = '<i class="fas fa-save mr-2"></i> Actualizar';
btnAgregar.className = 'bg-yellow-600 text-white py-2 px-6 rounded-lg hover:bg-yellow-700 transition flex items-center';
btnCancelarEdicion.classList.remove('hidden');
// Scroll to form
const nombreInput = document.getElementById('nombre');
if (nombreInput) {
nombreInput.focus();
nombreInput.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
} else {
alert("Error: No se pudo encontrar el registro para editar.");
resetearFormulario();
}
} catch (error) {
alert(`Error al preparar la edici贸n: ${error.message}`);
resetearFormulario();
}
}
function resetearFormulario() {
document.getElementById('fecha').valueAsDate = new Date();
document.getElementById('nombre').value = '';
document.getElementById('descripcion').value = '';
document.getElementById('total').value = '';
modoEdicion = false;
idEdicion = null;
btnAgregar.innerHTML = '<i class="fas fa-plus mr-2"></i> Agregar';
btnAgregar.className = 'bg-blue-600 text-white py-2 px-6 rounded-lg hover:bg-blue-700 transition flex items-center';
btnCancelarEdicion.classList.add('hidden');
document.getElementById('nombre').focus();
}
/*******************************
* Dashboard y Reportes
*******************************/
async function updateDashboard() {
try {
const pendientes = await memoryStorage.getPendientes();
const historial = await memoryStorage.getHistorial();
// Totales
const totalPendiente = pendientes.reduce((sum, item) => sum + (item.total || 0), 0);
const totalPagado = historial.reduce((sum, item) => sum + (item.total || 0), 0);
// Contadores
const countPendientes = pendientes.length;
const countHistorial = historial.length;
// Mes actual
const now = new Date();
const currentMonth = now.getMonth() + 1;
const currentYear = now.getFullYear();
const monthPagado = historial
.filter(item => {
const dateParts = item.fechaPago?.split('-');
if (dateParts?.length === 3) {
return parseInt(dateParts[0]) === currentYear && parseInt(dateParts[1]) === currentMonth;
}
return false;
})
.reduce((sum, item) => sum + (item.total || 0), 0);
// Vencidos (m谩s de 30 d铆as)
const countVencidos = pendientes.filter(item => {
try {
const itemDate = new Date(item.fecha);
const diffTime = now - itemDate;
const diffDays = diffTime / (1000 * 60 * 60 * 24);
return diffDays > 30;
} catch (e) {
return false;
}
}).length;
// Actualizar UI
document.getElementById('total-pendiente').textContent = `$${totalPendiente.toFixed(2)}`;
document.getElementById('total-pagado').textContent = `$${totalPagado.toFixed(2)}`;
document.getElementById('month-pagado').textContent = `$${monthPagado.toFixed(2)}`;
document.getElementById('count-pendientes').textContent = countPendientes;
document.getElementById('count-historial').textContent = countHistorial;
document.getElementById('count-vencidos').textContent = countVencidos;
document.getElementById('last-update').textContent = new Date().toLocaleTimeString();
// Pr贸ximos vencimientos (pr贸ximos 7 d铆as)
const upcomingPayments = pendientes
.filter(item => {
try {
const itemDate = new Date(item.fecha);
const diffTime = itemDate - now;
const diffDays = diffTime / (1000 * 60 * 60 * 24);
return diffDays > 0 && diffDays <= 7;
} catch (e) {
return false;
}
})
.sort((a, b) => new Date(a.fecha) - new Date(b.fecha))
.slice(0, 5);
const upcomingTbody = document.getElementById('upcoming-payments');
upcomingTbody.innerHTML = '';
if (upcomingPayments.length === 0) {
upcomingTbody.innerHTML = '<tr><td colspan="5" class="px-6 py-4 text-center text-sm text-gray-500">No hay pagos pr贸ximos en los pr贸ximos 7 d铆as</td></tr>';
} else {
upcomingPayments.forEach(item => {
const row = upcomingTbody.insertRow();
row.className = 'hover:bg-gray-50';
// Fecha
const cellFecha = row.insertCell();
cellFecha.className = 'px-6 py-4 whitespace-nowrap text-sm text-gray-900';
try {
const dateParts = item.fecha.split('-');
if (dateParts.length === 3) {
cellFecha.textContent = new Date(parseInt(dateParts[0]), parseInt(dateParts[1]) - 1, parseInt(dateParts[2])).toLocaleDateString();
} else {
cellFecha.textContent = item.fecha;
}
} catch (e) {
cellFecha.textContent = item.fecha;
}
// Nombre
const cellNombre = row.insertCell();
cellNombre.className = 'px-6 py-4 whitespace-nowrap text-sm text-gray-900';
cellNombre.textContent = item.nombre;
// Descripci贸n
const cellDesc = row.insertCell();
cellDesc.className = 'px-6 py-4 text-sm text-gray-900';
cellDesc.textContent = item.descripcion;
// Total
const cellTotal = row.insertCell();
cellTotal.className = 'px-6 py-4 whitespace-nowrap text-sm text-right font-medium';
cellTotal.textContent = `$${item.total?.toFixed(2) ?? '0.00'}`;
cellTotal.style.color = item.total < 0 ? '#dc2626' : '#1f2937';
// Estado
const cellEstado = row.insertCell();
cellEstado.className = 'px-6 py-4 whitespace-nowrap text-sm text-gray-900';
const badge = document.createElement('span');
badge.className = 'inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium';
try {
const itemDate = new Date(item.fecha);
const diffTime = itemDate - now;
const diffDays = diffTime / (1000 * 60 * 60 * 24);
if (diffDays < 0) {
badge.textContent = 'Vencido';
badge.classList.add('bg-red-100', 'text-red-800');
} else if (diffDays <= 3) {
badge.textContent = 'Pr贸ximo';
badge.classList.add('bg-yellow-100', 'text-yellow-800');
} else {
badge.textContent = 'Pendiente';
badge.classList.add('bg-blue-100', 'text-blue-800');
}
} catch (e) {
badge.textContent = 'Pendiente';
badge.classList.add('bg-blue-100', 'text-blue-800');
}
cellEstado.appendChild(badge);
});
}
// Actualizar gr谩ficos
actualizarGraficosDashboard(pendientes, historial);
} catch (error) {
console.error("Error updating dashboard:", error);
}
}
function actualizarGraficosDashboard(pendientes, historial) {
// Gr谩fico de evoluci贸n de pagos
const ctxPayments = document.getElementById('payments-chart').getContext('2d');
// Datos de ejemplo para el gr谩fico
const labels = ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic'];
const currentMonth = new Date().getMonth();
const currentYear = new Date().getFullYear();
// Calcular totales por mes
const monthlyTotals = Array(12).fill(0);
historial.forEach(item => {
try {
const dateParts = item.fechaPago?.split('-');
if (dateParts?.length === 3) {
const year = parseInt(dateParts[0]);
const month = parseInt(dateParts[1]) - 1;
if (year === currentYear && month >= 0 && month < 12) {
monthlyTotals[month] += item.total || 0;
}
}
} catch (e) { /* Ignore errors */ }
});
if (paymentsChart) {
paymentsChart.destroy();
}
paymentsChart = new Chart(ctxPayments, {
type: 'line',
data: {
labels: labels,
datasets: [{
label: 'Pagos por mes',
data: monthlyTotals,
backgroundColor: 'rgba(59, 130, 246, 0.1)',
borderColor: 'rgba(59, 130, 246, 1)',
borderWidth: 2,
tension: 0.1,
fill: true
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
display: false
}
},
scales: {
y: {
beginAtZero: true,
grid: {
color: 'rgba(0
</html> |