import { SetDataService } from "../../../service/set-data.service";
import { Router } from "@angular/router";
import swal from "sweetalert2";

import { Component, OnInit } from "@angular/core";
import { ActivatedRoute } from "@angular/router";
import { CompanyInformationData } from "app/data/CompanyInformationData";

@Component({
  selector: "app-set003-set-vendor",
  templateUrl: "./set003-set-vendor.component.html"
})
export class Set003SetVendorComponent implements OnInit {
  errorMessage: String;
  data: any;
  companyInformationData: CompanyInformationData;

  selectedItem: any;
  itemId: number;

  constructor(
    private setDataService: SetDataService,
    private route: ActivatedRoute,
    private router: Router
  ) {}

  ngOnInit() {
    this.companyInformationData = new CompanyInformationData();
    this.route.data.subscribe((data: { selectedItem: any }) => {
      this.selectedItem = data.selectedItem;
    });
  }

  doEditcompanyInformation() {
    this.setDataService.putCompanyInformationData(this.selectedItem).subscribe(res => {
      const result = res.json();
      if (result.Success === true) {
        swal({
          type: "success",
          title: "修改成功",
          text: "轉跳回檢視頁面...",
          showConfirmButton: false,
          timer: 1000,
          onOpen: () => {
            swal.showLoading();
          },
          onClose: () => {
            this.router.navigate(["set/set003"]);
          }
        });
      } else {
        swal({
          type: "error",
          title: "修改失敗",
          text: "請檢查您輸入的資訊...",
          showConfirmButton: true
        });
      }
    });
  }

  backPreviousPage() {
    this.router.navigateByUrl("/set/set003");
  }
}
