import {Component, OnInit, ViewChild, Injector} from '@angular/core';
import {Router} from '@angular/router';

const screenfull = require('screenfull');

import {UserblockService} from '../sidebar/userblock/userblock.service';
import {SettingsService} from '../../core/settings/settings.service';
import {MenuService} from '../../core/menu/menu.service';
import {BsModalService} from "ngx-bootstrap/modal";
import {FrameOptrPasswordComponent} from "../../routes/frame/optr/frame-optr-password/frame-optr-password.component";
import {FrameService} from "../../core/service/frame.service";
import {FrameOptrEditComponent} from "../../routes/frame/optr/frame-optr-edit/frame-optr-edit.component";
import {MsgService} from "../../core/service/msg.service";
import {AjaxService} from "../../core/service/ajax.service";

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

  navCollapsed = true; // for horizontal layout
  menuItems = []; // for horizontal layout
  router: Router;

  isNavSearchVisible: boolean;
  @ViewChild('fsbutton', {static: true}) fsbutton;  // the fullscreen button

  constructor(public menu: MenuService, public userblockService: UserblockService, public settings: SettingsService, public injector: Injector,public modalService: BsModalService) {

    // show only a few items on demo
    this.menuItems = menu.getMenu().slice(0, 4); // for horizontal layout

  }

  ngOnInit() {
    this.isNavSearchVisible = false;

    var ua = window.navigator.userAgent;
    if (ua.indexOf("MSIE ") > 0 || !!ua.match(/Trident.*rv\:11\./)) { // Not supported under IE
      this.fsbutton.nativeElement.style.display = 'none';
    }

    // Switch fullscreen icon indicator
    const el = this.fsbutton.nativeElement.firstElementChild;
    screenfull.on('change', () => {
      if (el)
        el.className = screenfull.isFullscreen ? 'fa fa-compress' : 'fa fa-expand';
    });

    this.router = this.injector.get(Router);

    // Autoclose navbar on mobile when route change
    this.router.events.subscribe((val) => {
      // scroll view to top
      window.scrollTo(0, 0);
      // close collapse menu
      this.navCollapsed = true;
    });

  }

  toggleUserBlock(event) {
    event.preventDefault();
    this.userblockService.toggleVisibility();
  }

  openNavSearch(event) {
    event.preventDefault();
    event.stopPropagation();
    this.setNavSearchVisible(true);
  }

  setNavSearchVisible(stat: boolean) {
    // console.log(stat);
    this.isNavSearchVisible = stat;
  }

  getNavSearchVisible() {
    return this.isNavSearchVisible;
  }

  toggleOffsidebar() {
    this.settings.toggleLayoutSetting('offsidebarOpen');
  }

  toggleCollapsedSideabar() {
    this.settings.toggleLayoutSetting('isCollapsed');
  }

  isCollapsedText() {
    return this.settings.getLayoutSetting('isCollapsedText');
  }

  toggleFullScreen(event) {
    screenfull.toggle();
  }

  /****************
   * 修改密码
   */
  modifyPassword($event) {
    const modal = Object.assign({}, {
      animated: true,
      keyboard: false,
      backdrop: true,
      ignoreBackdropClick: true,
      initialState: {},
      class: 'modal-4p'
    });
    modal.initialState = {};
    modal.initialState['ResponseEntity'] = FrameService.FrameOptr;
    modal.initialState['optrModel'] = 'edit';
    this.modalService.show(FrameOptrPasswordComponent,modal );
  }


  /****************
   * 修改操作员信息
   */
  modifyOptr($event) {
    const modal = Object.assign({}, {
      animated: true,
      keyboard: false,
      backdrop: true,
      ignoreBackdropClick: true,
      initialState: {},
      class: 'modal-65p'
    });
    modal.initialState = {};
    let opt = Object.assign({},FrameService.FrameOptr);
    opt.resMap = undefined;
    modal.initialState['ResponseEntity'] = opt;
    this.modalService.show(FrameOptrEditComponent,modal );
  }

  //退出系统
  logout(){
    MsgService.confirmAlert('确定要退出系统吗？', () => {
      AjaxService.request("frame/optr/logout.htm",{},  () => {
        window.location.href = "./assets/login/index.html";
      });
    });
  }


}
