import { LoginService } from "../../service/login.service";
import { Component, OnInit } from "@angular/core";
import { Router } from "@angular/router";
import { SysDataService } from "../../service/sys-data.service";
import { NewPwdMailData } from "../../data/NewPwdMailData";
import swal from "sweetalert2";
import { Validators } from "@angular/forms";

@Component({
  templateUrl: "./forgetPwd.component.html"
})
export class ForgetPwdComponent implements OnInit {
  newPwdMailData = new NewPwdMailData();
  errorMessage: String;
  constructor(private sysDataService: SysDataService, private router: Router) {}

  ngOnInit() {}

  doLogin() {
    this.sendNewPwdMail();
  }

  sendNewPwdMail() {
    this.sysDataService.sendNewPwdMail(this.newPwdMailData).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(["/login"]);
          }
        });
      } else {
        swal({
          type: "error",
          title: "查無使用者",
          text: "請檢查您輸入的使用者資訊...",
          showConfirmButton: true,
          confirmButtonColor: "#70C2DA"
        });
      }
    });
  }
}
