import { Component, OnInit } from "@angular/core";
import { Router } from "@angular/router";
import { SetDataService } from "app/service/set-data.service";
import swal from "sweetalert2";
import { CompanyInformationData } from "../../../data/CompanyInformationData";

@Component({
  selector: "app-set003-create-vendor",
  templateUrl: "./set003-create-vendor.component.html"
})
export class Set003CreateVendorComponent implements OnInit {
  errorMessage: string;
  loading = false;

  companyInformationData: CompanyInformationData;
  constructor(private setDataService: SetDataService, private router: Router) {}

  ngOnInit() {
    this.companyInformationData = new CompanyInformationData();
  }

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

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