import { Component, Inject, OnInit, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatTableDataSource } from '@angular/material/table';
import { EnviarInfoService } from '../../../Administrador/services/enviar-info.service';
import { MAT_DIALOG_DATA, MatDialog, MatDialogActions, MatDialogClose, MatDialogContent } from '@angular/material/dialog';
import { BitacoraService } from '../../../Administrador/services/bitacora.service';
import { DomSanitizer } from '@angular/platform-browser';
import { CommonModule } from '@angular/common';
import { MatIcon } from '@angular/material/icon';
import { ActivatedRoute } from '@angular/router';
import { RegistroAcademicoService } from '../../services/registroAcademico.service';

@Component({
  selector: 'app-bitacora-hijos',
  templateUrl: './bitacora-hijos.component.html',
  styleUrl: './bitacora-hijos.component.css'
})
export class BitacoraHijosComponent implements OnInit {

  public color: string = '';
  public textColor: string = '';
  isLoading: boolean = true;
  private id: string = '';
  dataAlum: any;
  public columnasBitacoras: string[] = ['materia', 'tipo', 'descripcion', 'fechaPublicacion', 'acciones'];
  public dataSourceBitacoras = new MatTableDataSource<any>();

  @ViewChild(MatPaginator, { static: true }) paginator!: MatPaginator;

  constructor(
    private _enviarInfo: EnviarInfoService,
    public dialog: MatDialog,
    private bitacoraService: BitacoraService,
    private sanitizer: DomSanitizer,
    private _registro: RegistroAcademicoService,
    private route: ActivatedRoute
  ) {}

  ngOnInit(): void {
    this.route.paramMap.subscribe(params => {
      const id = params.get('id');
console.log(id);
      this.id = atob(id || '');
    });
    this._enviarInfo.currentTextColor.subscribe(textColor => {
      this.textColor = textColor;
    });
    this._enviarInfo.currentColor.subscribe(color => {
      this.color = color;
    });
    this._registro.getOne(this.id).subscribe((data: any) => {
      this.dataAlum = data;
      if (this.dataAlum) {
        this.obtenerBitacoras();
      }
    });

  }


  applyFilter(event: Event) {
    const filterValue = (event.target as HTMLInputElement).value;
    this.dataSourceBitacoras.filter = filterValue.trim().toLowerCase();
  }

  openDialog(bitacora: any) {
    this.dialog.open(Modalbitacora, {
      data: {
        titulo: bitacora.materia,
        contenido: this.sanitizer.bypassSecurityTrustHtml(bitacora.descripcion)
      }
    });
  }

  obtenerBitacoras() {
    this.bitacoraService.getByUser(this.dataAlum[0].idUsuario).subscribe((response: any) => {
      this.isLoading = false;
      this.dataSourceBitacoras = new MatTableDataSource<any>(response.filter((f: any) => f.estado === 'Activo'));
      this.dataSourceBitacoras.paginator = this.paginator;

      response.forEach((item: any) => {
        if (typeof item.jsonAdjuntos === 'string') {
          item.jsonAdjuntos = JSON.parse(item.jsonAdjuntos);
        }
        if (!Array.isArray(item.jsonAdjuntos)) {
          item.jsonAdjuntos = [];
        }
      });

      return response;
    });
  }
}


export class Modalbitacora {
  constructor(@Inject(MAT_DIALOG_DATA) public data: any) {}
}
