import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ActivatedRoute, Router } from '@angular/router';
import { SharedService } from '../../../shared/shared.service';
import { ToastrService } from 'ngx-toastr';
import { PagesService } from '../../../shared/pages.service';

@Component({
  selector: 'ngx-system-admins-list',
  templateUrl: './system-admins-list.component.html',
  styleUrls: ['./system-admins-list.component.scss']
})
export class SystemAdminsListComponent implements OnInit {

  constructor(
    private http: HttpClient,
    private route: ActivatedRoute,
    private router: Router,
    private auth: SharedService,
    private toaster: ToastrService,
    private pageSer: PagesService,
  ) { }
  dataList = [];
  dataListIds= [];
  selectListIds = [];
  dataTableNumber: number = 5;
  currentPage:number = 1;
  count: number;
  perPagePagenation: number;
  selectedAll: any;

  ngOnInit() {

    this.pageSer.getListData(this.currentPage, this.dataTableNumber, 'all', 'system-admins')
    .subscribe(
      res => {
        console.log(res);
        this.dataList = res[('users')];
        console.log(this.dataList);
        this.count = res[('count')];
        console.log(this.count);
        this.perPagePagenation = res[('per_page')];
      },
      err => {
        console.log(err);
        
      }
    );
  }

  onGetValue(event){
    this.dataTableNumber =  event.target.value;
    console.log(this.dataTableNumber);
    this.dataList = [];
    this.pageSer.getListData(this.currentPage, this.dataTableNumber, 'all', 'system-admins')
      .subscribe(
        res => {
          console.log(res);
          this.dataList = res[('users')];
          console.log(this.dataList);
          this.count = res[('count')];
          this.perPagePagenation = res[('per_page')];
          
        },
        err => {
          console.log(err);
          
        }
      );
    
  }

  onPageChange(pagenationNumber){
    this.currentPage = pagenationNumber;
    this.pageSer.getListData(pagenationNumber,this.dataTableNumber , 'all', 'system-admins' )
          .subscribe(
            res => {
              console.log(res);
              this.dataList = [];
              this.dataList = res[('users')];
              this.count = res[('count')];
              this.perPagePagenation = res[('per_page')];
            },
            err => {
              console.log(err);
            }
          );
  }
  //make all checkbox of user checked 
  selectAll() {
    console.log('selected alllllll');
    for(let i = 0; i < this.dataList.length; i++){
      this.dataList[i].selected = this.selectedAll;
    }
  }
  checkIfAllSelected() {
    this.selectedAll = this.dataList.every(function(item:any) {
      return item.selected == true;
    });
  }

  onDelete(){
    this.dataListIds = [];
    for (let i = 0; i < this.dataList.length; i++) {
      if (this.dataList[i].selected == true) {
        this.dataListIds.push(this.dataList[i].id);
      }      
    }

    console.log(this.dataListIds);
    if(this.dataListIds.length > 0){
    this.pageSer.deleteItem(this.dataListIds, 'system-admins').subscribe(
      res => {
        console.log(res);
        this.toaster.success('Deleted');
        this.dataList = [];
        this.pageSer.getListData(this.currentPage, this.dataTableNumber,'all','system-admins' ).subscribe(
          res => {

            console.log(res);
            this.dataList = res['users'];
            this.count = res['count'];
            this.perPagePagenation = res['per_page'];
           
          },
          err => {
            console.log(err);
;          }
        );
      },
      err => {
        console.log(err);
        this.toaster.error('sorry something went wrong!')
      }
    );
  }else {
    this.toaster.warning('No thing to delete')
  }
  }
  
 
  onAdding(){
    this.router.navigate(['../add'], {relativeTo: this.route});
  }

  onEdit(editId: number) {
  this.router.navigate(['../edit/' + editId], {relativeTo: this.route});
  console.log(editId);
  
}


}
