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

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

  errorMessage: String;
  constructor(
    private sysDataService: SysDataService,
    private sys005Service: Sys005Service,
    private router: Router
  ) {}

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

  getPageDataList(tableQueryData: TableQueryData): any {
    this.tableQueryData.CurrentPage = tableQueryData.Page
      ? tableQueryData.Page
      : 1;
    this.sysDataService.getPageDataList(tableQueryData).subscribe(res => {
      const result = res.json().Result;
      this.pageDataList = 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.getPageDataList(this.tableQueryData);
  }

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

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

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

  goEditPage(page) {
    this.sys005Service.selectedPage = page;
    this.router.navigate(["/sys/sys005-set-page"]);
  }
}
