import { Component, Inject, OnInit } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialog, MatDialogRef } from '@angular/material/dialog';
import { CalendarEvent } from 'src/app/interfaces/calendar-events.interface';
import { ExecutionDialogComponent } from '../execution-dialog/execution-dialog.component';

@Component({
  selector: 'app-complete-events',
  templateUrl: './complete-events.component.html',
  styleUrls: ['./complete-events.component.css']
})
export class CompleteEventsComponent implements OnInit {
  dateStr: string;
  dayEvents: CalendarEvent[];

  MONTHS = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];

  constructor(
    @Inject(MAT_DIALOG_DATA) private _data: any,
    private _dialog: MatDialog,
    private _dialofRef: MatDialogRef<CompleteEventsComponent>,
  ) { 
    this.dateStr = '';
    this.dayEvents = [];
  }

  ngOnInit(): void {
    let dateArr = this._data.dateStr.split('-');
    this.dateStr = `${parseInt(dateArr[2])} de ${this.MONTHS[parseInt(dateArr[1]) - 1]} del ${dateArr[0]}`;
    this.dayEvents = this._data.events;
  }

  openExecution(idOrder: number){
    let dialogRef = this._dialog.open(ExecutionDialogComponent, {
      width: '480px',
      data: {
        idOrder: idOrder,
        date: this._data.dateStr,
      }
    });

    dialogRef.afterClosed().subscribe(res => {
      if(res != null && res != undefined && res != ''){
        this._dialofRef.close(`${idOrder}|${res}`);
      }
    });
  }
}
