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";

@Component({
  selector: "app-sys006-set-group",
  templateUrl: "./sys006-set-group.component.html"
})
export class Sys006SetGroupComponent implements OnInit {
  errorMessage: String;
  data: any;
  selectedGroup: any;
  groupId: number;

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

  ngOnInit() {
    this.route.data.subscribe((data: { selectedGroup: any }) => {
      this.selectedGroup = data.selectedGroup;
    });
  }

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

  doEditGroupData() {
    this.sysDataService.putGroupData(this.selectedGroup).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"
        });
      }
    });
  }
}
