import {Component, ElementRef, OnInit, ViewChild} 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';
import {timer} from 'rxjs/observable/timer';
import {AppService} from '../../app.service';

@Component({
    selector: 'jhi-sidebar',
    templateUrl: './sidebar.component.html',
    styleUrls: ['./sidebar.component.css']
})
export class SidebarComponent implements OnInit {
    @ViewChild('slideUL') slideUL: ElementRef;
    token: boolean;
    menuList = [];
    systemName;
    isCollapsed;

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

    ngOnInit() {
        this.getMenu();
        this.overAll.getSystem().subscribe(
            (res) => {
                this.systemName = res.main.name;
            }
        );
        this.appSer.Collapse.subscribe(value => {
            this.isCollapsed = value;
        })
        // 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;
            }
        }
    }

    toggle() {
        timer(100).subscribe(e => {
            console.log(this.slideUL.nativeElement.clientHeight);
            this.appSer.setContentHeight(this.slideUL.nativeElement.clientHeight);
        });
    }

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

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