import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { NzModalService } from 'ng-zorro-antd';
import { NgCortexService } from '@ng-cortex/core';

@Component({
  selector: 'nc-home',
  templateUrl: './nc-home.component.html',
  styleUrls: ['./nc-home.component.scss']
})
export class NcHomeComponent implements OnInit {

  _navigating = false;

  constructor(
    private _router: Router,
    private _modalSrv: NzModalService,
    private _ncSrv: NgCortexService
  ) { }

  ngOnInit() {
    if (this._ncSrv.activeApp && this._ncSrv.routeContents.length > 0) {
      this._navigating = true;
      this._modalSrv.confirm({
        nzTitle: `返回首页`,
        nzContent: `
          <p>即将退出 <strong>“${this._ncSrv.activeApp.name}”</strong> ，当前打开模块将被全部关闭。</p>
        `,
        nzOnOk: () => {
          this._navigating = false;
          this._ncSrv.changeApp(null).subscribe();
          this._ncSrv.clearRoute(null);
        },
        nzOnCancel: () => {
          this._router.navigateByUrl(`/${this._ncSrv.activeApp.code}`, { replaceUrl: true });
        }
      });
    } else {
      this._ncSrv.changeApp(null).subscribe();
      this._ncSrv.clearRoute(null);
    }
  }

}
