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

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

  postulation_id: string | null = null;
  pcontractssuppliers: any[] = [];
  pcontractssuppliers_dates: any[] = [];
  pcontractssuppliers_certified: any[] = [];
  pcontractssuppliers_category: any[] = [];
  pcontractssuppliers_en_category: any[] = [];
  pcontractssuppliers_address: any[] = [];
  pcontractssuppliers_amount: any[] = [];
  pcontractssuppliers_fee: any[] = [];
  lang: boolean = false;

  constructor(
    private modalController: ModalController,
    private menu: MenuController,
    private navCtrl: NavController,
    private events: EventService,
    private activatedRoute: ActivatedRoute,
    private router: Router,
    private authService: AuthService,
    private alertService: AlertService,
    private ichambaService: IchambaService,
    private translateService: TranslateService,
    private languageService: LanguageService,
    private env: EnvService,
  ) { }

  ngOnInit() {
    this.postulation_id = this.activatedRoute.snapshot.paramMap.get('postulation_id');
    this.getPostulants(this.postulation_id ?? '');
    if (this.languageService.getDefaultLanguage() == 'es') {
      this.lang = true;
    } else {
      this.lang = false
    }
  }

  refresh(event: any) {
    this.ichambaService.getPostulants(this.postulation_id ?? '').subscribe(
      (data: any) => {
        this.pcontractssuppliers = data;
        for (var i of this.pcontractssuppliers) {
          if (this.languageService.getDefaultLanguage() == 'es') {
            this.pcontractssuppliers_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'}));
            this.pcontractssuppliers_certified.push(i.membership == 1 ? 'Certificado' : '');
          } else {
            this.pcontractssuppliers_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'}));
            this.pcontractssuppliers_certified.push(i.membership == 1 ? 'Certified' : '');
          }
          this.pcontractssuppliers_category.push(i.category);
          this.pcontractssuppliers_en_category.push(i.en_category);
          this.pcontractssuppliers_address.push(i.address);
          this.pcontractssuppliers_amount.push(i.amount);
          this.pcontractssuppliers_fee.push(i.fee);
        }
        event.target.complete();
      }, error => {
        this.alertService.presentToast("Por favor contacte a soporte técnico, Estatus:" + error['status']);
        event.target.complete();
      });
  }

  getPostulants(postulation_id: String) {
    this.ichambaService.getPostulants(postulation_id).subscribe(
      (data: any) => {
        this.pcontractssuppliers = data;
        for (var i of this.pcontractssuppliers) {
          if (this.languageService.getDefaultLanguage() == 'es') {
            this.pcontractssuppliers_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'}));
            this.pcontractssuppliers_certified.push(i.membership == 1 ? 'Certificado' : '');
          } else {
            this.pcontractssuppliers_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'}));
            this.pcontractssuppliers_certified.push(i.membership == 1 ? 'Certified' : '');
          }
          this.pcontractssuppliers_category.push(i.category);
          this.pcontractssuppliers_en_category.push(i.en_category);
          this.pcontractssuppliers_address.push(i.address);
          this.pcontractssuppliers_amount.push(i.amount);
          this.pcontractssuppliers_fee.push(i.fee);
        }
      }, error => {
        this.alertService.presentToast("Por favor contacte a soporte técnico, Estatus:" + error['status']);
      });
  }

  hiresupplier(supplier_id: String, postulation_id: String, supplier: String, fee: Number) {
    this.router.navigate(['/hire', {supplier: supplier, supplier_id: supplier_id, postulation_id: postulation_id, fee: fee}]);
  }

}
