import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {WorkService} from '../../work.service';
import {NzFormatEmitEvent, NzMessageService, NzTreeNode} from 'ng-zorro-antd';

@Component({
    selector: 'smart-link-inventory',
    templateUrl: './link-inventory.component.html',
    styles: []
})
export class LinkInventoryComponent implements OnInit {
    @Output() done = new EventEmitter<any>();

    title;
    isVisiable = false;
    invertoryList;

    nodes;
    nodeList;
    selectList = [];
    allChecked;
    indeterminate;

    constructor(private workSer: WorkService, private message: NzMessageService) {
    }

    checkAll(value: boolean): void {
        this.invertoryList.forEach(data => data.checked = value);
        this.refreshStatus();
    }

    refreshStatus(): void {
        const allChecked = this.invertoryList.every(value => value.checked === true);
        const allUnChecked = this.invertoryList.every(value => !value.checked);
        this.allChecked = allChecked;
        this.indeterminate = (!allChecked) && (!allUnChecked);
    }

    selectItem(item,e){
        if(e){
            this.selectList.push(item);
        }else{
            this.selectList.forEach((value,index)=>{
                if(value.id == item.id){
                    this.selectList.splice(index,1)
                }
            })
        }
        this.refreshStatus();
    }

    ngOnInit() {
        this.getList();
    }

    toNode(data) {
        this.nodes = data.map(res => {
            return new NzTreeNode(res);
        });
    }

    //父级分类
    getList() {
        this.workSer.findByParentType().subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    let option = res.data;
                    option.forEach(res => {
                        res.title = res.name;
                        res.key = res.id;
                        res.selectable = false;
                    });
                    this.nodeList = option;
                    this.toNode(option);
                } else {
                    this.message.error(res.errMsg);
                }
            }
        );
    }

    //获取下级---子分类
    mouseAction(name: string, event: NzFormatEmitEvent) {
        if(name == "click"){
            this.selectCheckTree(event);
        }else{
            const index = <any>event.node.key - 1;
            const pId = event.node.origin.id;
            this.workSer.findByParentidCount(pId).subscribe(
                (res) => {
                    if (res.data) {
                        const dataSet = res.data;
                        dataSet.forEach(res => {
                            res.title = res.name;
                            res.key = res.id;
                            res.isLeaf = true;
                        });
                        event.node.addChildren(dataSet);
                    } else {
                        event.node.addChildren([]);
                        // this.message.warning('该下级为空');
                    }

                }
            );
            setTimeout(_ => {
            }, 1000);
        }
    }

    //选择树节点
    selectCheckTree(event: NzFormatEmitEvent) {
        const cId = event.node.origin.id;
        this.workSer.findInventory(cId).subscribe(
            (res) => {
                this.invertoryList = res.data;
            }
        );
    }

    showModal(title) {
        this.isVisiable = true;
        this.title = title;
    }

    handleEditCancel(){
        this.selectList = [];
        this.isVisiable = false;
    }

    handEditleOk(){
        this.isVisiable = false;
        this.done.emit(this.selectList);
    }

}
