import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { NzUploadModule } from 'ng-zorro-antd/upload';
import { CommonCompModule } from '../../services/common.modules';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzUploadChangeParam, NzUploadFile } from 'ng-zorro-antd/upload';
@Component({
  selector: 'app-comp-upload',
  standalone: true,
  imports: [NzUploadModule, CommonCompModule],
  templateUrl: './comp-upload.component.html',
  styleUrls: ['./comp-upload.component.scss'],
})
export class CompUploadComponent implements OnInit {
  @Input('title') title:string = '上传文件'
  @Input('files') files:Array<any> = []
  @Output() change: EventEmitter<any> = new EventEmitter<any>();

  fileList:any = [
    // {
    //   uid: '1',
    //   name: 'xxx.png',
    //   status: 'done',
    //   response: 'Server Error 500', 
    //   url: 'http://www.baidu.com/xxx.png'
    // },
  ];
  constructor(private msg: NzMessageService) {}

  ngOnInit() {
    this.fileList = this.files.map((item:any)=> {
      return {
        url: item
      }
    })
  }
  handleChange(info: NzUploadChangeParam): void {
    console.log(info);
    if (info.file.status !== 'uploading') {
      console.log(info.file, info.fileList);
    }
    if (info.file.status === 'done') {
      this.msg.success(`${info.file.name} file uploaded successfully`);
    } else if (info.file.status === 'error') {
      this.msg.error(`${info.file.name} file upload failed.`);
    }
    this.change.emit(info.fileList)
  }
}
