import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { MroDataService } from 'app/service/mro-data.service';
import { SetDataService } from 'app/service/set-data.service';
import { ActivatedRoute } from '@angular/router';
import { IMyDpOptions, IMyDateModel } from 'mydatepicker';
import { TableQueryData } from 'app/data/TableQueryData';
import { ApiTableData } from 'app/data/ApiTableData';
import swal from 'sweetalert2';

@Component({
  selector: 'app-mro007',
  templateUrl: './mro007.component.html',
})
export class Mro007Component implements OnInit {

  loading = false;
  errorMessage: String;
  tableQueryData: TableQueryData;
  apiDataList = [];
  currentPage = 0;
  pageCount = 0;
  pageLinks = [];
  pageLinkMarginCount = 2;
  tycgTableName: string;
  apiTableData: ApiTableData;
  IsAsc = true;
  item: string;
  fileName: string;
  urlExport: any;
  tycgTableNameList: any[];

  constructor(private mroDataService: MroDataService) {}

  ngOnInit() {

    this.tycgTableName = "";
    this.tableQueryData = new TableQueryData();
    this.tableQueryData.CurrentPage = 0;
    this.fileName = "";
    this.getTycgTableName();

  }

  getTycgTableName() {
    this.mroDataService.getTycgTableName().subscribe(res => {
      const result = res.json().Result;
      this.tycgTableNameList = result;
    }, error => (this.errorMessage = <any>error));
  }


  getAPITableDataList(tableQueryData: TableQueryData, tycgTableName: string, method: string): any {
    this.loading = true;
    this.tableQueryData.CurrentPage = tableQueryData.Page
      ? tableQueryData.Page
      : 1;
    this.mroDataService.getAPITableDataList(tableQueryData, tycgTableName, method).subscribe(res => {
      if (method === '0') {
        const result = res.json().Result;
        this.apiDataList = result.Data;
        this.loading = false;
      if (tableQueryData.Page === 1 || !tableQueryData.Page) {
        this.pageCount = result.PageCount;
        this.getPageLinks();
      }
      } else {
        this.urlExport = res;
        const link = document.createElement("a");
          link.href = this.urlExport._body;
          this.fileName = this.urlExport._body.substring(this.urlExport._body.lastIndexOf('/') + 1);
          link.download = this.fileName;
          link.click();
          this.loading = false;
      }
    }, error => (this.errorMessage = <any>error));
  }

  onPageChange(e: Event, page) {
    e.preventDefault();
    this.tableQueryData.CurrentPage = page;
    this.tableQueryData.Page = this.tableQueryData.CurrentPage;
    this.getPageLinks();
    this.getAPITableDataList(this.tableQueryData, this.tycgTableName, "0");
  }

  doSearch() {
    this.tableQueryData.Page = null;
    if (this.tycgTableName === null || this.tycgTableName === "" || this.tycgTableName === "undefined") {
      swal({
        type: "error",
        title: "API 表格名稱錯誤",
        text: "請檢查表格名稱...",
        showConfirmButton: true
      });
      return;
    }
    this.getAPITableDataList(this.tableQueryData, this.tycgTableName, "0");
  }

  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
      });
    }
  }

  order(item) {
    this.IsAsc = !this.IsAsc;
    this.item = item;
    this.tableQueryData.Order = item;
    this.tableQueryData.IsAsc = this.IsAsc.toString();
    this.getAPITableDataList(this.tableQueryData, this.tycgTableName, "0");
  }



  doSearchExport() {
    this.tableQueryData.Page = null;
    if (this.tycgTableName === null || this.tycgTableName === "" || this.tycgTableName === "undefined") {
      swal({
        type: "error",
        title: "API 表格名稱錯誤",
        text: "請檢查表格名稱...",
        showConfirmButton: true
      });
      return;
    }
    this.getAPITableDataList(this.tableQueryData, this.tycgTableName, "1");
  }



}
