import { Component, OnInit } from '@angular/core';
import { ServicesService } from 'src/app/services.service';
import { NgxSpinnerService } from 'ngx-spinner';
import { Router } from '@angular/router';
import { AuthserviceService } from 'src/app/authservice.service';
import { Location } from '@angular/common';
import { environment } from 'src/environments/environment';
@Component({
  selector: 'app-support',
  templateUrl: './support.component.html',
  styleUrls: ['./support.component.scss'],
})
export class SupportComponent implements OnInit {
  Subject: any;
  Message: any;
  userDetails: any;
  constructor(
    public service: ServicesService,
    public spinner: NgxSpinnerService,
    public router: Router,
    public auth: AuthserviceService,
    private location: Location,
  ) {
    this.service.title = "<p class='HeaderUser'><span>Support</span></p>";
    this.service.headershow = true;
    this.service.SearchValue = true;
    var access = window.localStorage.getItem('accessitem');
    if (access) {
      var set = JSON.parse(<string>access);
      if (set) {
        this.service.access.CATaccess = set.CAT_access ? true : false;
        this.service.access.Knowaccess = set.KA_access ? true : false;
        this.service.access.Practiceaccess = set.PT_access ? true : false;
        this.service.access.livechat_access = set.livechat_access ? true : false;
      }
    }
  }

  ngOnInit(): void {
    this.userDetails = localStorage.getItem('userdetails');
    if (!this.userDetails) {
      this.router.navigate(['/login']);
    }
    this.userDetails = JSON.parse(this.userDetails)

  }
  async Submit() {
    if (this.Subject && this.Message) {
      this.spinner.show();

      let params = {
        // to: environment.SUPPORT_EMAIL,
        to: 'tech4biztestfirst@gmail.com',
        text: "From: " + this.userDetails.email + "<br><br>" + this.Message,
        subject: this.Subject
      }
      await this.auth.sendmail(params).then(async (result) => {
        this.service.addsupport(this.userDetails.id, this.Subject, this.Message).then(
          (data) => {
            if (data[0].status == 200) {
              this.service.ShowSuccess(
                'Thank you for contacting support. We will get in touch with you!'
              );
              this.Subject = '';
              this.Message = '';
              this.spinner.hide();
            } else {
              this.spinner.hide();
            }
          },
          () => {
            this.spinner.hide();

            this.service.ShowError('Encountered server error!');
          }
        );
      })

    } else {
      this.service.ShowError('Please enter details!');
    }
  }

  back() {
    this.location.back();
  }
}
