import { AfterViewInit, Component, OnInit, ViewChildren } from "@angular/core";
import { DataTableDirective } from "angular-datatables";
import { Subject } from "rxjs/Subject";
import { Http, Response } from "@angular/http";
import { TableQueryData } from "../../../data/TableQueryData";
import { SetDataService } from "app/service/set-data.service";
import { Set003Service } from "app/views/set/set003/set003.service";
import { Router } from "@angular/router";
import { ActivatedRoute } from "@angular/router";
import { HelperService } from "app/service/helper.service";
import swal from "sweetalert2";
import { CompanyInformationData } from "../../../data/CompanyInformationData"

@Component({
  selector: "app-set003",
  templateUrl: "./set003.component.html"
})
export class Set003Component implements OnInit {

  tableQueryData: TableQueryData;
  IsAsc = true;
  status = false;
  loginRole = localStorage.getItem("role");
  setRole = "1";
  loading = false;
  pageCount = 0;
  pageLinkMarginCount = 2;
  pageLinks = [];
  item: string;
  errorMessage: string;

  companyInformationData: CompanyInformationData;
  companyInformatioPageList = [];
  constructor(
    private setDataService: SetDataService,
    private set003Service: Set003Service,
    private helperService: HelperService,
    private route: ActivatedRoute,
    private router: Router,
    private http: Http
  ) {}

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

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

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

  clearSearch() {
    this.tableQueryData.Term = null;
    this.getCompanyInformation(this.tableQueryData);
  }

  order(item) {
    this.IsAsc = !this.IsAsc;
    this.item = item;
    this.tableQueryData.Order = item;
    this.tableQueryData.IsAsc = this.IsAsc.toString();
    this.getCompanyInformation(this.tableQueryData);
  }

  deleteCompanyInformationData(id): any {
    swal({
      title: "是否要刪除?",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#70C2DA",
      cancelButtonColor: "#f86c6b",
      confirmButtonText: "是",
      cancelButtonText: "否"
    }).then(result => {
      if (result.value) {
        this.setDataService.deleteCompanyInformationData(id).subscribe(res => {
          const result2 = res.json();
          if (result2.Success === true) {
            this.companyInformatioPageList = this.companyInformatioPageList.filter(u => {
              return u.Id !== id;
            });
            this.getCompanyInformation(this.tableQueryData);
            this.getPageLinks();
            swal({
              type: "success",
              title: "刪除成功",
              showConfirmButton: true,
              confirmButtonColor: "#70C2DA",
              timer: 1000,
              onOpen: () => {
                swal.showLoading();
              },
              onClose: () => {}
            });
          } else {
            swal({
              type: "error",
              title: "刪除失敗",
              showConfirmButton: true,
              confirmButtonColor: "#70C2DA"
            });
          }
        });
      }
    });
  }

  goDetail(item) {
    this.set003Service.selectedItem = item;
    this.router.navigate(["set/set003-view-vendor"]);
  }

  goEdit(item) {
    this.set003Service.selectedItem = item;
    this.router.navigate(["set/set003-set-vendor"]);
  }

}
