import { Component, OnInit, ViewChild, ViewContainerRef } from '@angular/core';
import { Router } from '@angular/router';
import { ActivatedRoute } from '@angular/router';
import { Partner } from './partner';
import { PartnerService } from './partner.service';
import { FileUploader, FileItem, ParsedResponseHeaders } from 'ng2-file-upload';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';
// const URL = '/api/';
const URL = 'http://localhost:9000/partner/upload';
declare var require: any;
@Component({
selector: 'partner-form',
templateUrl: './partner-form.component.html',
styleUrls: ['./partner-form.css']
})
export class PartnerFormComponent implements OnInit {
public uploader:FileUploader = new FileUploader({url: URL});
public hasBaseDropZoneOver:boolean = false;
public hasAnotherDropZoneOver:boolean = false;
public fileOverBase(e:any):void {
this.hasBaseDropZoneOver = e;
}
public fileOverAnother(e:any):void {
this.hasAnotherDropZoneOver = e;
}
id: string;
partner: Partner;
filePath: string;
partnerLogoUuid: string;
constructor(private router: Router, private route: ActivatedRoute, private partnerService: PartnerService,
public toastr: ToastsManager, vcr: ViewContainerRef) {
debugger
route.params.subscribe(params => {this.id = params['id'];});
this.partner = new Partner();
this.toastr.setRootViewContainerRef(vcr);
}
/**
* 初始化方法
*/
ngOnInit() {
debugger
this.setPartner();
this.uploader = new FileUploader({
url: URL,
method: "POST",
itemAlias: "myuploader"
});
this.uploader.onAfterAddingFile = (file) => { file.withCredentials = false; };
this.uploader.onSuccessItem = this.successItem.bind(this);
}
setPartner(){
if (this.id != '0') {
this.partnerService.getPartner(this.id).subscribe(partner => {
debugger
this.partner = partner;
});
} else {
this.partner = new Partner();
}
}
successItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders):any{
// 上传文件成功
if (status == 200) {
// 上传文件后获取服务器返回的数据
let tempRes = JSON.parse(response);
this.fileName = tempRes.fileName;
this.filePath = tempRes.filePath;
this.toastr.success('上传成功!');
}else {
// 上传文件后获取服务器返回的数据错误
}
console.info(response+" for "+item.file.name + " status " + status);
}
updatePartner(){
debugger
if (this.fileName != null) {
this.partner.partnerLogo = this.fileName;
this.partner.partnerLogoUuid = this.filePath;
}
if (this.partner.partnerId != null) {
this.partnerService.update(this.partner).subscribe(data => {
});
this.toastr.success('修改保存成功!');
} else {
this.partnerService.add(this.partner).subscribe(data => {
});
this.toastr.success('新增保存成功!');
}
//this.router.navigate(['/business/partner']);
}
}