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

@Component({
  selector: "app-sys003",
  templateUrl: "./sys003.component.html"
})
export class Sys003Component implements OnInit {
  selectedUser: any;
  selectedUserId = +localStorage.getItem("id");
  term: string;
  page: number;
  loading = false;
  errorMessage: String;

  constructor(
    private sysDataService: SysDataService,
    private helperService: HelperService,
    private route: ActivatedRoute
  ) {}

  ngOnInit() {
    this.loading = true;
    this.getUserDatabyId(this.selectedUserId);
  }

  getUserDatabyId(id?: number): any {
    this.sysDataService.getUserbyId(id).subscribe(res => {
      this.selectedUser = res.json();
      this.loading = false;
    }, error => (this.errorMessage = <any>error));
  }

  doEditUser() {
    this.sysDataService.putUserData(this.selectedUser).subscribe(res => {
      const result = res.json();
      if (result.Success === true) {
        swal({
          type: "success",
          title: "修改成功",
          showConfirmButton: true,
          confirmButtonColor: "#70C2DA"
        });
      } else {
        swal({
          type: "error",
          title: "修改失敗",
          text: "請檢查您輸入的資訊...",
          showConfirmButton: true,
          confirmButtonColor: "#70C2DA"
        });
      }
    });
  }
}
