Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | <template> <div data-testid="admin-teams"> <div class="flex justify-between items-center mb-6"> <h3 class="text-lg font-semibold text-gray-900">Teams Management</h3> <button @click="showAddModal = true" data-testid="add-team-button" class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-md text-sm font-medium" > Add Team </button> </div> <!-- Loading State --> <div v-if="loading" class="flex justify-center py-8" data-testid="teams-loading" > <div class="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600" ></div> </div> <!-- Error State --> <div v-if="error" class="bg-red-50 border border-red-200 rounded-md p-4 mb-4" data-testid="teams-error" > <div class="text-red-800">{{ error }}</div> </div> <!-- Teams Table --> <div v-else class="overflow-hidden shadow ring-1 ring-black ring-opacity-5 md:rounded-lg" data-testid="teams-table-container" > <table class="min-w-full divide-y divide-gray-300" data-testid="teams-table" > <thead class="bg-gray-50"> <tr> <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider" > Name </th> <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider" > Parent Club </th> <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider" > League </th> <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider" > Age Groups </th> <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider" > Actions </th> </tr> </thead> <tbody class="bg-white divide-y divide-gray-200" data-testid="teams-tbody" > <tr v-for="team in teams" :key="team.id" :data-testid="`team-row-${team.id}`" data-team-row > <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900" data-testid="team-name" > {{ team.name }} </td> <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"> <span v-if="team.parent_club" class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-purple-100 text-purple-800" > {{ team.parent_club.name }} </span> <span v-else class="text-gray-400 italic">Independent</span> </td> <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"> <span v-if="team.league_name === 'Academy'" class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-purple-100 text-purple-800" > Academy </span> <span v-else-if="team.league_name === 'Homegrown'" class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-green-100 text-green-800" > Homegrown </span> <span v-else class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-gray-100 text-gray-800" > {{ team.league_name || 'Unknown' }} </span> </td> <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"> <div class="flex flex-wrap gap-1"> <span v-for="ageGroup in (team.age_groups || []).filter(ag => ag)" :key="ageGroup.id" class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-blue-100 text-blue-800" > {{ ageGroup.name }} </span> </div> </td> <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium" > <button @click="editTeam(team)" class="text-blue-600 hover:text-blue-900 mr-3" data-testid="edit-team-button" > Edit </button> <button @click="manageTeamMappings(team)" class="text-green-600 hover:text-green-900 mr-3" data-testid="manage-leagues-button" > Leagues </button> <button @click="manageRoster(team)" class="text-purple-600 hover:text-purple-900 mr-3" data-testid="manage-roster-button" > Roster </button> <button @click="deleteTeam(team)" class="text-red-600 hover:text-red-900" data-testid="delete-team-button" > Delete </button> </td> </tr> </tbody> </table> </div> <!-- Add/Edit Team Modal --> <div v-if="showAddModal || showEditModal" class="modal-overlay" @click="closeModals" data-testid="team-modal-overlay" > <div class="modal-content" @click.stop data-testid="team-modal"> <div class="p-6"> <h3 class="text-lg font-medium text-gray-900 mb-4" data-testid="team-modal-title" > {{ showEditModal ? 'Edit Team' : 'Add New Team' }} </h3> <form @submit.prevent="showEditModal ? updateTeam() : createTeam()" data-testid="team-form" > <div class="mb-4"> <label class="block text-sm font-medium text-gray-700 mb-2" >Team Name</label > <input v-model="formData.name" type="text" required data-testid="team-name-input" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="e.g., New York City FC, Boston United..." /> </div> <div class="mb-4"> <label class="block text-sm font-medium text-gray-700 mb-2" >City</label > <input v-model="formData.city" type="text" data-testid="team-city-input" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" placeholder="e.g., New York, Boston..." /> </div> <div class="mb-4"> <label class="block text-sm font-medium text-gray-700 mb-2" >Parent Club</label > <select v-model="formData.parentClubId" :disabled="isClubManager()" data-testid="team-club-select" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 disabled:bg-gray-100 disabled:cursor-not-allowed" > <option v-if="!isClubManager()" :value="null"> Independent Team (No Parent Club) </option> <option v-for="club in clubs" :key="club.id" :value="club.id"> {{ club.name }} </option> </select> <p class="text-xs text-gray-500 mt-1"> <span v-if="isClubManager()"> Teams you create will be assigned to your club </span> <span v-else> Link this team to a parent club organization </span> </p> </div> <div class="mb-4"> <label class="block text-sm font-medium text-gray-700 mb-2" >Team Type<span v-if="!showEditModal" class="text-red-500" >*</span ></label > <select v-model="formData.teamType" @change="onTeamTypeChange" :required="!showEditModal" data-testid="team-type-select" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" > <option value="">Select Team Type</option> <option value="league">League Team</option> <option value="guest">Guest Team</option> <option value="tournament">Tournament Team</option> </select> <p class="text-xs text-gray-500 mt-1"> League teams can play in all game types. Guest teams are for friendlies only. Tournament teams can play in tournaments and friendlies. </p> </div> <!-- League and Division Selection (only for Add, not Edit) --> <div v-if="!showEditModal && formData.teamType === 'league'" class="mb-4" > <label class="block text-sm font-medium text-gray-700 mb-2" >League <span class="text-red-500">*</span></label > <select v-model="formData.leagueId" @change="formData.divisionId = null" required data-testid="team-league-select" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" > <option :value="null">Select League</option> <option v-for="league in leagues" :key="league.id" :value="league.id" > {{ league.name }} </option> </select> <p class="text-xs text-gray-500 mt-1"> Select which league this team will participate in </p> </div> <div v-if=" !showEditModal && formData.teamType === 'league' && formData.leagueId " class="mb-4" > <label class="block text-sm font-medium text-gray-700 mb-2" >Division <span class="text-red-500">*</span></label > <select v-model="formData.divisionId" required data-testid="team-division-select" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" > <option :value="null">Select Division</option> <option v-for="division in formFilteredDivisions" :key="division.id" :value="division.id" > {{ division.name }} </option> </select> <p class="text-xs text-gray-500 mt-1"> Select the division within the league (e.g., Northeast, Bracket A) </p> </div> <div v-if="showEditModal" class="mb-4"> <label class="block text-sm font-medium text-gray-700 mb-2" >Current Age Groups</label > <div class="flex flex-wrap gap-1 mb-2"> <span v-for="ageGroup in editingTeam?.age_groups || []" :key="ageGroup.id" class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-blue-100 text-blue-800" > {{ ageGroup.name }} </span> <span v-if="!editingTeam?.age_groups?.length" class="text-sm text-gray-500 italic" > No age groups assigned </span> </div> <p class="text-xs text-gray-500"> Use the "Leagues" button to manage age group assignments </p> </div> <div v-if="!showEditModal" class="mb-4"> <label class="block text-sm font-medium text-gray-700 mb-2" >Age Groups</label > <div class="space-y-2 max-h-40 overflow-y-auto border border-gray-300 rounded-md p-3" > <label v-for="ageGroup in ageGroups" :key="ageGroup.id" class="flex items-center text-sm" > <input type="checkbox" :value="ageGroup.id" v-model="formData.ageGroupIds" class="rounded border-gray-300 text-blue-600 focus:ring-blue-500" /> <span class="ml-2">{{ ageGroup.name }}</span> </label> </div> <p class="text-xs text-gray-500 mt-1"> Select one or more age groups this team will participate in </p> </div> <div class="mb-4"> <label class="block text-sm font-medium text-gray-700 mb-2"> Game Types Participation <span v-if="!showEditModal" class="text-xs text-gray-500" >(auto-selected based on team type)</span > </label> <div class="space-y-2"> <label v-for="gameType in gameTypes" :key="gameType.id" class="flex items-center text-sm" > <input type="checkbox" :value="gameType.id" v-model="formData.gameTypeIds" class="rounded border-gray-300 text-blue-600 focus:ring-blue-500" /> <span class="ml-2">{{ gameType.name }}</span> </label> </div> <p v-if="showEditModal" class="text-xs text-gray-500 mt-1"> Note: This will update game type participation for all age groups this team is assigned to </p> </div> <div class="mb-4"> <label class="flex items-center text-sm font-medium text-gray-700" > <input type="checkbox" v-model="formData.academyTeam" class="rounded border-gray-300 text-blue-600 focus:ring-blue-500 mr-2" /> Pro Academy Team </label> <p class="text-xs text-gray-500 mt-1"> Check this if this is a pro academy team </p> </div> <div class="flex justify-end space-x-3"> <button type="button" @click="closeModals" data-testid="team-modal-cancel" class="px-4 py-2 text-sm font-medium text-gray-700 bg-gray-100 hover:bg-gray-200 rounded-md" > Cancel </button> <button type="submit" :disabled="formLoading" data-testid="team-modal-submit" class="px-4 py-2 text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 rounded-md disabled:opacity-50" > {{ formLoading ? 'Saving...' : showEditModal ? 'Update' : 'Create' }} </button> </div> </form> </div> </div> </div> <!-- Team Mappings Modal --> <div v-if="showMappingsModal" class="modal-overlay" @click="closeMappingsModal" > <div class="modal-content" @click.stop> <div class="p-6"> <h3 class="text-lg font-medium text-gray-900 mb-4"> Manage League Assignments - {{ selectedTeam?.name }} </h3> <div class="mb-4"> <p class="text-sm text-gray-600 mb-4"> Assign this team to age groups and divisions. Each assignment creates a league participation. </p> <!-- Current Mappings --> <div class="mb-6"> <h4 class="text-sm font-medium text-gray-700 mb-2"> Current Assignments </h4> <div class="space-y-2"> <div v-for="mapping in selectedTeam?.team_mappings" :key="`${mapping.age_groups.id}-${mapping.divisions.id}`" class="flex items-center justify-between p-3 border border-gray-200 rounded-md" > <span class="text-sm"> <span class="font-medium">{{ mapping.divisions?.leagues?.name || 'Unknown League' }}</span> <span class="text-gray-400 mx-1">/</span> {{ mapping.divisions.name }} <span class="text-gray-400 mx-1">/</span> {{ mapping.age_groups.name }} </span> <button @click="removeTeamMapping(mapping)" class="text-red-600 hover:text-red-900 text-sm" > Remove </button> </div> <div v-if="!selectedTeam?.team_mappings?.length" class="text-sm text-gray-500 italic" > No league assignments yet </div> </div> </div> <!-- Add New Mapping --> <div class="border-t pt-4"> <h4 class="text-sm font-medium text-gray-700 mb-3"> Add New Assignment </h4> <form @submit.prevent="addTeamMapping()" class="space-y-3"> <div class="grid grid-cols-3 gap-3"> <div> <label class="block text-sm font-medium text-gray-700 mb-1" >League</label > <select v-model="mappingForm.league_id" required class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" > <option value="">Select League</option> <option v-for="league in leagues" :key="league.id" :value="league.id" > {{ league.name }} </option> </select> </div> <div> <label class="block text-sm font-medium text-gray-700 mb-1" >Division</label > <select v-model="mappingForm.division_id" required :disabled="!mappingForm.league_id" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 disabled:bg-gray-100 disabled:cursor-not-allowed" > <option value=""> {{ mappingForm.league_id ? 'Select Division' : 'Select League first' }} </option> <option v-for="division in filteredDivisions" :key="division.id" :value="division.id" > {{ division.name }} </option> </select> </div> <div> <label class="block text-sm font-medium text-gray-700 mb-1" >Age Group</label > <select v-model="mappingForm.age_group_id" required class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" > <option value="">Select Age Group</option> <option v-for="ageGroup in ageGroups" :key="ageGroup.id" :value="ageGroup.id" > {{ ageGroup.name }} </option> </select> </div> </div> <button type="submit" :disabled="mappingLoading" class="px-4 py-2 text-sm font-medium text-white bg-green-600 hover:bg-green-700 rounded-md disabled:opacity-50" > {{ mappingLoading ? 'Adding...' : 'Add Assignment' }} </button> </form> </div> </div> <div class="flex justify-end"> <button @click="closeMappingsModal" class="px-4 py-2 text-sm font-medium text-gray-700 bg-gray-100 hover:bg-gray-200 rounded-md" > Close </button> </div> </div> </div> </div> <!-- Roster Manager Modal --> <RosterManager v-if="showRosterModal && selectedTeamForRoster" :team-id="selectedTeamForRoster.id" :team-name="selectedTeamForRoster.name" :season-id="currentSeasonId" :age-group-id="selectedTeamForRoster.age_groups?.[0]?.id" @close="closeRosterModal" /> </div> </template> <script> import { ref, computed, onMounted } from 'vue'; import { useAuthStore } from '@/stores/auth'; import { getApiBaseUrl } from '../../config/api'; import RosterManager from '@/components/roster/RosterManager.vue'; export default { name: 'AdminTeams', components: { RosterManager, }, setup() { const authStore = useAuthStore(); const teams = ref([]); const clubs = ref([]); const ageGroups = ref([]); const divisions = ref([]); const leagues = ref([]); const gameTypes = ref([]); const loading = ref(true); const formLoading = ref(false); const mappingLoading = ref(false); const error = ref(null); const showAddModal = ref(false); const showEditModal = ref(false); const showMappingsModal = ref(false); const showRosterModal = ref(false); const editingTeam = ref(null); const selectedTeam = ref(null); const selectedTeamForRoster = ref(null); const seasons = ref([]); const currentSeasonId = ref(null); const formData = ref({ name: '', city: '', parentClubId: null, teamType: '', ageGroupIds: [], gameTypeIds: [], academyTeam: false, leagueId: null, divisionId: null, }); const mappingForm = ref({ league_id: '', age_group_id: '', division_id: '', }); // Computed property to filter divisions by selected league (for mappings modal) const filteredDivisions = computed(() => { if (!mappingForm.value.league_id) { return []; } return divisions.value.filter( d => d.league_id === parseInt(mappingForm.value.league_id) ); }); // Computed property to filter divisions by selected league (for team creation form) const formFilteredDivisions = computed(() => { if (!formData.value.leagueId) { return []; } return divisions.value.filter( d => d.league_id === parseInt(formData.value.leagueId) ); }); const fetchTeams = async () => { try { loading.value = true; const response = await authStore.apiRequest( `${getApiBaseUrl()}/api/teams?include_parent=true`, { method: 'GET', } ); teams.value = response; } catch (err) { error.value = err.message; } finally { loading.value = false; } }; const fetchAgeGroups = async () => { try { const response = await authStore.apiRequest( `${getApiBaseUrl()}/api/age-groups`, { method: 'GET', } ); ageGroups.value = response; } catch (err) { console.error('Error fetching age groups:', err); } }; const fetchClubs = async () => { try { // Club managers can only see their own club const userRole = authStore.userRole.value; const userClubId = authStore.userClubId.value; if (userRole === 'club_manager' && userClubId) { // Fetch only the club manager's club const response = await authStore.apiRequest( `${getApiBaseUrl()}/api/clubs/${userClubId}`, { method: 'GET', } ); clubs.value = [response]; // Auto-select the club for club managers formData.value.parentClubId = userClubId; } else { // Admins can see all clubs const response = await authStore.apiRequest( `${getApiBaseUrl()}/api/clubs?include_empty=true`, { method: 'GET', } ); clubs.value = response.sort((a, b) => a.name.localeCompare(b.name)); } } catch (err) { console.error('Error fetching clubs:', err); // Not fatal - parent club is optional } }; // Check if user is a club manager (used for UI restrictions) const isClubManager = () => { return authStore.userRole.value === 'club_manager'; }; const fetchDivisions = async () => { try { const response = await authStore.apiRequest( `${getApiBaseUrl()}/api/divisions`, { method: 'GET', } ); divisions.value = response; } catch (err) { console.error('Error fetching divisions:', err); } }; const fetchLeagues = async () => { try { const response = await authStore.apiRequest( `${getApiBaseUrl()}/api/leagues`, { method: 'GET', } ); leagues.value = response; } catch (err) { console.error('Error fetching leagues:', err); } }; const fetchGameTypes = async () => { try { const response = await authStore.apiRequest( `${getApiBaseUrl()}/api/match-types`, { method: 'GET', } ); gameTypes.value = response; } catch (err) { console.error('Error fetching game types:', err); } }; const fetchSeasons = async () => { try { const response = await authStore.apiRequest( `${getApiBaseUrl()}/api/seasons`, { method: 'GET', } ); seasons.value = response; // Set current season to the most recent one (first in list, assuming sorted desc) if (response.length > 0) { currentSeasonId.value = response[0].id; } } catch (err) { console.error('Error fetching seasons:', err); } }; const onTeamTypeChange = () => { // Auto-select game types based on team type const teamType = formData.value.teamType; const leagueId = gameTypes.value.find(gt => gt.name === 'League')?.id; const friendlyId = gameTypes.value.find(gt => gt.name === 'Friendly')?.id; const tournamentId = gameTypes.value.find( gt => gt.name === 'Tournament' )?.id; const playoffId = gameTypes.value.find(gt => gt.name === 'Playoff')?.id; if (teamType === 'league') { // League teams can participate in all game types formData.value.gameTypeIds = [ leagueId, friendlyId, tournamentId, playoffId, ].filter(id => id); } else if (teamType === 'guest') { // Guest teams typically only for friendlies formData.value.gameTypeIds = [friendlyId].filter(id => id); } else if (teamType === 'tournament') { // Tournament teams for tournaments and friendlies formData.value.gameTypeIds = [tournamentId, friendlyId].filter( id => id ); } else { formData.value.gameTypeIds = []; } }; const createTeam = async () => { try { formLoading.value = true; // Clear any previous errors error.value = null; // Validate that at least one age group is selected if ( !formData.value.ageGroupIds || formData.value.ageGroupIds.length === 0 ) { error.value = 'Please select at least one age group'; return; } // Validate that a division is selected (only for league teams) if ( formData.value.teamType === 'league' && !formData.value.divisionId ) { error.value = 'Please select a league and division'; return; } // Create the team with basic info and age groups const teamData = { name: formData.value.name, city: formData.value.city, club_id: formData.value.parentClubId, age_group_ids: formData.value.ageGroupIds.map(id => parseInt(id)), match_type_ids: formData.value.gameTypeIds.map(id => parseInt(id)), division_id: formData.value.divisionId ? parseInt(formData.value.divisionId) : null, academy_team: formData.value.academyTeam, }; await authStore.apiRequest(`${getApiBaseUrl()}/api/teams`, { method: 'POST', body: JSON.stringify(teamData), }); // Note: In a full implementation, you would also add game type participation here // using the team ID returned from the response and the selected game type IDs await fetchTeams(); closeModals(); resetForm(); } catch (err) { error.value = err.message; } finally { formLoading.value = false; } }; const editTeam = team => { editingTeam.value = team; // Determine team type based on current game type participation let teamType = 'league'; // default const teamGameTypes = team.team_game_types || []; const hasLeague = teamGameTypes.some(tgt => tgt.game_type_id === 1); const hasTournament = teamGameTypes.some(tgt => tgt.game_type_id === 2); const hasFriendly = teamGameTypes.some(tgt => tgt.game_type_id === 3); const hasPlayoff = teamGameTypes.some(tgt => tgt.game_type_id === 4); if (!hasLeague && !hasTournament && !hasPlayoff && hasFriendly) { teamType = 'guest'; } else if (!hasLeague && hasTournament && hasFriendly && !hasPlayoff) { teamType = 'tournament'; } // Get unique game type IDs from all age groups const gameTypeIds = [ ...new Set(teamGameTypes.map(tgt => tgt.game_type_id)), ]; formData.value = { name: team.name, city: team.city, parentClubId: team.club_id || null, teamType: teamType, ageGroupIds: (team.age_groups || []).map(ag => ag.id), gameTypeIds: gameTypeIds, academyTeam: team.academy_team || false, }; showEditModal.value = true; }; const updateTeam = async () => { try { formLoading.value = true; const updateData = { name: formData.value.name, city: formData.value.city, club_id: formData.value.parentClubId, academy_team: formData.value.academyTeam, }; await authStore.apiRequest( `${getApiBaseUrl()}/api/teams/${editingTeam.value.id}`, { method: 'PUT', body: JSON.stringify(updateData), } ); // Update game type participations if changed const currentGameTypes = editingTeam.value.team_game_types || []; const currentGameTypeIds = [ ...new Set(currentGameTypes.map(tgt => tgt.game_type_id)), ]; const newGameTypeIds = formData.value.gameTypeIds; // Remove game types that are no longer selected for (const gameTypeId of currentGameTypeIds) { if (!newGameTypeIds.includes(gameTypeId)) { // Remove for all age groups for (const ageGroup of editingTeam.value.age_groups || []) { try { await authStore.apiRequest( `${getApiBaseUrl()}/api/teams/${editingTeam.value.id}/game-types/${gameTypeId}/${ageGroup.id}`, { method: 'DELETE', } ); } catch (err) { console.error( `Failed to remove game type ${gameTypeId} for age group ${ageGroup.id}:`, err ); } } } } // Add new game types for (const gameTypeId of newGameTypeIds) { if (!currentGameTypeIds.includes(gameTypeId)) { // Add for all age groups for (const ageGroup of editingTeam.value.age_groups || []) { try { await authStore.apiRequest( `${getApiBaseUrl()}/api/teams/${editingTeam.value.id}/game-types`, { method: 'POST', body: JSON.stringify({ game_type_id: gameTypeId, age_group_id: ageGroup.id, }), } ); } catch (err) { console.error( `Failed to add game type ${gameTypeId} for age group ${ageGroup.id}:`, err ); } } } } await fetchTeams(); closeModals(); resetForm(); } catch (err) { error.value = err.message; } finally { formLoading.value = false; } }; const deleteTeam = async team => { if (!confirm(`Are you sure you want to delete "${team.name}"?`)) return; try { await authStore.apiRequest(`${getApiBaseUrl()}/api/teams/${team.id}`, { method: 'DELETE', }); await fetchTeams(); } catch (err) { error.value = err.message; } }; const manageTeamMappings = team => { selectedTeam.value = team; showMappingsModal.value = true; }; const addTeamMapping = async () => { try { mappingLoading.value = true; await authStore.apiRequest(`${getApiBaseUrl()}/api/team-mappings`, { method: 'POST', body: JSON.stringify({ team_id: selectedTeam.value.id, age_group_id: parseInt(mappingForm.value.age_group_id), division_id: parseInt(mappingForm.value.division_id), }), }); await fetchTeams(); selectedTeam.value = teams.value.find( t => t.id === selectedTeam.value.id ); mappingForm.value = { league_id: '', age_group_id: '', division_id: '', }; } catch (err) { error.value = err.message; } finally { mappingLoading.value = false; } }; const removeTeamMapping = async mapping => { if (!confirm('Are you sure you want to remove this league assignment?')) return; try { await authStore.apiRequest( `${getApiBaseUrl()}/api/team-mappings/${selectedTeam.value.id}/${mapping.age_groups.id}/${mapping.divisions.id}`, { method: 'DELETE', } ); await fetchTeams(); selectedTeam.value = teams.value.find( t => t.id === selectedTeam.value.id ); } catch (err) { error.value = err.message; } }; const closeModals = () => { showAddModal.value = false; showEditModal.value = false; editingTeam.value = null; resetForm(); }; const closeMappingsModal = () => { showMappingsModal.value = false; selectedTeam.value = null; mappingForm.value = { league_id: '', age_group_id: '', division_id: '' }; }; const manageRoster = team => { selectedTeamForRoster.value = team; showRosterModal.value = true; }; const closeRosterModal = () => { showRosterModal.value = false; selectedTeamForRoster.value = null; }; const resetForm = () => { formData.value = { name: '', city: '', parentClubId: null, teamType: '', ageGroupIds: [], gameTypeIds: [], academyTeam: false, leagueId: null, divisionId: null, }; }; onMounted(async () => { // Fetch teams with game counts (optimized backend aggregation) // Other reference data (clubs, age groups, etc.) loaded in parallel await Promise.all([ fetchTeams(), // Now includes game_count for each team fetchClubs(), fetchAgeGroups(), fetchDivisions(), fetchLeagues(), fetchGameTypes(), fetchSeasons(), ]); }); return { teams, clubs, ageGroups, divisions, leagues, filteredDivisions, formFilteredDivisions, gameTypes, seasons, currentSeasonId, loading, formLoading, mappingLoading, error, showAddModal, showEditModal, showMappingsModal, showRosterModal, selectedTeam, selectedTeamForRoster, editingTeam, formData, mappingForm, onTeamTypeChange, createTeam, editTeam, updateTeam, deleteTeam, manageTeamMappings, manageRoster, addTeamMapping, removeTeamMapping, closeModals, closeMappingsModal, closeRosterModal, isClubManager, }; }, }; </script> <style scoped> .modal-overlay { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(0, 0, 0, 0.5); display: flex; justify-content: center; align-items: center; z-index: 1000; } .modal-content { background: white; border-radius: 8px; max-width: 600px; width: 90%; max-height: 90vh; overflow-y: auto; } </style> |