import { Component, OnInit } from "@angular/core";
import { SysDataService } from "../../../service/sys-data.service";
import { Http, Response } from "@angular/http";
import { Headers, RequestOptions } from "@angular/http";
import { Sys004Service } from "app/views/sys/sys004/sys004.service";
import { RoleData } from "app/Data/RoleData";
import { TableQueryData } from "../../../data/TableQueryData";
import { Router } from "@angular/router";

@Component({
  selector: "app-sys004",
  templateUrl: "./sys004.component.html"
})
export class Sys004Component implements OnInit {
  roleDataList = [];
  tableQueryData: TableQueryData;
  IsAsc = true;
  item: string;
  pageCount = 0;
  loading = false;
  pageLinks = [];
  pageLinkMarginCount = 2;
  errorMessage: String;
  constructor(
    private sysDataService: SysDataService,
    private sys004Service: Sys004Service,
    private router: Router
  ) {}

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

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

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

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

  goDetail(item) {
    this.sys004Service.selectedRole = item;
    this.router.navigate(["/sys/sys004-set-role"]);
  }
}
