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

@Component({
  selector: "app-set002-create-reason",
  templateUrl: "./set002-create-reason.component.html"
})
export class Set002CreateReasonComponent implements OnInit {
  reasonData: ReasonData;
  fileToUpload: File = null;
  isLightNoExisted: boolean;
  errorMessage: string;
  loading = false;
  selectEventTypeId: any;
  constructor(private setDataService: SetDataService, private router: Router) {}

  ngOnInit() {
    this.reasonData = new ReasonData();
    this.getEventTypeId();
  }

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



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

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