import {AfterViewInit, ChangeDetectorRef, Component, OnInit} from '@angular/core';
import {MatTableDataSource} from '@angular/material';
import {ApiService} from '../../../../../core/api/api.service';
import {ActivatedRoute, Router} from '@angular/router';
import {LayoutUtilsService, MessageType} from '../../../../../core/_base/crud';
import {TiposServicio} from '../tipos-servicio.model';

@Component({
  selector: 'kt-tipos-servicio-list',
  templateUrl: './tipos-servicio-list.component.html',
  styleUrls: ['./tipos-servicio-list.component.scss']
})
export class TiposServicioListComponent implements OnInit {

 //Search
 lastLength = 0;
 typingTimer;
 typingInterval = 700;

 displayedColumns: string[] = ['nombre', 'created_at', 'actions'];
 dataSourceTiposServicio: MatTableDataSource<TiposServicio>;

 //Loadings
 isLoading = true;
 showMessageWait = true;

 deleted_at = '0';

 //Paginator, Searcher
 searchText: string;
 sortBy = 'nombre';
 order = 'asc';
 page = '1';
 perPage = '10';
 total = 0;
 pages = [10, 25, 50, 100];
 titulo = 'Tipos Servicios';

 constructor(private api: ApiService,
       private router: Router,
       private layoutUtilsService: LayoutUtilsService,
       private activatedRoute: ActivatedRoute,
       private ref: ChangeDetectorRef) {
   this.dataSourceTiposServicio = new MatTableDataSource<TiposServicio>();
 }

 ngOnInit() {
 }

toggle(event){
	this.deleted_at = event.checked == false ? '0' : '1';
	this.getTiposServicio();
}

 getTiposServicio(){
   this.isLoading = true;
   this.api.tiposServicio.index(this.page, this.searchText, this.sortBy, this.order, this.perPage, null, this.deleted_at).subscribe(res =>{
     this.isLoading = false;
     this.showMessageWait = false;
     this.dataSourceTiposServicio.data = res.data;
     this.total = res.total;
     this.ref.detectChanges();
   }, ()=>{
     this.isLoading = false;
     this.showMessageWait = false;
   })
 }

 //Search
 filtrarBusqueda(filterValue: string) {
   if (filterValue.length != 0) {
     this.searchText = filterValue.trim().toLowerCase();
     this.getTiposServicio();
   } else if (this.lastLength != 0) {
     this.searchText = '';
     this.getTiposServicio();
   }

   this.lastLength = filterValue.length;
 }

 onKeyUp(ev) {
   clearTimeout(this.typingTimer);
   this.typingTimer = setTimeout(() => {
     this.filtrarBusqueda(ev.target.value)
   }, this.typingInterval);
 }

 onKeyDown() {
   this.showMessageWait = true;
   this.dataSourceTiposServicio.data = [];
   clearTimeout(this.typingTimer);
 }//Fin search

 editTiposServicio(id) {
   this.router.navigate(['../tipos-servicio/edit/', id], {relativeTo: this.activatedRoute})
 }

 deleteTiposServicio(tiposServicio) {
   let title = (!tiposServicio.deleted_at) ? 'Eliminar tipo de servicio' : 'Activar tipo de servicio' ;
   let name = (tiposServicio.nombre)? tiposServicio.nombre : '';
   let description = (!tiposServicio.deleted_at) ? '¿Está seguro de eliminar el tipo de servicio ' + name + '?' : '¿Está seguro de activar el tipo de servicio ' + name + '?';
   let waitDesciption = (!tiposServicio.deleted_at) ? 'Eliminando tipo de servicio...' : 'Activando tipo de servicio...';
   let deleteMessage = (!tiposServicio.deleted_at) ? `El tipo de servicio ha sido eliminado con éxito` : `El tipo de servicio ha sido activado con éxito`;
   let errorMessage = (!tiposServicio.deleted_at) ? `El tipo de servicio no se ha podido eliminar` : `El tipo de servicio no se ha podido activar`;
   let btnText = (!tiposServicio.deleted_at) ? 'Eliminar' : 'Activar';

	   let dialogRef = this.layoutUtilsService.deleteElement(title, description, waitDesciption, btnText);
   dialogRef.afterClosed().subscribe(res => {
     if (!res) {
       return;
     }

   let reactivate = (tiposServicio.deleted_at != null)

     this.api.tiposServicio.delete(tiposServicio.id, reactivate).subscribe(() => {
       this.getTiposServicio();
       this.layoutUtilsService.showActionNotification(deleteMessage, MessageType.Delete);
     }, () => {
       this.layoutUtilsService.showActionNotification(errorMessage, MessageType.Delete);
     });
   });

 }

 sort(event) {
   this.order = event.direction;
   this.sortBy = event.active;
   this.getTiposServicio();
 }

 paginator(event) {
   let nextPage = event.pageIndex + 1;
   this.page = nextPage.toString();
   this.perPage = event.pageSize.toString();
   this.getTiposServicio();
 }

 ngAfterViewInit(): void {
   this.getTiposServicio();
 }

}
