import { Component ,OnInit } from '@angular/core';
import { ActivatedRoute, Route, Router } from '@angular/router';
import { apiUrl } from 'src/app/_providers/_resources/api-url.properties';
import { MasterService } from 'src/app/_providers/_services/master.service';
import { UiService } from 'src/app/_providers/_services/ui.service';

@Component({
  selector: 'app-document-upload',
  templateUrl: './document-upload.component.html',
  styleUrls: ['./document-upload.component.css']
})
export class DocumentUploadComponent  implements OnInit {

  boqFile ={fileName:"",filePath:""}
  revisedDocumentData={tenderId : null , revisedDocument : '',requotedOrder : ''}
  docSize: any;
  tenderId: number;
  order: any;

  constructor(private _masterService:MasterService,
              private route:ActivatedRoute,
              private router:Router,
              private _ui:UiService){
    this.tenderId = +this.route.snapshot.paramMap.get('id');
    this.order = +this.route.snapshot.paramMap.get('order');
 
  }
  ngOnInit(): void {

     
  }


  removeBoqfile(){
    this._masterService.deleteMasterData(apiUrl.quotedTender,this.boqFile.fileName).then(res=>{
      this.boqFile.fileName = null
      this.boqFile.filePath = null
      })
  }

  uploadBoqFiles(event){ 
    let file: File | undefined = event.target.files[0];
    if (!file) {
      return;
    }

    const fd = new FormData();
    
  fd.append('file', file );
   
  this._masterService.saveMasterData(apiUrl.quotedTender,fd).then(res=>{
    this.boqFile.fileName = res.fileName; 
    this.boqFile.filePath = res.filePath;
    this.docSize = res.fileSizeKB;
  
  }) 

   
  }

  navigateTo(destination: any) {
    this.router.navigate([destination])
  }

  close(){
this.navigateTo('dashboard')
  }

  saveBoqFile(){
   
    
    this.revisedDocumentData.revisedDocument=this.boqFile.fileName;
    this.revisedDocumentData.tenderId=this.tenderId
    this.revisedDocumentData.requotedOrder = this.order;

    if(!this.revisedDocumentData || this.revisedDocumentData.revisedDocument=='' || this.revisedDocumentData.revisedDocument==null ){
      this._ui.toastMessage('Please select any documents ')
      return true;
    }  

    this._masterService.saveMasterData(apiUrl.revisedFileUpload,this.revisedDocumentData).then(res=>{
      
      this._ui.toastMessage('Document uploaded successfully ')
      this.navigateTo('dashboard')


    },err=>{
      this._ui.toastMessage(err.error.message)
    })
  }
}


