import { Component, Input } from "@angular/core";
import { NgbActiveModal } from "@ng-bootstrap/ng-bootstrap";
import { DeviceService } from "../../service/device.service";

@Component({
  selector: "app-modal-export",
  templateUrl: "./modal-export.component.html",
  styleUrls: ["./modal-export.component.css"],
})
export class ModalExportComponent {
  @Input() buildingId: number;
  @Input() dataRow: any;
  data: any;
  filteredRows: any[];
  data_device: any[];
  constructor(
    public activeModal: NgbActiveModal,
    private deviceService: DeviceService
  ) {}

  ngOnInit() {
    this.fetchData(this.buildingId);
  }

  fetchData(buildingSelected) {
    this.deviceService.getDeviceDataById(buildingSelected).subscribe((res) => {
      this.data = res;
      this.filteredRows = this.data.results.data;
      this.data_device = this.filteredRows.map((item) => ({
        buildingName: item.building_name,
        id: item.id,
        name: item.name,
        icon: item.icon,
        watt: item.watt,
        categoryName: item.category_name,
        typeName: item.type_name,
        voltageName: item.voltage_name,
        statusName: item.status_name,
      }));
    });
  }
}
