import { Component, OnInit } from '@angular/core';
import { NzModalService, NzDropdownMenuComponent, NzContextMenuService, NzTreeNode, NzFormatEmitEvent } from 'ng-zorro-antd';
import { MessageService, Config } from 'pz-unit';
import { AuthStatusApi } from 'app/core/authStatus.api';
import { <%= classify(name) %>DialogComponent } from './dialog/<%= dasherize(name) %>-dialog.component';
import { <%= classify(name) %>Api } from 'app/api/<%= dasherize(name) %>.api';
import { <%= classify(name) %>Model } from 'app/api/model/<%= dasherize(name) %>.model';
import { EnumResourceList } from 'app/api/enums/Enum<%= classify(name) %>';

@Component({
    selector: '<%= selector %>',
    templateUrl: './<%= dasherize(name) %>.component.html',
    styleUrls: ['./<%= dasherize(name) %>.component.<%= style %>']
})
export class <%= classify(name) %>Component implements OnInit {
    public pageConfig: any = new Config();

        activedNode: NzTreeNode;
        nodes = [
            {
                title: 'parent 0',
                key: '100',
                author: 'NG ZORRO',
                expanded: true,
                children: [
                    { title: 'leaf 0-0', key: '1000', author: 'NG ZORRO', isLeaf: true },
                    { title: 'leaf 0-1', key: '1001', author: 'NG ZORRO', isLeaf: true }
                ]
            },
            {
                title: 'parent 1',
                key: '101',
                author: 'NG ZORRO',
                children: [
                    { title: 'leaf 1-0', key: '1010', author: 'NG ZORRO', isLeaf: true },
                    { title: 'leaf 1-1', key: '1011', author: 'NG ZORRO', isLeaf: true }
                ]
            }
        ];


    public constructor(
            private api: <%= classify(name) %>Api,
            private modal: NzModalService,
            private auth: AuthStatusApi,
            private msg: MessageService,
            private nzContextMenuService: NzContextMenuService
    ) {
        }

    public ngOnInit() {
            this.getResource();
    }

    getResource() {
        this.auth.getRouteResource(location.pathname).subscribe(res => {
            this.pageConfig = new Config(res, EnumResourceList);
            if (this.pageConfig.canActive(1)) {
                this.getPage();
            }
        });
    }

    public openDialog(id: string, type: string) {
            this.msg.showLoading();
            // 模态窗放式弹窗
            const modal = this.modal.create({
                nzTitle: '详情内容',
                nzContent: <%= classify(name) %>DialogComponent,
                nzStyle: {top: '20px', padding: '0 20px 20px 20px'},
                nzWidth: '60vw',
                nzFooter: null
            });
            // 抽屉弹窗
            modal.afterOpen.subscribe(() => {
                // 赋值
                if (type === 'update' || type === 'view') {
                    // 更新操作
                    this.msg.hideLoading();
                } else {
                    this.msg.hideLoading();
                }
            });
            modal.afterClose.subscribe((result) => {
                if (result) {
                    this.getPage();
                }
            });
        }

        // 页面加载数据
    public getPage() {
            this.msg.showLoading();
            this.api.getAllList().subscribe(
                (res: any) => {
                    this.msg.hideLoading();
                    this.nodes = res;
                    console.log(res);
                }
            );
        }

        openFolder(data: NzTreeNode | Required<NzFormatEmitEvent>): void {
            // do something if u want
            if (data instanceof NzTreeNode) {
            data.isExpanded = !data.isExpanded;
        } else {
            const node = data.node;
            if (node) {
                node.isExpanded = !node.isExpanded;
            }
        }
    }

        activeNode(data: NzFormatEmitEvent): void {
            this.activedNode = data.node!;
    }

        contextMenu($event: MouseEvent, menu: NzDropdownMenuComponent): void {
            this.nzContextMenuService.create($event, menu);
    }

    selectDropdown(): void {
        // do something
    }
}
