import { Component, OnInit, OnDestroy } from '@angular/core';
import { Router } from '@angular/router';

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

import { AngularFireAuth } from 'angularfire2/auth';
import { AuthService } from '../../../services/auth/auth.service';
import { OpCommercialService } from '../../../services/opCommercial/op-commercial.service';

import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-list-carte-envoyer',
  templateUrl: './list-carte-envoyer.component.html',
  styleUrls: ['./list-carte-envoyer.component.scss'],
  providers: [AuthService, OpCommercialService]
})
export class ListCarteEnvoyerComponent implements OnInit, OnDestroy {
  opComm: OperationCommerciale;
  opCommList: FirebaseListObservable<any[]>;
  carteParList: FirebaseListObservable<any[]>;
  hideme = {};
  constructor(public afAuth: AngularFireAuth, public afDb: AngularFireDatabase,
              private router: Router, private route: ActivatedRoute) {
    this.hideme = {}; // init is required

    this.route.params.subscribe(params => {
      this.carteParList = this.afDb.list('/CarteParrainage/', {
          query: {
            orderByChild: 'operationCommercialCarte/uid',
            equalTo: params['idOp']
          }
        });
      this.opCommList = this.afDb.list('/OperationCommercial/', {
        query: {
          orderByChild: 'uid',
          equalTo: params['idOp']
        }
      });
      this.opCommList.forEach(op => {
        op.forEach(element => {
          this.opComm = element;
        });
      });
    });
  }

  goToOpComAdmin(){
    this.router.navigate(['/listOpCommercialAdmin']);
  }

  ngOnInit() {
  }
  ngOnDestroy() {
  }
}
