import { Component, OnInit } from "@angular/core";
import { FormControl } from "@angular/forms";
import { Observable } from "rxjs/Observable";
import { Subject } from "rxjs/Subject";
import { of } from "rxjs/observable/of";
import { debounceTime, distinctUntilChanged, switchMap } from "rxjs/operators";
import { Router } from "@angular/router";
import { timeInterval } from "rxjs/operators/timeInterval";
import { HelperService } from "../../service/helper.service";
import { MroDataService } from "../../service/mro-data.service";
import { SetDataService } from "app/service/set-data.service";
import { RepairData } from "../../data/RepairData";

import swal from "sweetalert2";
import { SysDataService } from "app/service/sys-data.service";

@Component({
  selector: "app-mro000-result",
  templateUrl: "./mro000-result.component.html"
})
export class Mro000ResultComponent implements OnInit {
  selectInfo: any[];
  selectVillage: any;
  selectLightNo$: Observable<any[]>;
  searchTerms = new Subject<string>();
  repairData: RepairData;
  errorMessage: string;
  loading = false;
  isLightNoExisted = true;
  searchFinish: boolean;
  result: any;
  companyEmail: any[];
  siteInformationList: any;
  constructor(
    private mroDataService: MroDataService,
    private setDataService: SetDataService,
    private helperService: HelperService,
    private sysDataService: SysDataService,
    private router: Router
  ) {}

  ngOnInit() {
    this.repairData = new RepairData();
    this.repairData.questionType = 0;
    this.getCompanyEmail();
    this.siteInformationList = [];
    this.getSiteInformationAll();
  }


  getSiteInformationAll(): any {
    this.sysDataService.getSiteInformationAll().subscribe(res => {
      const result = res.json().Result;
      this.siteInformationList = result;
    }, error => (this.errorMessage = <any>error));
  }


  backPreviousPage() {
    window.history.go(-1);
  }


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






  postRepairFormData(): void {
    this.loading = true;

    this.mroDataService.postRepairForm(this.repairData).subscribe(res => {
      const result = res.json();
      if (result.Success === true) {
        this.loading = false;
        swal({
          type: "success",
          title: "新增成功，感謝您的回報",
          showConfirmButton: false,
          timer: 1000,
          onOpen: () => {
            swal.showLoading();
          },
          onClose: () => {
            this.router.navigate(["login"]);
          }
        });
      } else {
        this.loading = false;
        swal({
          type: "error",
          title: "新增失敗",
          text: "請檢查您輸入的資訊...",
          showConfirmButton: true
        });
      }
    });
  }




}
