import { Observable } from "rxjs/Observable";
import { Injectable } from "@angular/core";
import { HttpService } from "app/service/http.service";
import { Http, Headers, Response, RequestOptions } from "@angular/http";
import "rxjs/add/operator/map";
import "rxjs/add/operator/catch";
import { RepairData } from "app/data/RepairData";
import { TableQueryData } from "../data/TableQueryData";

@Injectable()
export class MroDataService {
  constructor(private httpService: HttpService) {}


  getAPITableDataList(tableQueryData: TableQueryData, tycgTableName: string, method: string): Observable<any> {
    let query =
        "?tycgTableName=" + (tycgTableName ? tycgTableName : "") + "&";
        query += "term=" + (tableQueryData.Term ? tableQueryData.Term : "") + "&";
        query += "page=" + (tableQueryData.Page ? tableQueryData.Page : "") + "&";
        query += "order=" + (tableQueryData.Order ? tableQueryData.Order : "") + "&";
        query += "isAsc=" + (tableQueryData.IsAsc ? tableQueryData.IsAsc : "") + "&";
        query += "method=" + (method ? method : "");
    return (
        this.httpService
        .get("/report/tycg" + query)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }


  postRepairForm(repairData: RepairData): Observable<any> {
    return (
      this.httpService
        .post("/Repair", repairData)
        // .map(this.extractData)
        .catch(this.handleErrorObservable)
    );
  }

  getTycgTableName(): Observable<any> {
    return this.httpService
      .get("/report/tycg/tableName")
      // .map(this.extractData)
      .catch(this.handleErrorObservable);
  }


  private extractData(res: Response) {
    const body = res.json();
    return body.Result || [];
  }
  private handleErrorObservable(error: Response | any) {
    console.error(error.message || error);
    return Observable.throw(error.message || error);
  }
}
