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

@Component({
  selector: "app-sys004-create-role",
  templateUrl: "./sys004-create-role.component.html"
})
export class Sys004CreateRoleComponent implements OnInit {
  roleData: RoleData;

  errorMessage: string;
  data: any;
  constructor(private sysDataService: SysDataService, private router: Router) {}

  ngOnInit() {
    this.roleData = new RoleData();
  }

  createNewRole() {
    this.sysDataService.postRoleDataList(this.roleData).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(["sys/sys004"]);
          }
        });
      } else {
        swal({
          type: "error",
          title: "新增失敗",
          text: "請檢查您輸入的資訊...",
          showConfirmButton: true,
          confirmButtonColor: "#70C2DA"
        });
      }
    });
  }

  backPreviousPage() {
    this.router.navigateByUrl("/sys/sys004");
  }
}
