import {
  ViewportScroller
} from '@angular/common';
import { Component, HostListener, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { NgxSpinnerService } from 'ngx-spinner';
import Swal from 'sweetalert2';
import { GlobalService } from '../../shared/services/common/global.service';
declare let $: any;

@Component({
  selector: 'app-footer',
  templateUrl: './footer.component.html',
  styleUrls: ['./footer.component.scss']
})
export class FooterComponent implements OnInit {
  pageYoffset = 0;
  isActive = false;

  @HostListener('window:scroll') onScroll(event: any) {
    this.pageYoffset = window.pageYOffset;
  }
  @HostListener('window:scroll', [])
  onWindowScroll() {
    const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
    this.isActive = scrollTop > 500;
  }
  public currentDate: Date = new Date();
  public basicDetailForm: FormGroup;
  public isNavToggle: boolean = false;
  constructor(
    private formBuilder: FormBuilder,
    private globalService: GlobalService,
    private spinnerService: NgxSpinnerService,
    private scroll: ViewportScroller) {

    this.basicDetailForm = formBuilder.group({
      'email': [null, [Validators.required, Validators.pattern("[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$")]],
    })
  }

  ngOnInit(): void {
    //   $('.go-top').on('click', function () {
    //     $('html, body').animate({
    //       scrollTop: '0'
    //     }, 500);
    //   });
    // }
  }

  public async submitForm(form: FormGroup) {
    debugger;
    if (form.valid) {
      try {
        this.spinnerService.show();
        let res = await this.globalService.insertNewsLetterSubscription(this.basicDetailForm.value.email);
        if (res && res.status == 200) {
          //this.toastrService.success(res.message);
          Swal.fire({
            icon: 'success',
            text: res.message,
            confirmButtonColor: '#0162e8'
          })
          this.basicDetailForm.reset();
        } else {
          //this.toastrService.error(res.message, "Error");
          Swal.fire({
            icon: 'error',
            text: res.message,
            confirmButtonColor: '#0162e8'
          })
        }
        this.spinnerService.hide();
      } catch (error: any) {
        this.spinnerService.hide();
        let err = error;
        if (error && error.error && error.error.message) {
          err = error.error.message
        }
        //this.toastrService.error(err, "Error");
        Swal.fire({
          icon: 'error',
          text: err,
          confirmButtonColor: '#0162e8'
        })
      }
    } else {
      Object.keys(form.controls).forEach(field => {  //{2}
        const control = form.get(field);             //{3}
        if (control instanceof FormControl) {             //{4}
          control.markAsTouched({ onlySelf: true });
        }
      });
    }
  }

  scrollToTop() {
    this.scroll.scrollToPosition([0, 0]);
  }

  public toggleDropDown() {
    this.isNavToggle = !this.isNavToggle
  }
}
