import { Component, ChangeDetectionStrategy, Inject, OnInit, OnDestroy } from '@angular/core';
import { SettingsService } from '@delon/theme';
import { Router } from '@angular/router';
import { _HttpClient } from '@delon/theme';
import { DA_SERVICE_TOKEN, ITokenService } from '@delon/auth';
import { environment } from '@env/environment';

@Component({
  selector: 'layout-header',
  templateUrl: './header.component.html',
  styles: [`
    .alain-default__nav-item, .alain-default__nav nz-badge {
      color: rgba(0, 0, 0, 0.85);
    }
  `],
  // changeDetection: ChangeDetectionStrategy.OnPush
})
export class HeaderComponent implements OnInit, OnDestroy {
  searchToggleStatus: boolean;
  dt = new Date();
  timer;

  constructor(public settings: SettingsService,
  private router: Router,
  private http: _HttpClient,
  @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService) { }

  ngOnInit(): void {
    // this.timer = setInterval(() => {
    //   this.dt = new Date();
    // }, 1000);
  }

  ngOnDestroy(): void {
    // clearInterval(this.timer);
  }

  toggleCollapsedSidebar() {
    this.settings.setLayout('collapsed', !this.settings.layout.collapsed);
  }

  searchToggleChange() {
    this.searchToggleStatus = !this.searchToggleStatus;
  }

  logout() {
    this.http.get(environment.baseUrl + '/login/logout').subscribe((res: any) => {
      console.log(res);
      // this.data = res;
      // this.change({ index: 0, tab: null });
      // this.cdr.detectChanges();
    });
    this.tokenService.clear();
    this.router.navigateByUrl(this.tokenService.login_url);
  }
}
