import { Component, Inject, OnInit } from '@angular/core';
import { ContractsInfo } from '../../../../interfaces/personal-managment/intervention.interface';
import { TempFileInfo } from '../../../../interfaces/temp-file-info.interface';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { EncService } from '../../../../services/enc.service';
import { FunctionsService } from '../../../../services/functions.service';

@Component({
    selector: 'app-contract-details',
    templateUrl: './contract-details.component.html',
    styleUrl: './contract-details.component.css',
    standalone: false
})
export class ContractDetailsComponent implements OnInit {
  isLoading: boolean;
  hasError: boolean;
  errorStr: string;
  contract: ContractsInfo;
  documents: TempFileInfo[];

  constructor(
    @Inject(MAT_DIALOG_DATA) private _data: ContractsInfo,
    private _encService: EncService,
    private _functService: FunctionsService,
  ) {
    this.isLoading = true;
    this.hasError = false;
    this.errorStr = "";
    this.contract = this._data;
    this.documents = [];
  }

  ngOnInit(): void {
    this.init();
  }

  async init(){
    this.contract.CONTRACT_ID = await this._encService.decrypt(this.contract.CONTRACT_ID as string);
    this.contract.CONTRACT_ID = parseInt(this.contract.CONTRACT_ID);

    let docsArr = JSON.parse(this.contract.DOCUMENT);
    for(const doc of docsArr){
      this.documents.push({
        id: doc.id,
        name: doc.name,
        size: this._functService.formatBytes(doc.size),
      })
    }

    this.contract.WORK_ORDER_TYPE = this.contract.WORK_ORDER_TYPE.toLowerCase();
    this.contract.START_DATE = this._functService.orderDate(this.contract.START_DATE)!;
    this.contract.END_DATE = this._functService.orderDate(this.contract.END_DATE)!;
    this.contract.REGISTER_DATE = this._functService.orderDate(this.contract.REGISTER_DATE)!;
    this.contract.MODIFICATION_DATE = this.contract.MODIFICATION_DATE == null ? null : this._functService.orderDate(this.contract.MODIFICATION_DATE)!;
    
    this.isLoading = false;
  }
}
