import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { SharedService } from './shared.service';
import { ToastrService } from 'ngx-toastr';
import { Location } from '@angular/common';


@Injectable({
  providedIn: 'root'
})
export class PagesService {

  constructor(
    private http: HttpClient,
    private auth: SharedService,
    private toster: ToastrService,
    private location: Location,

  ) { }

  getListData(currentPage: number ,perPage: number, type: any, pageName){
    if(pageName == 'system-admins'){
    console.log(this.auth.pathApi + '/users_list/' + currentPage+ '/' + '/' + perPage+ '/' + type );
    return this.http.get(this.auth.pathApi + '/users_list/' + currentPage+ '/'  + perPage+ '/' + type );
    }else   if(pageName == 'customers'){
      console.log(this.auth.pathApi + '/companies_list/' + currentPage+ '/' + '/' + perPage+ '/' + type );
      return this.http.get(this.auth.pathApi + '/companies_list/' + currentPage+ '/'  + perPage+ '/' + type );
      }

  }
  addItem(data, pageName){
    if(pageName == 'system-admins')
    {
      return this.http.post(this.auth.pathApi + '/register_new_user', data);
    }
  }
   //edit event
   editItem(editId,dataEdit,pageName){
    const editData = dataEdit;
    console.log(editId);
    editData['id'] = editId;
    console.log(dataEdit);
    if(pageName == 'system-admins'){
      return this.http.post(this.auth.pathApi + '/edit_current_user' ,editData);
    }
  }
  getItemData(pageId: number, pageName: String)
   {
      console.log('pageId', pageId);
      if(pageName == 'system-admins')
      return  this.http.get(this.auth.pathApi + '/get_user_by_id/' + pageId);
      
   }
    //previous back locarion
    perviousLocation() {
      this.location.back();
    }

      //delete event
      deleteItem(dataIds, pageName) {
        console.log(dataIds);
        console.log(pageName);
        console.log(this.auth.pathApi+ '/delete_current_users');
        
        if(pageName == 'system-admins') {
          return this.http.post(this.auth.pathApi+ '/delete_current_companies', {'companies_id':dataIds});
        }else if (pageName == 'customers' ) {
          
        }
        
      }
   
}
