import { Component, OnInit } from '@angular/core';
import { ModalController, MenuController, NavController } from '@ionic/angular';
import { EventService } from '../../../services/event.service';
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 { TranslateService } from '@ngx-translate/core';
import { LanguageService } from 'src/app/services/language.service';
import { AlertService } from 'src/app/services/alert.service';

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

  pcontracts = [];
  pcontracts_dates = [];
  lang: boolean;

  constructor(
    private modalController: ModalController,
    private menu: MenuController,
    private navCtrl: NavController,
    private events: EventService,
    private authService: AuthService,
    private alertService: AlertService,
    private ichambaService: IchambaService,
    private translateService: TranslateService,
    private languageService: LanguageService,
    private env: EnvService,
  ) {
    this.events.subscribe('refreshpcontracts', (data) => {
      this.getpcontracts();
    });
  }

  ngOnInit() {
    this.getpcontracts();
    if (this.languageService.getDefaultLanguage() == 'es') {
      this.lang = true;
    } else {
      this.lang = false
    }
  }

  refresh (event) {
    this.ichambaService.getPendingcontracts().subscribe(
      data => {
        this.pcontracts = data;
        for (var i of this.pcontracts) {
          if (this.languageService.getDefaultLanguage() == 'es') {
            this.pcontracts_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'}));
          } else {
            this.pcontracts_dates.push(new Date((new Date(i.date).toLocaleString('en-US') + ' UTC').replace("," ,"")).toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute:'numeric', timeZoneName: 'long'}));
          }
        }
        event.target.complete();
      }, error => {
        this.alertService.presentToast(this.translateService.instant('alerts.error') + error['status']);
        event.target.complete();
      });
  }

  getpcontracts() {
    this.ichambaService.getPendingcontracts().subscribe(
      data => {
        this.pcontracts = data;
        for (var i of this.pcontracts) {
          if (this.languageService.getDefaultLanguage() == 'es') {
            this.pcontracts_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'}));
          } else {
            this.pcontracts_dates.push(new Date((new Date(i.date).toLocaleString('en-US') + ' UTC').replace("," ,"")).toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute:'numeric', timeZoneName: 'long'}));
          }
        }
      }, error => {
        this.alertService.presentToast(this.translateService.instant('alerts.error') + error['status']);
      });
  }

  viewsuppliers(postulation_id: String) {
    this.navCtrl.navigateForward(['/viewsuppliers/', postulation_id]);
  }

}
