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

import { LocalDataSource } from 'ng2-smart-table';
import { SmartTableData } from '../../../@core/data/smart-table';


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

  source: LocalDataSource = new LocalDataSource();


  constructor(
    private auth: SharedService,
    private http: HttpClient,
    private route: ActivatedRoute,
    private router: Router,
    private toaster: ToastrService,
    private pageser: PagesService,
    private service: SmartTableData,
    ) { }



    // settings = {
      
    //   edit: {
    //     editButtonContent: '<i class="nb-edit"></i>',
    //     saveButtonContent: '<i class="nb-checkmark"></i>',
    //     cancelButtonContent: '<i class="nb-close"></i>',
    //   },
    //   delete: {
    //     deleteButtonContent: '<i class="nb-trash"></i>',
    //     confirmDelete: true,
    //   },
    //   selectMode: 'multi',
    //   actions: {
    //     add: false,
    //     delete: false,
    //     select: true,
    //     },
    //   columns: {
    //     name: {
    //       title: 'Name',
    //       type: 'string',
    //     },
    //     industrial: {
    //       title: 'industrial',
    //       type: 'string',
    //     },
    //     website_address: {
    //       title: 'website',
    //       type: 'string',
    //     },
    //     phone: {
    //       title: 'Phone',
    //       type: 'string',
    //     },
    //   },
    // };

    dataList = [];
    dataListIds= [];
    selectListIds = [];
    dataTableNumber: number = 5;
    currentPage:number = 1;
    count: number;
    perPagePagenation: number;
    selectedAll: any;

  ngOnInit() {
    this.pageser.getListData(this.currentPage, this.dataTableNumber, 'all', 'customers')
      .subscribe(
        res => {
          console.log(res);
          this.dataList = res[('companies')];
          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',  'customers')
    .subscribe(
      res => {
        console.log(res);
        this.dataList = res[('companies')];
        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' ,  'customers')
          .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,'customers ').subscribe(
      res => {
        console.log(res);
        this.toaster.success('Deleted');
        this.dataList = [];
        this.pageser.getListData(this.currentPage, this.dataTableNumber,'all','customers' ).subscribe(
          res => {

            console.log(res);
            this.dataList = res['companies'];
            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(['../register'], {relativeTo: this.route});
  }

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