import {Component, Inject, OnInit} from '@angular/core'; import {MatTableDataSource} from "@angular/material/table"; import {Client} from "../../../groups/model/model"; import {ToastrService} from "ngx-toastr"; import {HttpClient} from "@angular/common/http"; import {MAT_DIALOG_DATA, MatDialog, MatDialogRef} from "@angular/material/dialog"; import {ConfigService} from "@services/config.service"; import {DeleteModalComponent} from "../../../../shared/delete_modal/delete-modal/delete-modal.component"; import {CreateTaskScheduleComponent} from "../create-task-schedule/create-task-schedule.component"; import {DatePipe} from "@angular/common"; @Component({ selector: 'app-show-task-schedule', templateUrl: './show-task-schedule.component.html', styleUrl: './show-task-schedule.component.css' }) export class ShowTaskScheduleComponent implements OnInit{ baseUrl: string; dataSource = new MatTableDataSource([]); length = 0; itemsPerPage: number = 10; pageSizeOptions: number[] = [5, 10, 20]; page = 0; loading: boolean = false; filters: { [key: string]: string } = {}; datePipe: DatePipe = new DatePipe('es-ES'); columns = [ { columnDef: 'id', header: 'ID', cell: (schedule: any) => schedule.id }, { columnDef: 'recurrenceType', header: 'Recurrencia', cell: (schedule: any) => schedule.recurrenceType }, { columnDef: 'time', header: 'Hora de ejecución', cell: (schedule: any) => this.datePipe.transform(schedule.executionTime, 'HH:mm') }, { columnDef: 'daysOfWeek', header: 'Dias de la semana', cell: (schedule: any) => schedule.recurrenceDetails.daysOfWeek }, { columnDef: 'months', header: 'Meses', cell: (schedule: any) => schedule.recurrenceDetails.months }, { columnDef: 'enabled', header: 'Activo', cell: (schedule: any) => schedule.enabled } ]; displayedColumns: string[] = ['id', 'recurrenceType', 'time', 'daysOfWeek', 'months', 'enabled', 'actions']; constructor( private toastService: ToastrService, private http: HttpClient, public dialogRef: MatDialogRef, public dialog: MatDialog, private configService: ConfigService, @Inject(MAT_DIALOG_DATA) public data: any ) { this.baseUrl = this.configService.apiUrl; } ngOnInit(): void { if (this.data) { this.loadData(); } } loadData() { this.loading = true; this.http.get(`${this.baseUrl}/command-task-schedules?page=${this.page + 1}&itemsPerPage=${this.itemsPerPage}&commandTask.id=${this.data.commandTask?.id}`, { params: this.filters }).subscribe( (data) => { this.dataSource.data = data['hydra:member']; this.length = data['hydra:totalItems']; this.loading = false; }, (error) => { this.loading = false; } ); } editSchedule(schedule: any): void { this.dialog.open(CreateTaskScheduleComponent, { width: '800px', data: { schedule: schedule, task: this.data.commandTask } }).afterClosed().subscribe(() => this.loadData()); } deleteSchedule(schedule: any): void { const dialogRef = this.dialog.open(DeleteModalComponent, { width: '300px', data: { name: 'tarea programada' } }); dialogRef.afterClosed().subscribe(result => { if (result) { this.http.delete(`${this.baseUrl}${schedule['@id']}`).subscribe( () => { this.toastService.success('Programación eliminada correctamente'); this.loadData(); }, (error) => { this.toastService.error(error.error['hydra:description']); } ); } }) } onNoClick(): void { this.dialogRef.close(false); } onPageChange(event: any) { this.page = event.pageIndex; this.itemsPerPage = event.pageSize; this.loadData() } }