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';
import {OverAllService} from '../../../overAll/overAll.service';
import {InfomationService} from '../../../infomationResource/infomation.service';

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

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

    constructor(private message: NzMessageService, private workSer: WorkService,
                private systemSer: SystemService,private overAllSer:OverAllService,
                private inforSer:InfomationService) {
    }

    ngOnInit() {
    }

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

    beforeUpload = (file: UploadFile): boolean => {
        const isLt5M = file.size / 1024 / 1024 < 5;
        if (!isLt5M) {
            this.message.error('文件最大上传5M!');
            return false;
        }
        const isExcel = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' || 'application/octet-stream';
        if (!isExcel) {
            this.message.error('请上传正确的文件!');
        } else {
            this.fileList[0] = file;
        }
        return false;
    };

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

    handleOk() {
        if (this.fileList.length == 0) {
            this.message.warning('请选择文件');
            return false;
        }
        this.isOkLoading = true;
        if (this.title == '上传资产文件') {
            this.assetsUpload();
        }
        if (this.title == '上传用户文件') {
            this.userUpload();
        }
        if (this.title == '上传组织文件') {
            this.orgUpload();
        }
        if(this.title == '上传XML模版'){
            this.xmlpload();
        }
        if(this.title == "导入资产"){
            this.assetUpload()
        }
        if(this.title == "导入备件管理数据"){
            this.componentUpload();
        }
        if (this.title == '上传常见问题文件' || this.title == '上传信息技术文件' || this.title == '上传管理制度文件') {
            this.knowladgeUpload();
        }
    }

    //上传资产文件
    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('上传成功');
                }
                this.isOkLoading = false;
            }
        );
    }

    //导入用户
    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('上传成功');
                }
                this.isOkLoading = false;
            }
        );
    }

    //导入组织
    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('上传成功');
                }
                this.isOkLoading = false;
            }
        );
    }

    //上传XML文件
    xmlpload() {
        const formData = new FormData();
        this.fileList.forEach((file: any) => {
            formData.append('file', file);
        });
        this.overAllSer.importTemplates(formData).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.isVisible = false;
                    this.fileList = [];
                    this.done.emit();
                    this.message.success('上传成功');
                }
                this.isOkLoading = false;
            }
        );
    }

    assetUpload(){
        const formData = new FormData();
        this.fileList.forEach((file: any) => {
            formData.append('file', file);
        });
        this.inforSer.importE(formData).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.isVisible = false;
                    this.fileList = [];
                    this.message.success('上传成功');
                    this.done.emit();
                }
                this.isOkLoading = false;
            }
        );
    }

    //知识库里面的文件
    knowladgeUpload() {
        let flag;
        if (this.title == '上传常见问题文件') flag = 0;
        if (this.title == '上传信息技术文件') flag = 1;
        if (this.title == '上传管理制度文件') flag = 2;
        const formData = new FormData();
        this.fileList.forEach((file: any) => {
            formData.append('file', file);
        });
        this.workSer.importKnowladage(formData, flag).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.isVisible = false;
                    this.fileList = [];
                    this.done.emit();
                    this.message.success('上传成功');
                } else {
                    this.message.error(res.errMsg);
                }
                this.isOkLoading = false;
            }
        );
    }

    //备件管理文件
    componentUpload(){
        const formData = new FormData();
        this.fileList.forEach((file: any) => {
            formData.append('file', file);
        });
        this.workSer.componentImportExcel(formData).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.isVisible = false;
                    this.fileList = [];
                    this.message.success('上传成功');
                    this.done.emit();
                }
                this.isOkLoading = false;
            }
        );
    }
}
