import { LoginService } from "../../service/login.service";
import { Component, OnInit } from "@angular/core";
import { LoginData } from "../../data/LoginData";
import { Router } from "@angular/router";
import { SysDataService } from "../../service/sys-data.service";
import { HelperService } from "app/service/helper.service";
import swal from "sweetalert2";

@Component({
  templateUrl: "./login.component.html"
})
export class LoginComponent implements OnInit {
  loginData = new LoginData();
  errorMessage: String;
  loading = false;

  constructor(
    private loginService: LoginService,
    private helperService: HelperService,
    private sysDataService: SysDataService,
    private router: Router
  ) {}

  ngOnInit() {}

  doLogin() {
    this.postLoginForm();
  }

  keyupEnter() {
    this.postLoginForm();
  }

  postLoginForm(): void {
    this.loading = true;
    this.loginService.postLoginForm(this.loginData).subscribe(res => {
      const result = res.json();
      if (result.Success === true && result.Token != null) {
        this.loginService.token = result.Token;
        this.loginService.role = result.Role;
        this.loginService.id = result.Id;
        this.sysDataService
          .getAllPagePermission(result.Role)
          .subscribe(res2 => {
            this.loading = false;
            this.loginService.permissions = res2;
            if (typeof Storage !== "undefined") {
              localStorage.setItem("token", result.Token);
              localStorage.setItem("id", result.Id);
              localStorage.setItem("role", result.Role);
              localStorage.setItem("name", result.Name);
            } else {
              alert("此瀏覽器不支援Web Storage");
            }

            if (result.Message.includes("未更改密碼，請立即更改密碼")) {
              swal({
                type: "warning",
                title: "密碼到期提示",
                text:  result.Message,
                showConfirmButton: true,
                // timer: 1000,
                onOpen: () => {
                  // swal.showLoading();
                },
                onClose: () => {
                  this.router.navigate(["dashboard"]);
                }
              });

            } else if (result.Message.includes("請立即更改密碼")) {
              swal({
                type: "warning",
                title: "初始密碼到期提示",
                text:  result.Message,
                showConfirmButton: true,
                // timer: 1000,
                onOpen: () => {
                  // swal.showLoading();
                },
                onClose: () => {
                  localStorage.setItem("password", "1");
                  this.router.navigate(["dashboard"]);
                }
              });
            } else {
              this.router.navigate(["dashboard"]);
            }
          });
      } else {
        this.loading = false;
        swal({
          type: "error",
          title: "登入失敗",
          text:  result.Message,
          showConfirmButton: true
        });
      }
    }, error => (this.errorMessage = <any>error));
  }
}
