import { Injectable } from '@angular/core';
import {
  AngularFirestore,
  AngularFirestoreCollection
} from '@angular/fire/firestore';
import { take } from 'rxjs/operators';
import { Return } from './services/return';
import { ServiceService } from './service.service';
import { Router, ActivatedRoute } from '@angular/router';

@Injectable({
  providedIn: 'root'
})
export class ReturnService {
  loader: boolean;
  constructor(
    public afs: AngularFirestore,
    private router: Router,
    private service: ServiceService
  ) {
    this.loader = false;
  }

  returnCreate(data) {
    this.loader = true;
    this.afs
      .collection('orderReturn')
      .add(data)
      .then(docRef => {
        data.contactId = docRef.id;
        this.afs
          .collection('orderReturn')
          .doc(data.contactId)
          .update({ contactId: data.contactId })
          .then(() => {
            this.service
              .showToast('Thank you for your feedback!', 'top', 'my-toast', 500)
              .then(() => {
                this.loader = false;
                console.log('redirected');
                setTimeout(() => {
                  this.router.navigateByUrl('home');
                }, 500);
              })
              .catch(err => {
                this.loader = false;
              });
          });
      });
  }
}
