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-sys005-set-page",
  templateUrl: "./sys005-set-page.component.html"
})
export class Sys005SetPageComponent implements OnInit {
  errorMessage: String;
  data: any;
  selectedPage: any;
  roleId: number;

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

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

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

  doEditPageData() {
    this.sysDataService.putPageData(this.selectedPage).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/sys005"]);
          }
        });
      } else {
        swal({
          type: "error",
          title: "修改失敗",
          text: "請檢查您輸入的資訊...",
          showConfirmButton: true,
          confirmButtonColor: "#70C2DA"
        });
      }
    });
  }
}
