import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs/index';
import {environment} from '../../../environments/environment';
import {map} from 'rxjs/internal/operators';

@Injectable()
export class OrderDataService {

  constructor(private http: HttpClient) {}

  searchOrders(options: any): Observable<any> {
    const qstring = environment.api + '/order/search';
    return this.http.post(qstring, options)
      .pipe(map((response: any) => {
        return response.data;
      }));
  }

  getOrderForLookup(orderId: number, orderDate: any): Observable<any> {
    const qstring = environment.api + '/order/lookup/' + orderId + '/' + orderDate;
    return this.http.get(qstring)
      .pipe(map((response: any) => {
        return response;
      }));
  }

  getOrderForEdit(orderId: number, orderDate: any): Observable<any> {
    const qstring = environment.api + '/order/' + orderId + '/' + orderDate;
    return this.http.get(qstring)
      .pipe(map((response: any) => {
        return response;
      }));
  }

  getOrderRecords(keys: any[]): Observable<any> {
    const qstring = environment.api + '/order/lookup-many';
    return this.http.post(qstring, keys)
      .pipe(map((response: any) => {
        return response.data as any[];
      }));
  }

  orderMarkFull(options: any): Observable<any> {
    const qstring = environment.api + '/order/mark-full';
    return this.http.post(qstring, options)
      .pipe(map((response: any) => {
        return response;
      }));
  }

  orderMarkApproved(options: any): Observable<any> {
    const qstring = environment.api + '/order/mark-approved';
    return this.http.post(qstring, options)
      .pipe(map((response: any) => {
        return response;
      }));
  }

  setOrderNom(options: any): Observable<any> {
    const qstring = environment.api + '/order/set-number';
    return this.http.post(qstring, options)
      .pipe(map((response: any) => {
        return response;
      }));
  }

  getLastStator(orderId: number, orderDate: any): Observable<any> {
    const qstring = environment.api + '/order/last-stator/' + orderId + '/' + orderDate;
    return this.http.get(qstring)
      .pipe(map((response: any) => {
        return response.data;
      }));
  }

}
