import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {NzMessageService, UploadFile} from 'ng-zorro-antd';
import {TopologyService} from '../../topology.service';
import {FormBuilder, FormGroup, Validator, Validators} from '@angular/forms';
import {filter} from 'rxjs/operators';

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

    isVisible = false;
    fileList: UploadFile[] = [];
    title;
    firstTypeId;  //分类ID
    iconId;   //图标ID

    options;  //一级分类list；

    constructor(private topologySer: TopologyService, private message: NzMessageService,
                private fb: FormBuilder) {
    }

    values;

    ngOnInit() {
        this.initForm();
        this.getList();
    }

    initForm() {
        this.fileList = [];
        this.firstTypeId = null;  //分类ID
        this.iconId = null;   //图标ID
    }

    //一级分类
    getList(){
        this.topologySer.findTreeWithWeb().subscribe(
            (res)=>{
                if(res.errCode == 10000){
                    this.options = res.data;
                }else{
                    this.message.error(res.errMSg);
                }
            }
        )
    }

    beforeUpload = (file: UploadFile): boolean => {
        const isLt2M = file.size / 1024 < 300;
        if (!isLt2M) {
            this.message.error('图标必须小于300kb!');
        } else {
            this.fileList.push(file);
        }
        return false;
    };

    showAddModal(title) {
        this.title = title;
        this.isVisible = true;
    }

    showEditModal(title,id) {
        this.iconId = id;
        this.title = title;
        this.isVisible = true;
    }

    handleOk() {
        if (this.fileList.length == 0) {
            this.message.error('请选择图标');
            return false;
        }

        if(this.title == "添加图标"){
            if (!this.firstTypeId) {
                this.message.error('请选择一级分类');
                return false;
            }else{
                this.create();
            }
        }

        if(this.title == "更换图标"){
            this.update();
        }
    }

    create(){
        const formData = new FormData();
        this.fileList.forEach((file: any) => {
            formData.append('file', file);
        });
        formData.append('firstTypeId', this.firstTypeId);
        this.topologySer.iconUpload(formData).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.message.success('添加成功');
                    this.isVisible = false;
                    this.initForm();
                    this.done.emit();
                } else {
                    this.message.error(res.errMsg);
                }
            }
        );
    }

    update(){
        const formData = new FormData();
        this.fileList.forEach((file: any) => {
            formData.append('file', file);
        });
        formData.append('id', this.iconId);

        this.topologySer.updateIcon(formData).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.message.success('添加成功');
                    this.isVisible = false;
                    this.initForm();
                    this.done.emit();
                } else {
                    this.message.error(res.errMsg);
                }
            }
        );
    }

    handleCancel() {
        this.isVisible = false;
        this.initForm();
    }

}
