import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {Http} from '@angular/http';
import {LayoutService} from '../layout.service';
import {LocalStorageService, SessionStorageService} from 'ngx-webstorage';
import {CommonService} from '../../shared/common/common.service';
import {OverAllService} from '../../overAll/overAll.service';

@Component({
    selector: 'jhi-sidebar',
    templateUrl: './sidebar.component.html',
    styles: [
            ` span i {
            margin-right: 10px;
        }
        `
    ]
})
export class SidebarComponent implements OnInit {
    token: boolean;
    menuList = [];
    systemName;

    constructor(public router: Router, private http: Http,
                public layoutSer: LayoutService,
                public commonSer: CommonService,
                private overAll:OverAllService,
                private $localStorage: LocalStorageService,
                private $sessionStorage: SessionStorageService) {
    }

    ngOnInit() {
        this.getMenu();
        this.overAll.getSystem().subscribe(
            (res)=>{
                console.log(res);
                this.systemName = res.main.name;
            }
        )
        // this.menuList = this.commonSer.listToTree('id', 'parentId', this.menuList);
    }

    getMenu() {
        this.layoutSer.getMenu().subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.menuList = this.commonSer.listToTree('id', 'parentId', res.data);
                }
            }
        );
    }

    //简单使用

    openHandler(value: string): void {
        for (let key in this.menuList) {
            if (key !== value) {
                this.menuList[key].checked = false;
            }
        }
    }

    goTo(route) {
        this.router.navigate([route]);
    }

    goToRoute(node){
        this.$localStorage.store("moduleId",node.id);
        this.router.navigate(["app/main/"+ node.url]);
    }
}
