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

@Component({
  selector: 'smart-upload',
  templateUrl: './upload.component.html',
  styles: []
})
export class UploadComponent implements OnInit {

    isVisible = false;
    fileList: UploadFile[] = [];
  constructor(private message:NzMessageService,private workSer:WorkService) { }

  ngOnInit() {
  }

  showModal(){
    this.isVisible = true;
  }

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

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

    handleOk(){
        if(this.fileList.length == 0){
            this.message.warning("请选择文件");
            return false;
        }
        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);
                }
            }
        )
    }

}
