import { Component, OnInit, OnDestroy } from '@angular/core';
import { Observable } from 'rxjs/Observable';

import { OperationCommerciale } from '../../../../models/OperationCommerciale';
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';

import * as firebase from 'firebase/app';
import { AngularFireAuth } from 'angularfire2/auth';
import { AuthService } from '../../../services/auth/auth.service';
import { OpCommercialService } from '../../../services/opCommercial/op-commercial.service';
import { Commercant } from '../../../../models/Commercant';
import { ToastrService } from '../../../typescripts/angular-bootstrap-md/pro';

@Component({
  selector: 'app-demande-op-commercial',
  templateUrl: './demande-op-commercial.component.html',
  styleUrls: ['./demande-op-commercial.component.scss'],
  providers: [AuthService, OpCommercialService]
})
export class DemandeOpCommercialComponent implements OnInit, OnDestroy  {
  opComm: OperationCommerciale;
  user: Observable<firebase.User>;
  opCommDataEnCours: FirebaseListObservable<any[]>;
  opCommDataRejeter: FirebaseListObservable<any[]>;
  profileData: FirebaseListObservable<any[]>;
  commercant: Commercant;
  databaseRef: any;
  tarifXJCParr: number;
  constructor(public afAuth: AngularFireAuth, public afDb: AngularFireDatabase,
              private toastrService: ToastrService) {
    this.opComm = new OperationCommerciale();
    this.databaseRef = firebase.database().ref();
    this.user = afAuth.authState;
    this.commercant = new Commercant();
    this.tarifXJCParr = 0;
    this.user.subscribe(
      (auth) => {
        if (auth) {

          this.opCommDataEnCours = this.afDb.list('/OperationCommercial/',{
            query: {
              orderByChild: 'validationXJC',
              equalTo: 'En Cours'
            }
          }).map((array) => array.reverse()) as FirebaseListObservable<any[]>;

          this.opCommDataEnCours.$ref
            .orderByChild('validationXJC')
            .equalTo('En Cours')
            .on('child_added', (child) => {
              this.toastrService.info("Nouvelle demande d'opération commerciale" );
            });


          this.opCommDataRejeter = this.afDb.list('/OperationCommercial/',{
            query: {
              orderByChild: 'validationXJC',
              equalTo: 'Rejeté'
            }
          }).map((array) => array.reverse()) as FirebaseListObservable<any[]>;
        }
      }
    );
  }
  rejeter(opRejeter: OperationCommerciale) {

    const opTarifPRef = this.databaseRef.child('OperationCommercial').child(opRejeter.uid).child('tarifXJCParrain');
    opTarifPRef.set('');
    const opTarifFRef = this.databaseRef.child('OperationCommercial').child(opRejeter.uid).child('tarifXJCFilleul');
    opTarifFRef.set('');
    const opEnvRef = this.databaseRef.child('OperationCommercial').child(opRejeter.uid).child('validationXJC');
    opEnvRef.set('Rejeté');
    this.tarifXJCParr = 0;
    this.toastrService.success("L'opération commerciale : " + opRejeter.name + ' a été rejetée ' );

  }

  valider(opValider: OperationCommerciale) {

    const opTarifRef = this.databaseRef.child('OperationCommercial').child(opValider.uid).child('tarifXJCParrain');
    opTarifRef.set(this.tarifXJCParr.toString());
    const opTarifFRef = this.databaseRef.child('OperationCommercial').child(opValider.uid).child('tarifXJCFilleul');
    opTarifFRef.set(this.tarifXJCParr.toString());
    const opEnvRef = this.databaseRef.child('OperationCommercial').child(opValider.uid).child('validationXJC');
    opEnvRef.set('Validé');

    this.tarifXJCParr = 0;
    this.toastrService.success("L'opération commerciale : " + opValider.name + ' a été validée ' );


  }

  ngOnInit() {
    try {
      this.user.subscribe(
        (auth) => {
          if (auth) {
            console.log(auth.uid);
          } else {
            // this.router.navigate(['/']);
          }
        });
    } catch (e) {
      // No content response..
      console.log(e);
      // this.router.navigate(['/']);
    }
  }
  ngOnDestroy() {
    this.opCommDataEnCours = null;
    this.opCommDataRejeter = null;
  }

}
