import { Component, OnInit } from '@angular/core';
import { ModalController, MenuController, NavController, Events } from '@ionic/angular';
import { EnvService } from 'src/app/services/env.service';
import { AuthService } from 'src/app/services/auth.service';
import { IchambaService } from 'src/app/services/ichamba.service';
import { AlertService } from 'src/app/services/alert.service';

@Component({
  selector: 'app-ended',
  templateUrl: './ended.page.html',
  styleUrls: ['./ended.page.scss'],
})
export class EndedPage implements OnInit {

  postulations = [];
  postulations_dates = [];

  constructor(
    private modalController: ModalController,
    private menu: MenuController,
    private navCtrl: NavController,
    private events: Events,
    private authService: AuthService,
    private alertService: AlertService,
    private ichambaService: IchambaService,
    private env: EnvService,
  ) {
    this.events.subscribe('refreshpostulations', (data) => {
      this.getfinishedpostulations();
    });
  }

  ngOnInit() {
    this.getfinishedpostulations();
  }

  refresh(event) {
    this.ichambaService.getFinishedPostulation().subscribe(
      data => {
        this.postulations = data;
        for (var i of this.postulations) {
          this.postulations_dates.push(new Date((new Date(i.date).toLocaleString('en-US') + ' UTC').replace("," ,"")).toLocaleDateString('es-US', { weekday: 'long', year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute:'numeric', timeZoneName: 'long'}));
        }
        event.target.complete();
      }, error => {
        this.alertService.presentToast("Por favor contacte a soporte técnico, Estatus:" + error['status']);
        event.target.complete();
      });
  }

  getfinishedpostulations() {
    this.ichambaService.getFinishedPostulation().subscribe(
      data => {
        this.postulations = data;
        for (var i of this.postulations) {
          this.postulations_dates.push(new Date((new Date(i.date).toLocaleString('en-US') + ' UTC').replace("," ,"")).toLocaleDateString('es-US', { weekday: 'long', year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute:'numeric', timeZoneName: 'long'}));
        }
      }, error => {
        this.alertService.presentToast("Por favor contacte a soporte técnico, Estatus:" + error['status']);
      });
  }

}
