import { Component, Inject, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { MatTableDataSource } from '@angular/material/table';
import { DataProducts, DetailsProduct, RequestLines } from 'src/app/interfaces/acquisition-management/acquisition/order.interface';

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

  public formGroup: FormGroup;
  public displayedColumns: Array<string>;
  public dataSource:Array<DetailsProduct>;

  constructor(@Inject(MAT_DIALOG_DATA) public requestLine: RequestLines) {
    this.formGroup = this.createFormControl();
    this.dataSource = requestLine.PRODUCTOS;
    this.displayedColumns = ['MODELO','CODIGO','CARACTERISTICAS','CANTIDAD','PRECIO_UNITARIO','PRECIO_TOTAL'];
  }

  ngOnInit(): void {
  }

  public getTotalCost() {
    return this.dataSource.map(t => t.PRECIO_TOTAL * t.CANTIDAD).reduce((acc, value) => acc + value, 0);
  }

  private createFormControl(){
    return new FormGroup({
      descripcion : new FormControl(this.requestLine.DESCRIPCION , Validators.required),
      estado: new FormControl(this.requestLine.ESTADO, Validators.required),
      fechaModificacion: new FormControl(this.requestLine.FECHA_MODIFICA === null ? this.requestLine.FECHA_REGISTRA : this.requestLine.FECHA_MODIFICA, Validators.required),
      usuarioModificacion: new FormControl(this.requestLine.USUARIO_MODIFICA === null ? this.requestLine.USUARIO_REGISTRA : this.requestLine.USUARIO_MODIFICA, Validators.required),
    });
  }

  get descripcion () {return this.formGroup.get('descripcion')}
  get estado () {return this.formGroup.get('estado')}
  get fechaModificacion () {return this.formGroup.get('fechaModificacion')}
  get usuarioModificacion () {return this.formGroup.get('usuarioModificacion')}
}
