import { HttpErrorResponse } from '@angular/common/http';
import { AfterViewInit, Component, OnInit, ViewChild } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { lastValueFrom } from 'rxjs';
import { GetSymptom, ResponseDataGet } from 'src/app/interfaces/failure-analysis/symptom.interface';
import { ResponseData } from 'src/app/interfaces/response-data';
import { SymptomService } from 'src/app/services/failure-analysis/symptom.service';
import { ResourcesService } from 'src/app/services/resources/resources.service';
import { AlertComponent } from '../../resources/dialogs/alert/alert.component';
import { SymptomFormComponent } from './symptom-form/symptom-form.component';

@Component({
  selector: 'app-symptom',
  templateUrl: './symptom.component.html',
  styleUrls: ['./symptom.component.css']
})
export class SymptomComponent implements OnInit, AfterViewInit {

  public isLoading: boolean;
  public isLoadingForm: boolean;
  public btnSmall: boolean;

  public txtBuscador: string;

  public displayedColumns: Array<string>;
  public dataSource: MatTableDataSource<GetSymptom>;

  @ViewChild(MatPaginator) paginator!: MatPaginator;
  @ViewChild(MatSort) sort!: MatSort;

  constructor(
    private _symptomService: SymptomService,
    public resourcesService: ResourcesService,
    private _dialog: MatDialog,
  ) {
    this.isLoading = false;
    this.isLoadingForm = false;
    this.btnSmall = false;

    this.txtBuscador = '';

    this.displayedColumns = [
      'SINT_COEQ',
      'LIFA_NOFA',
      'SINT_CAUS',
      'SINT_CLAS',
      'SINT_LIVA',
      'SINT_USRE',
      'SINT_FERE',
      'SINT_ESTA',
      'ACTIONS'
    ]
    this.dataSource = new MatTableDataSource<GetSymptom>();
  }

  ngOnInit(): void {
    this.getSymptoms();
  }

  ngAfterViewInit(): void {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
  }

  public async getSymptoms() {
    this.isLoading = true;
    await lastValueFrom(this._symptomService.getSymptom()).then(
      (responseData: ResponseDataGet) => {
        this.dataSource.data = responseData.response;
      }, (error: HttpErrorResponse) => this.resourcesService.checkErrors(error)
    );
    this.isLoading = false;
  }

  public openDialog( typeForm: string, element?:GetSymptom ) {
    if ( typeForm === 'REG' || typeForm === 'UPD' ) {
      this._dialog.open( SymptomFormComponent, {
        width: '800px',
        disableClose: true,
        data: { action: typeForm, dataElement: element },
      }).afterClosed().subscribe((result) => {
        if (result === 'EXITO') {
          this.getSymptoms();
        }
      });
    } else if ( typeForm === 'DEL' ) {
      this._dialog.open( AlertComponent, {
        data: {
          title: 'Eliminación de Síntoma',
          description: '¿Está seguro que desea eliminar el síntoma?',
          icon: 'warning',
        },
        disableClose: true,
      }).afterClosed().subscribe((result) => {
        if (result) {
          this.deleteSymptom(element!.NUMERO_SINTOMA);
        }
      });
    }
  }

  private async deleteSymptom(numeroSintoma: string) {
    await lastValueFrom(this._symptomService.deteleProvSymptom({
      NUMERO_SINTOMA: numeroSintoma,
      NUMERO_LINEA: '1',
      USUARIO: this.resourcesService.getUser()
    })).then(
      (responseData: ResponseData) => {
        this.resourcesService.openSnackBar('¡Eliminación Exitosa!');
      }, (error: HttpErrorResponse) => this.resourcesService.checkErrors(error)
    );
  }


}
