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

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

    title;
    isVisible = false;
    fileList: UploadFile[] = [];

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

    ngOnInit() {
    }

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

    beforeUpload = (file: UploadFile): boolean => {
        const isExcel = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
        if (!isExcel) {
            this.message.error('请上传excel文件!');
        } else {
            this.fileList[0] = file;
        }
        return false;
    };

    handleCancel() {
        this.isVisible = false;
        this.fileList = [];
    }

    handleOk() {
        if (this.fileList.length == 0) {
            this.message.warning('请选择文件');
            return false;
        }
        if (this.title == '上传资产文件') {
            this.assetsUpload();
        }
        if (this.title == '上传用户文件') {
            this.userUpload();
        }
        if (this.title == '上传组织文件') {
            this.orgUpload();
        }
    }

    //上传资产文件
    assetsUpload() {
        const formData = new FormData();
        this.fileList.forEach((file: any) => {
            formData.append('file', file);
        });
        this.workSer.importInventory(formData).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.isVisible = false;
                    this.fileList = [];
                    this.message.success('上传成功');
                } else {
                    this.message.error(res.errMsg);
                }
            }
        );
    }

    //导入用户
    userUpload() {
        const formData = new FormData();
        this.fileList.forEach((file: any) => {
            formData.append('user', file);
        });
        this.systemSer.importUser(formData).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.isVisible = false;
                    this.fileList = [];
                    this.done.emit();
                    this.message.success('上传成功');
                } else {
                    this.message.error(res.errMsg);
                }
            }
        );
    }

    //导入组织
    orgUpload() {
        const formData = new FormData();
        this.fileList.forEach((file: any) => {
            formData.append('organization', file);
        });
        this.systemSer.importOrg(formData).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.isVisible = false;
                    this.fileList = [];
                    this.done.emit();
                    this.message.success('上传成功');
                } else {
                    this.message.error(res.errMsg);
                }
            }
        );
    }
}
