import { Component, OnInit } from '@angular/core';
import { ReviewPage } from '../review/review.page';
import { ModalController, MenuController, NavController, Events } from '@ionic/angular';
import { ActivatedRoute, Router } from '@angular/router';
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';
import { AlertController } from '@ionic/angular';

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

  fcontracts = [];
  fcontracts_dates = [];
  lang: boolean;

  constructor(
    private modalController: ModalController,
    private menu: MenuController,
    private navCtrl: NavController,
    private events: Events,
    private activatedRoute: ActivatedRoute,
    private router: Router,
    private alertController: AlertController,
    private authService: AuthService,
    private alertService: AlertService,
    private ichambaService: IchambaService,
    private translateService: TranslateService,
    private languageService: LanguageService,
    private env: EnvService,
  ) {
    this.events.subscribe('refreshccontracts', (data) => {
      this.getfcontracts();
    });
  }

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

  refresh(event) {
    this.ichambaService.getFinishedcontracts().subscribe(
      data => {
        this.fcontracts = data;
        for (var i of this.fcontracts) {
          if (this.languageService.getDefaultLanguage() == 'es') {
            this.fcontracts_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.fcontracts_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();
      });
  }

  getfcontracts() {
    this.ichambaService.getFinishedcontracts().subscribe(
      data => {
        this.fcontracts = data;
        for (var i of this.fcontracts) {
          if (this.languageService.getDefaultLanguage() == 'es') {
            this.fcontracts_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.fcontracts_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']);
      });
  }

  reviewContract(contract_id: String) {
    this.navCtrl.navigateForward(['/review/', contract_id]);
  }

  addExtra(contract_id: String, supplier: String) {
    this.navCtrl.navigateForward(['/extra/', { contract_id: contract_id, supplier: supplier }]);
  }

  reportContract(contract_id: String) {
    this.router.navigate(['/report/', contract_id]);
  }

}
