import {
  Component,
  OnInit,
  Input,
  ViewChild,
  ComponentFactoryResolver
} from "@angular/core";
import { Router, ActivatedRoute } from "@angular/router";
import { InvestmentProposalForm } from "@app/models/investment-proposal-form";
import { FormInvestmentProposalService } from "@app/services/form-investment-proposal.service";
import { Instrument } from "@app/models/instrument";
import { InvestmentProposalWorkflowService } from "@app/services/investment-proposal-workflow.service";
import { InstrumentDirective } from "../instrument/instrument.directive";
import { InstrumentComponent } from "../instrument/instrument.component";
import { InstrumentsService } from "@app/services/instruments.service";
import { InvestmentsService } from "@app/services/investments.service";
import { environment } from "@environments/environment";

@Component({
  selector: "app-investment-proposal-changes",
  templateUrl: "./change-history.component.html",
  styleUrls: ["./change-history.component.scss"]
})
export class ChangeHistoryComponent implements OnInit {
  title = "Histórico de la propuesta";
  @Input() ads: Instrument[];
  @Input() formData: InvestmentProposalForm;
  @ViewChild(InstrumentDirective, { static: true })
  adHost: InstrumentDirective;
  investmentProposalID: string;
  logs: any;
  investmentCode: any;
  fileDownload: string;

  constructor(
    private router: Router,

    private formDataService: FormInvestmentProposalService,
    private componentFactoryResolver: ComponentFactoryResolver,
    private instrumentService: InvestmentProposalWorkflowService,
    private loadInstrumentsService: InstrumentsService,
    private route: ActivatedRoute,
    private investmentService: InvestmentsService
  ) {}

  ngOnInit() {
    this.route.params.subscribe(params => {
      this.investmentProposalID = params["id"];
    });
    if (this.investmentProposalID == undefined)
      this.investmentProposalID = this.route.snapshot.queryParamMap.get("id");

    this.investmentService
      .getProposalInvestment(this.investmentProposalID)
      .subscribe(
        res => {
          this.fileDownload = environment.productionMediaUrl;
          this.investmentCode = res["result"]["codigo_inversion"];
        },
        err => {}
      );

    this.investmentService
      .getInvestmentProposalLogs(this.investmentProposalID)
      .subscribe(
        res => {
          this.logs = res["result"];
        },
        error => {}
      );
  }
}
