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

@Component({
  selector: "app-set001-create-site",
  templateUrl: "./set001-create-site.component.html"
})
export class Set001CreateSiteComponent implements OnInit {
  fileToUpload: File = null;
  isLightNoExisted: boolean;
  errorMessage: string;
  loading = false;
  EventTypeList: any;
  companyAllInfo: any;

  siteInformationData: SiteInformationData;
  constructor(private setDataService: SetDataService, private router: Router) {}

  ngOnInit() {
    this.siteInformationData = new SiteInformationData();
    this.getEventTypeId();
    this.EventTypeList = [];
    this.siteInformationData.Country = "桃園市";
    this.companyAllInfo = [];
    this.getCompanyAllInfo();
  }

  getCompanyAllInfo() {
    this.setDataService.getCompanyAllInfo().subscribe(res => {
      const result = res.json().Result;
      this.companyAllInfo = result;
    }, error => (this.errorMessage = <any>error));
  }

  createSiteInformationData() {
    this.setDataService.postSiteInformations(this.siteInformationData).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/set001"]);
          }
        });
      } else {
        this.loading = false;
        swal({
          type: "error",
          title: "新增失敗",
          text: "請檢查您輸入的資訊...",
          showConfirmButton: true
        });
      }
    });
  }



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


  getEventTypeId() {
    this.setDataService.getEventTypeId().subscribe(res => {
      const result = res.json().Result;
      this.EventTypeList = result;
    }, error => (this.errorMessage = <any>error));
  }

}
