import { Component, OnInit } from '@angular/core';
import { DashboardService } from './../../../shared/dashboard.service';
import { HttpClient } from '@angular/common/http';
import { UserService } from './../../../shared/user.service';
import { ActivatedRoute, Router, Params } from '@angular/router';
import { NgxSpinnerService } from 'ngx-spinner';
import { AuthServiceService } from './../../../shared/auth-service.service';
import { ToastrService } from 'ngx-toastr';
import { Modal } from 'ngx-modialog/plugins/bootstrap';

@Component({
  selector: 'app-print-card-trainee-list',
  templateUrl: './print-card-trainee-list.component.html',
  styleUrls: ['./print-card-trainee-list.component.css']
})

export class PrintCardTraineeListComponent implements OnInit {

  constructor(private route: ActivatedRoute,    
    private router: Router, 
    private userSer: UserService,
    private http: HttpClient,
    private toastr: ToastrService,
    private modal: Modal,
    private dashBoardService: DashboardService,
    private spinner: NgxSpinnerService,
    private authSer: AuthServiceService) { }

    pageId: number;
    dataList = [];
    dataListIds = [];
    count: number;
    perPagePagenation: number;
    currentPage:number = 1;
    filtterStatus = '';
    selectedAll: any;
    userLoginId:number;
    serviceId:number;
    dataTableNumber: number = 5;
    serviceName: string = '';
    pages = [];

  ngOnInit() {

    this.spinner.show();

    //init the values of permision boolean
    this.route.params.subscribe(
      (params: Params) => {
        this.pageId = params['printTraineeId'];
      }
    );

    //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.route.parent.params.subscribe(
      (params:Params) => {
          this.userLoginId = params['userID'];
          this.serviceId = params['serviceID'];
  
          this.userSer.getPagesPermetiotns(this.userLoginId, this.serviceId).subscribe(
            (responce) => {
              console.log(responce);
              this.pages = responce['pages'];
              for(let i = 0; i< this.pages.length; i++) {
                if(this.pages[i].id == 37) {
                  if(this.pages[i]['permissions'][0].name == 'print_the_card_to_the_trainee') {
                    this.authSer.showAddBtn = true;
                    this.authSer.showEditBtn = true;
                    this.authSer.showDeleteBtn = true;
                  } else {
                    console.log('no permissions');
                  }
                } else {
                  console.log('no events');
                }
              }
            },
            (error) => {console.log(error)}
          );
        }
  
      );
    

          //get list data 
        this.dashBoardService.getListData(this.pageId, this.currentPage ,this.dataTableNumber).subscribe(
          (responce) => {
            console.log('rescponce dataaaa', responce);
            this.dataList = responce['requests'];
            if(this.dataList.length == 0) {
              this.toastr.warning('القائمه فارغه من الطلبات ');
            }
            this.count = responce['count'];
            this.perPagePagenation = responce['per_page'];
            console.log('evennnnts', this.dataList);
            if(this.dataList.length > 0) {
              this.spinner.hide();
            }
          },
          (error) => {
            console.log(error);
            this.spinner.hide();
          }
        );

   
    
  }

  //filtter function
  filtterFunc(data) {
    this.dataList = [];
    console.log(data.target.value);
    const dataSearch = data.target.value;     
    this.currentPage = 1;
    this.dashBoardService.getDataUSerSearchBar(dataSearch, this.pageId, this.currentPage, this.dataTableNumber).subscribe(
      (responce) => {
        console.log(responce);
        this.dataList = responce['requests'];
        this.count = responce['count'];
        this.perPagePagenation = responce['per_page'];
        console.log('filtter count', this.count);
        console.log('filtter perPagePAgenation', this.perPagePagenation);
      },
      (error) => {
        console.log(error)
      }
    );
  };

  //change page 
  onPageChange(pagenationNumber) {
    this.spinner.show();
    this.currentPage = pagenationNumber;
    this.dataList = [];
    //console.log(pagenationNumber);
    //console.log(this.pageId);
    this.dashBoardService.getListData(this.pageId, pagenationNumber, this.dataTableNumber).subscribe(
      (responce) => {
        console.log(responce);
        this.dataList = responce['requests'];
        this.count = responce['count'];
        this.perPagePagenation = responce['per_page'];
        console.log(this.dataList);
        this.spinner.hide();
      },
      (error) => {
        console.log(error);
        this.spinner.hide();
      }
    );
  }

  //determine the list count from select element 
  onGetValue(event) {
    this.spinner.show();
    this.dataList = [];
    this.dataTableNumber = event.target.value;
    this.dashBoardService.getListData(this.pageId, this.currentPage, this.dataTableNumber).subscribe(
      (responce) => {
        console.log(responce);
        this.dataList = responce['requests'];
        this.count = responce['count'];
        this.perPagePagenation = responce['per_page'];
        this.spinner.hide();
      },
      (error) => {
        console.log(error);
        this.spinner.hide();
      }
    );
  };



  //edit function
  onEdit(editPageID) {
    this.router.navigate(['service/' + this.userLoginId + '/' + this.serviceId + '/' + 'printCardTrainee/edit/' + editPageID]);
  }

}
