import {AfterViewInit, Component, EventEmitter, Output, ViewChild} from '@angular/core'; import {OtherComponent, SelectData} from "@component/schedule/tabs/other/other.component"; import {MatTab, MatTabGroup} from "@angular/material/tabs"; import {Observable} from "rxjs"; import {FormsModule, ReactiveFormsModule} from "@angular/forms"; import {MatButton} from "@angular/material/button"; import {GroupComponent} from "@component/schedule/tabs/group/group.component"; import {ProfessorComponent} from "@component/schedule/tabs/professor/professor.component"; import {LectureHallComponent} from "@component/schedule/tabs/lecture-hall/lecture-hall.component"; import {ScheduleService} from "@api/v1/schedule.service"; import {ScheduleResponse} from "@api/v1/scheduleResponse"; import {IScheduleTab} from "@component/schedule/tabs/ischedule-tab"; import {DisciplineService} from "@api/v1/discipline.service"; import {LectureHallService} from "@api/v1/lectureHall.service"; import {GroupService} from "@api/v1/group.service"; import {ProfessorService} from "@api/v1/professor.service"; import {AuthRoles} from "@model/authRoles"; import {HasRoleDirective} from "@/directives/has-role.directive"; import {TabSelectType, TabStorageService} from "@service/tab-storage.service"; import {ScheduleRequest} from "@api/v1/scheduleRequest"; import {CampusService} from "@api/v1/campus.service"; import {LessonTypeService} from "@api/v1/lessonType.service"; export enum TabsSelect { Group, Professor, LectureHall, Other } @Component({ selector: 'app-tabs', standalone: true, imports: [ OtherComponent, MatTabGroup, MatTab, ReactiveFormsModule, MatButton, GroupComponent, ProfessorComponent, LectureHallComponent, FormsModule, HasRoleDirective ], templateUrl: './tabs.component.html', styleUrl: './tabs.component.css', providers: [ ScheduleService, DisciplineService, LectureHallService, GroupService, ProfessorService, TabStorageService, CampusService, LessonTypeService] }) export class TabsComponent implements AfterViewInit { @Output() eventResult = new EventEmitter<[TabsSelect, number, Observable, ScheduleRequest]>(); private currentTab: number = -1; constructor(private scheduleApi: ScheduleService, private disciplineApi: DisciplineService, private lectureApi: LectureHallService, private groupApi: GroupService, private professorApi: ProfessorService, private tabStorage: TabStorageService, private campusApi: CampusService, private lessonTypeApi: LessonTypeService) { } ngAfterViewInit(): void { this.groupTab.selectChangeEvent.subscribe(event => this.tabStorage.select(TabSelectType.group, event)); this.professorTab.selectChangeEvent.subscribe(event => this.tabStorage.select(TabSelectType.professor, event)); this.lectureHallTab.selectChangeEvent.subscribe(event => this.tabStorage.select(TabSelectType.lecture, event)); this.groupTab.eventResult.subscribe(event => this.eventResult.emit( [ TabsSelect.Group, event, this.scheduleApi.getByGroup(event), {groups: [event]} ] )); this.professorTab.eventResult.subscribe(event => this.eventResult.emit( [ TabsSelect.Professor, event, this.scheduleApi.getByProfessor(event), {professors: [event]} ] )); this.lectureHallTab.eventResult.subscribe(event => this.eventResult.emit( [ TabsSelect.LectureHall, event, this.scheduleApi.getByLectureHall(event), {lectureHalls: [event]} ] )); let selected = TabStorageService.selected; let index = 0; if (selected !== null) { const selectedKeys = Object.keys(selected?.selected); if (selected.type === null) { if (this.groupTab.getEnclosureList().every((value, index) => value === selectedKeys[index])) index = 0; else if (this.professorTab.getEnclosureList().every((value, index) => value === selectedKeys[index])) index = 1; else if (this.lectureHallTab.getEnclosureList().every((value, index) => value === selectedKeys[index])) index = 2; } else index = selected.type; } if (index === 0) this.chooseTabs(0).then(); else this.tabs.selectedIndex = index; } protected async chooseTabs(index: number) { let needGetEnclosure = false; if (this.currentTab !== index) { this.currentTab = index; needGetEnclosure = true; } switch (index) { case 0: this.groupTab.load(); if (needGetEnclosure) this.tabStorage.enclosure = this.groupTab.getEnclosureList(); break; case 1: this.professorTab.load(); if (needGetEnclosure) this.tabStorage.enclosure = this.professorTab.getEnclosureList(); break; case 2: this.lectureHallTab.load(); if (needGetEnclosure) this.tabStorage.enclosure = this.lectureHallTab.getEnclosureList(); break; case 3: await this.loadDisciplines(); await this.loadLectureHalls(); await this.loadGroups(); await this.loadProfessors(); await this.loadLessonType(); break; default: await this.chooseTabs(0); break; } } protected async loadDisciplines() { this.disciplineApi.getDisciplines().subscribe(data => { this.disciplineEx.Data = data.map(x => ({ id: x.id, name: x.name }) as SelectData); }); } protected async loadLectureHalls() { this.campusApi.getCampus().subscribe(campus => { this.lectureApi.getLectureHalls().subscribe(data => { this.lectureHallEx.Data = data.map(x => ({ id: x.id, name: x.name + ` (${campus.find(c => c.id == x.campusId)?.codeName})` }) as SelectData); }); }); } protected async loadGroups() { this.groupApi.getGroups().subscribe(data => { this.groupEx.Data = data.map(x => ({ id: x.id, name: x.name }) as SelectData); }); } protected async loadProfessors() { this.professorApi.getProfessors().subscribe(data => { this.professorEx.Data = data.map(x => ({ id: x.id, name: x.name }) as SelectData); }); } protected async loadLessonType() { this.lessonTypeApi.getLessonTypes().subscribe(data => { this.lessonTypeEx.Data = data.map(x => ({ id: x.id, name: x.name }) as SelectData); }); } @ViewChild('groupTab') groupTab!: IScheduleTab; @ViewChild('professorTab') professorTab!: IScheduleTab; @ViewChild('lectureHallTab') lectureHallTab!: IScheduleTab; @ViewChild('discipline') disciplineEx!: OtherComponent; @ViewChild('lecture') lectureHallEx!: OtherComponent; @ViewChild('group') groupEx!: OtherComponent; @ViewChild('professor') professorEx!: OtherComponent; @ViewChild('lesson_type') lessonTypeEx!: OtherComponent; @ViewChild('tabGroup') tabs!: MatTabGroup; protected readonly AuthRoles = AuthRoles; protected otherFilter() { const data: ScheduleRequest = ({ groups: this.groupEx.selectedIds, disciplines: this.disciplineEx.selectedIds, professors: this.professorEx.selectedIds, lectureHalls: this.lectureHallEx.selectedIds, lessonType: this.lessonTypeEx.selectedIds }); this.eventResult.emit( [ TabsSelect.Other, 0, this.scheduleApi.postSchedule(data), data ] ); } }