import { Component, OnInit, ViewChild } from '@angular/core';
const screenfull = require('screenfull');
const browser = require('jquery.browser');
declare var $: any;

import { UserblockService } from '../sidebar/userblock/userblock.service';
import { SettingsService } from '../../core/settings/settings.service';
import { MenuService } from '../../core/menu/menu.service';
import { FrameRequest } from '../../shared/frame/frame-request';
import { AppComponent } from '../..';
import { BsModalService } from "ngx-bootstrap/modal";
import {InjectorInstance} from '../../app.module';
import { FrameVariable } from '../../shared/frame/frame-variable';



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

    navCollapsed = true; // for horizontal layout
    menuItems: Array<any>;
    levelOnemenuItems: Array<any>;
    levelTwomenuItems: Array<any>;
    
    idCardWin: any;
    jcJcryxxzb: any;

    isActive=0;

    listItem=[];

    isNavSearchVisible: boolean;
    @ViewChild('fsbutton') fsbutton;  // the fullscreen button

    login_name = null;
    dlzh = null;
    role_name = null;
    jgmc = null;
    dept_name = null;

    token = null;


    constructor(public bsModalService: BsModalService,public menu: MenuService, public userblockService: UserblockService, public settings: SettingsService) {

        // show only a few items on demo
        
        this.menuItems = menu.getMenu();
        this.levelOnemenuItems = menu.getLevelOneMenu();
        
    }

    ngOnInit() {
        this.isNavSearchVisible = false;
        if (browser.msie) { // Not supported under IE
            this.fsbutton.nativeElement.style.display = 'none';
		}
		this.initUserName();
        

        //this.listItem= [
       //
         //   { name: '业务管理' },
        //    { name: '信息查询' },
           
         //   { name: '信息打印' },
        //    { name: '业务配置' },
       // ]
        
	}
	
	initUserName(){
		let that=this;
		if (!localStorage.getItem("login_name")) {
			setTimeout(function(){
				that.initUserName();
			},1000);
		}else{
            
			this.login_name = localStorage.getItem("login_name");
            let optr_org_id = localStorage.getItem("optr_org_id");
            this.token = localStorage.getItem("token");
            if("undefined" == this.token){
                this.token=null;
            }
			
			FrameRequest.requestMap("cf/cfOrganization/queryForLogin.htm",{}, async (map) => {
                
                this.login_name = map.info;
                this.dlzh = map.dlzh;
                this.role_name = map.role_name;
                this.jgmc = map.jgmc;
                this.dept_name = map.dept_name;
                
                this.jcJcryxxzb = map.jcJcryxxzb;
			});
        }
        
        setTimeout(function(){
            
            let div_widht = $('#login_name_div').width();
            if(div_widht && div_widht>251){
                $('#login_out_div').css('right',div_widht);
            }

            
           
        },1000);
	}

    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) {

        if (screenfull.enabled) {
            screenfull.toggle();
        }
        // Switch icon indicator
        let el = $(this.fsbutton.nativeElement);
        if (screenfull.isFullscreen) {
            el.children('em').removeClass('fa-expand').addClass('fa-compress');
        }
        else {
            el.children('em').removeClass('fa-compress').addClass('fa-expand');
        }
    }

    async logout(){
        AppComponent.confirm('确定要退出系统？', () => {
            FrameRequest.requestEntity("frame/optr/logout.htm",{}, async (entity) => {
                let logout_url =  "/assets/login/index.html";
                let token = localStorage.getItem("token");
                let sso_url = localStorage.getItem("sso_url");;
                
                if(token && token != 'null' && token != 'undefined'){
                    if(sso_url && sso_url != 'null' && sso_url != 'undefined'){
                        logout_url =  sso_url;
                    }else{
                        logout_url =  "https://zhgl.gxeea.cn:20080/login/index";
                    }
                  
                }

                localStorage.clear();
                window.location.href = logout_url;
            });
        });
       
    }

    async goback(){
        //AppComponent.confirm('确定返回到门户吗？', () => {
            FrameRequest.requestEntity("frame/optr/logout.htm",{}, async (entity) => {
                let logout_url =  "/assets/login/index.html";
                let token = localStorage.getItem("token");
                let sso_url = localStorage.getItem("sso_url");;
                
                if(token && token != 'null' && token != 'undefined'){
                    if(sso_url && sso_url != 'null' && sso_url != 'undefined'){
                        logout_url =  sso_url;
                    }else{
                        logout_url =  "https://zhgl.gxeea.cn:20080/login/index";
                    }
                  
                }

                localStorage.clear();
                window.location.href = logout_url;
            });
        //});
       
    }

    showIdCardDialog(templateModal) {
    
        this.idCardWin = this.bsModalService.show(templateModal, { class: "modal-lg" });
        
      }

      clickLi(item,index) {
		//点击时，将索引值赋值给变量isActive可以实现点击切换样式
        this.isActive = index;

         // 添加二级菜单
         const mService =  InjectorInstance.get<MenuService>(MenuService);

        // 二级菜单
        const levelTwoMenu = new Array();

        let resArray = FrameVariable.ResArray;

        for (const v of resArray) {
            
            if(item.text==v.res_name){
                // 二级菜单
                if (v.childArray && v.childArray.length > 0) {
                    for (const c of v.childArray) {
                    if (c.childArray && c.childArray.length > 0) {
                        const dArray = new Array();
                        for (const d of c.childArray) {
                            dArray.push({
                            text: d.res_name,
                            link: d.res_url,
                            icon: d.res_css
                            });
                        }
        
                        levelTwoMenu.push({
                            text: c.res_name,
                            link: c.res_url,
                            icon: c.res_css,
                            submenu: dArray
                        });
                    } else {
                            levelTwoMenu.push({
                            text: c.res_name,
                            link: c.res_url,
                            icon: c.res_css
                        });
                    }
                    }
                    }
            }
            
        }

      
        mService.resetLevelTwoMenu(levelTwoMenu);

        
        let $aside = $('.aside-container');
        $aside.children('.aside-inner').click();
       
      

	}
    
}
