import { SysDataService } from "../../../service/sys-data.service";
import { Component, OnInit } from "@angular/core";
import { Sys006Service } from "app/views/sys/sys006/sys006.service";
import { TableQueryData } from "../../../data/TableQueryData";
import { Router } from "@angular/router";
import swal from "sweetalert2";

@Component({
  selector: "app-sys006",
  templateUrl: "./sys006.component.html"
})
export class Sys006Component implements OnInit {
  groupDataList = [];
  loading = false;
  tableQueryData: TableQueryData;
  item: string;
  IsAsc = true;
  pageCount = 0;
  pageLinks = [];
  pageLinkMarginCount = 2;

  errorMessage: String;
  constructor(
    private sysDataService: SysDataService,
    private sys006Service: Sys006Service,
    private router: Router
  ) {}

  ngOnInit() {
    this.loading = true;
    this.tableQueryData = new TableQueryData();
    this.tableQueryData.CurrentPage = 0;
    this.getGroupDataList(this.tableQueryData);
  }

  getGroupDataList(tableQueryData: TableQueryData): any {
    this.tableQueryData.CurrentPage = tableQueryData.Page
      ? tableQueryData.Page
      : 1;
    this.sysDataService.getGroupData(tableQueryData).subscribe(res => {
      const result = res.json().Result;
      this.groupDataList = result.Data;
      this.loading = false;
      if (tableQueryData.Page === 1 || !tableQueryData.Page) {
        this.pageCount = result.PageCount;
        this.getPageLinks();
      }
    }, error => (this.errorMessage = <any>error));
  }

  getPageLinks() {
    this.pageLinks = [];
    let firstPageNumber =
      this.tableQueryData.CurrentPage - this.pageLinkMarginCount;
    let lastPageNumber =
      this.tableQueryData.CurrentPage + this.pageLinkMarginCount;
    if (firstPageNumber < 1) {
      firstPageNumber = 1;
    }
    if (lastPageNumber > this.pageCount) {
      lastPageNumber = this.pageCount;
    }
    if (firstPageNumber > 1) {
      const isCurrentPage = firstPageNumber === this.tableQueryData.CurrentPage;
      this.pageLinks.push({
        text: "第一頁",
        pageNo: 1,
        isCurrent: isCurrentPage
      });
    }
    for (let i = firstPageNumber; i <= lastPageNumber; i++) {
      const isCurrentPage = i === this.tableQueryData.CurrentPage;
      this.pageLinks.push({ text: i, pageNo: i, isCurrent: isCurrentPage });
    }
    if (lastPageNumber < this.pageCount) {
      const isCurrentPage = lastPageNumber === this.tableQueryData.CurrentPage;
      this.pageLinks.push({
        text: "最後頁(共 " + this.pageCount + " 頁)",
        pageNo: this.pageCount,
        isCurrent: isCurrentPage
      });
    }
  }

  onPageChange(e: Event, page) {
    e.preventDefault();
    this.tableQueryData.CurrentPage = page;
    this.tableQueryData.Page = this.tableQueryData.CurrentPage;
    this.getPageLinks();
    this.getGroupDataList(this.tableQueryData);
  }

  doSearch() {
    this.tableQueryData.Page = null;
    this.getGroupDataList(this.tableQueryData);
  }

  clearSearch() {
    this.tableQueryData.Term = null;
    this.getGroupDataList(this.tableQueryData);
  }
  order(item) {
    this.IsAsc = !this.IsAsc;
    this.item = item;
    this.tableQueryData.Order = item;
    this.tableQueryData.IsAsc = this.IsAsc.toString();
    this.getGroupDataList(this.tableQueryData);
  }

  deleteGroupData(id: string): any {
    swal({
      title: "是否要刪除?",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#70C2DA",
      cancelButtonColor: "#f86c6b",
      confirmButtonText: "是",
      cancelButtonText: "否"
    }).then(result => {
      if (result.value) {
        this.sysDataService.deleteGroupData(id).subscribe(res => {
          const result2 = res.json();
          if (result2.Success === true) {
            swal({
              type: "success",
              title: "刪除成功",
              showConfirmButton: true,
              confirmButtonColor: "#70C2DA"
            });
            this.groupDataList = this.groupDataList.filter(p => {
              return p.Id !== id;
            });
          } else {
            swal({
              type: "error",
              title: "刪除失敗",
              showConfirmButton: true
            });
          }
        });
      }
    });
  }

  goDetailGroup(group) {
    this.sys006Service.selectedGroup = group;
    this.router.navigate(["/sys/sys006-view-group"]);
  }

  goEditGroup(group) {
    this.sys006Service.selectedGroup = group;
    this.router.navigate(["/sys/sys006-set-group"]);
  }
}
