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

@Component({
  selector: "app-sys006-create-group",
  templateUrl: "./sys006-create-group.component.html"
})
export class Sys006CreateGroupComponent implements OnInit {
  groupData: GroupData;

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

  ngOnInit() {
    this.groupData = new GroupData();
  }

  createNewGroup() {
    this.groupData.Id = 0;
    this.sysDataService.postGroupData(this.groupData).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/sys006"]);
          }
        });
      } else {
        swal({
          type: "error",
          title: "新增失敗",
          text: "請檢查您輸入的資訊...",
          showConfirmButton: true,
          confirmButtonColor: "#70C2DA"
        });
      }
    });
  }

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