import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { Location } from '@angular/common'
import { MasterService } from 'src/app/_providers/_services/master.service';
import { apiUrl } from 'src/app/_providers/_resources/api-url.properties';
import { ActivatedRoute } from '@angular/router';
import { MatTableDataSource } from '@angular/material/table';
import { MatSort } from '@angular/material/sort';
import { log } from 'console';
import { UiService } from 'src/app/_providers/_services/ui.service';
import { ErrorTranslateService } from 'src/app/_providers/_services/error-translate.service';

@Component({
  selector: 'app-requoted-report',
  templateUrl: './requoted-report.component.html',
  styleUrls: ['./requoted-report.component.scss']
})

export class RequotedReportComponent implements OnInit {
  loading: boolean;

  reportData: any;
  searchTerm:any;
  tenderID: number;
  tableData: any;
  dataSource: MatTableDataSource<unknown>;
  displaycolumns: string[] = ['slNo', 'name', 'mobileNumber', 'email', 'boqDocument', 'boqStatus'];
  selectedOrder: any

  @ViewChild('table', { static: false }) table: ElementRef;

  @ViewChild(MatSort, { static: false }) sort: MatSort;
  formattedOrders: any[]
  data: any;
  boqStatus: string;
  dataCopy: any;
  allData: any;

  constructor(private _location: Location,
    private _masterService: MasterService,
    private _route: ActivatedRoute,
    private  _ui :UiService,
    private errorTranslateService:ErrorTranslateService) {

    this.tenderID = + this._route.snapshot.paramMap.get('id')
      

  }

  ngOnInit(): void {
    this.getData();

  }

  getData() {
    this.loading = true;
    this._masterService.getMasterDatabyId(apiUrl.revisedTenderReport, this.tenderID).then(res => {
      this.tableData = res;
      this.loading = false;

      let uniqueOrders = [];

      this.tableData.forEach(element => {
        if (element.revisedDocument != null) {
          this.boqStatus = 'Uploaded'


        } else {
          this.boqStatus = 'not uploaded'


        }
        element.boqStatus = this.boqStatus;
      });

      this.tableData.forEach(element => {
        let order = element.requotedOrder;

        if (!uniqueOrders.includes(order)) {
          uniqueOrders.push(order);
        }
      });

      this.formattedOrders = uniqueOrders.map(order => {
        return { order: order };
      });

       
      this.dataSource = new MatTableDataSource(this.tableData);
      this.dataSource.sort = this.sort;

      let groupedOrders = {};

      this.tableData.forEach(element => {
        let order = element.requotedOrder;

        if (!groupedOrders[order]) {
          groupedOrders[order] = [];
        }

        groupedOrders[order].push(element);
      });
      this.data = groupedOrders
      this.dataCopy = groupedOrders 
    })

  }


  requoteNoChange(order) {
   
    this.allData =this.data[order]
    this.dataCopy =this.data[order]
 
    this.dataSource =this.allData; 
  }

  backButton() {
    this._location.back();
  }

search(){

  // this.allData = [...this.allData];
  this.allData = [...this.dataCopy];
 
  if (!this.searchTerm || this.searchTerm.trim() === "") {
    this._ui.toastMessage(this.errorTranslateService.translate('enterSearchTerm'), 3000);
    this.requoteNoChange(this.selectedOrder);
    return;
}

 this.allData =this.allData.filter(res => { 
  let includedInUser = (res.userD.userName) ? res.userD.userName.toLowerCase().includes(this.searchTerm.toLowerCase()) : false;
   
  let includedInUserEmail = (res.userD.email) ? res.userD.email.toLowerCase().includes(this.searchTerm.toLowerCase()) : false;
  let includedInUserPhno = (res.userD.mobileNumber) ? res.userD.mobileNumber.toLowerCase().includes(this.searchTerm.toLowerCase()) : false;

  if (includedInUser || includedInUserEmail || includedInUserPhno) { 
     
    return true;

  }

  else
  return false;
});

// this.noData = (this.userLov.length === 0);
// this.pageLength = this.userLov.length;
this.dataSource = new MatTableDataSource(this.allData);

}

reset(){
 
    this.searchTerm = "";
    this. requoteNoChange(this.selectedOrder); 
}

  sortData(event) {

  }

  
  downloadBoqdocs(docName) {
    this.loading=true;
 
    this._masterService.downloadFileWithAuth(apiUrl.quotedTender,docName).then(res=>{
 
      this.loading=false;

    })
  
  }
}
