import { Component, OnInit,ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { apiUrl } from 'src/app/_providers/_resources/api-url.properties';
import { AuthService } from 'src/app/_providers/_services/auth.service';
import { MasterService } from 'src/app/_providers/_services/master.service';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort, Sort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { TenderDetailDto } from 'src/app/_dto/tender-detail.dto';
import { ErrorTranslateService } from 'src/app/_providers/_services/error-translate.service';
import { UiService } from 'src/app/_providers/_services/ui.service';
@Component({
  selector: 'app-admin-home',
  templateUrl: './admin-home.component.html',
  styleUrls: ['./admin-home.component.scss']
})
export class AdminHomeComponent implements OnInit {

  tenderData: any;

  dataSource: MatTableDataSource<TenderDetailDto>;

  displayedColumns = ['slno','title','publishedDate','bidOpenDate','view'];


  dataCopy: any;

  pageLength: any;

  searchTerm:any;

  @ViewChild('paginator') paginator: MatPaginator;

  @ViewChild(MatSort, { static: false }) sort: MatSort;

  loading: boolean = true;

  noData: boolean;

  constructor(
    private authenticationService: AuthService,
    private router: Router,
    private _masterService: MasterService,
    private errorService:ErrorTranslateService,
    private _ui: UiService,
    ) { }

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

  getData(){
    
    this._masterService.getMasterData(apiUrl.getTender +'?order[id]=DESC').then((res) =>{
    //  console.log("tenderData=1=",res);
      this.tenderData = res;
      this.dataCopy = res;
      this.pageLength = this.tenderData.length;
      this.dataSource = new MatTableDataSource(this.tenderData);
      this.dataSource.sort = this.sort;
      this.dataSource.paginator = this.paginator;
      this.noData = (this.tenderData && this.tenderData.length == 0) ? true : false;
      this.loading = false;
    },err=>{
      console.error(err)
    })
   
  }
  public signOut(): void {
 
    this.authenticationService.logOut();
    
  }

  signIn(){
    this.router.navigate(['/login']);
  }

  goToHome(){
    window.location.href = 'https://www.thrickodithanamscb.in/index.html#hero';
  }
  goToContactUs(){
    window.location.href = 'https://www.thrickodithanamscb.in/index.html#contact';
  }
  
  goToView(tenderId:number) {
    this.router.navigate(['/auth/view',tenderId]);
  }

  goToUpload(tenderId){
    this.router.navigate(['/tender-upload',tenderId]);
 }
  search(){
    this.tenderData = this.dataCopy;
    this.loading = true;
    if (!this.searchTerm || this.searchTerm == null || this.searchTerm == undefined || this.searchTerm == "") {
      this._ui.toastMessage(this.errorService.translate('enterSearchTerm'), 3000);
      this.getData();
      return;
    }
    this.tenderData = this.tenderData.filter(res => {
      let includedInTitle = (res.title) ? res.title.toLowerCase().includes(this.searchTerm.toLowerCase()) : false;
      let includedInPublishDate = (res.publishDate) ? res.publishDate.includes(this.searchTerm.toLowerCase()) : false;
      
      let includedInbidOpenDate = (res.bidOpenDate) ? res.bidOpenDate.includes(this.searchTerm.toLowerCase()) : false;

      if (includedInTitle)
        return true;
      if (includedInPublishDate)
        return true;
        if (includedInbidOpenDate)
        return true;
      else
        return false;
    });
    this.noData = (this.tenderData && this.tenderData.length == 0) ? true : false;
    this.pageLength = this.tenderData.length;
    this.dataSource = new MatTableDataSource(this.tenderData);
    this.loading = false;
  }

  reset(){
    this.searchTerm = "";
    this.getData();
  }

  sortData(sort: Sort) {

    const data = this.tenderData.slice();
    if (!sort.active || sort.direction === '') {
      this.dataSource.data = data;
      
      return;
    }

    this.dataSource.data = data.sort((a, b) => {
      const isAsc = sort.direction === 'asc';
      switch (sort.active) {
        case 'title':
          return compare(a.title, b.title, isAsc);
        case 'publishedDate':
          return compare(a.publishDate, b.publishDate, isAsc);
          case 'bidOpenDate':
          return compare(a.bidOpenDate, b.bidOpenDate, isAsc);
        default:
          return 0;
      }
    });
  }
  // public getUser(): Observable<User> {
    // return of({
    //    email: localStorage.getItem('username'),
    //    name: localStorage.getItem('username'),

    // });
  // }

  navigateTo(destination: any) {
    this.router.navigate([destination])
  }



}
function compare(a: any, b: any, isAsc: boolean): number {
  return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}