import { ToastrService } from 'ngx-toastr';
import { AuthServiceService } from './../../shared/auth-service.service';
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { NgxSpinnerService } from 'ngx-spinner';
import { Router, ActivatedRoute, Params } from '@angular/router';

@Component({
  selector: 'app-all-requests',
  templateUrl: './all-requests.component.html',
  styleUrls: ['./all-requests.component.css']
})
export class AllRequestsComponent implements OnInit {

  constructor(private http: HttpClient,
    private spinner: NgxSpinnerService, 
    private router: Router,
    private toastr: ToastrService,
    private route: ActivatedRoute,
    private authSer: AuthServiceService) { }

    requests = [];
    count: number;
    visitorId: number;
  

  ngOnInit() {
    this.spinner.show();


    this.route.params.subscribe(
      (params: Params) => {
        this.visitorId = +params['visitorID'];
      }
    );

    //init the values of permision boolean
    this.authSer.showAddBtn = false;
    this.authSer.showDeleteBtn = false;
    this.authSer.showEditBtn = false;
    //show / hide notification search in header
    this.authSer.notificationLogin = true;
    this.authSer.showSearchHeader = false;
    this.authSer.showHeaderLogin = false;
    this.authSer.showHeaderDashBoard = true;
    this.authSer.showDashboardHeader = true;
    this.authSer.internalHeader = false;

    this.http.get(this.authSer.pathApi + '/get_my_requests').subscribe(
      (responce) => {
        console.log(responce);
        this.requests = responce['requests'];
        if(this.requests.length == 0) {
          this.toastr.warning('قائمه الطلبات فارغه ');
        }
        this.spinner.hide();
      },
      (error) => {
        console.log(error);
      }
    )
  }

  onEdit(requestData) {
    console.log(requestData);
    console.log('editRequest/' + this.visitorId + '/' + requestData.id);
    localStorage.setItem('requestStatus', requestData.status);
    this.router.navigate(['editRequest/' + this.visitorId + '/' + requestData.id]);
  }



  


}
