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,
    ) { }




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

      company_name : '',
      phone : '',
      commercial_number : '',
      industrial : '',
      website_address :'',
      address : '',
      total_users_request : '',
      register_date : '',
      expire_date : '',
      logo_photo : '',
      company_admins : [
         {  
            name : '',
            email : '',
            password : '',
            password_confirmation : ''
   
         }],
      company_branches : [
         {
            branch_name : '',
            branch_users : [
               {
                  name: '',
                  email : '',
                  type : '',
                  position : '',
                  phone : '',
                  hire_date: '',
                  is_active: null,
                  password : '',
                  password_confirmation : ''
               }]
         }]
    }

  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});
    localStorage.setItem('company_registrtion', JSON.stringify(this.company_registrtion));
  }

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