import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { AuthenticationService } from 'src/app/hmg-common/services/authentication/authentication.service';
import { ConnectorService } from 'src/app/hmg-common/services/connector/connector.service';
import { MowadhafiService } from 'src/app/mowadhafi/mowadhafi.service';

@Injectable({
  providedIn: 'root'
})
export class ItemForSaleService {
  public static getItemForSale = 'Services/COCWS.svc/REST/Mohemm_ITG_GetItemForSale';
  public static getItemForSaleByEmployee = 'Services/COCWS.svc/REST/Mohemm_ITG_GetItemForSaleByEmployee';
  public static getCategories = 'Services/COCWS.svc/REST/Mohemm_ITG_GetItemSaleCategory';
  public static createItemForSale = 'Services/COCWS.svc/REST/Mohemm_ITG_AddItemForSaleMobile';
  public static updateItemForSale = 'Services/COCWS.svc/REST/Mohemm_ITG_UpdateItemForSale';
  public static getRegion = 'Services/COCWS.svc/REST/Mohemm_ITG_GetRegion';
  public static ITEMS_SELECTED = 'item-selected';
  public static CATEGORIES = 'categories';
  public static EDIT_ITEMS = 'edit-items';
  constructor(
    public con: ConnectorService,
    private authService: AuthenticationService,
    private http: HttpClient
  ) { }
  public getItems(request: any, onError?: any, errorLabel?: string) {
    this.authService.authenticateRequest(request);
    request.EmployeeNumber = request.UserName;
    request.ItgPageNo = request.pageNo;
    request.ItgPageSize = 10;
    request.ItgStatus = "Approved";
    return this.con.postNoLoad(ItemForSaleService.getItemForSale, request, onError, errorLabel);
  }

  public getCategories(request: any, onError?: any, errorLabel?: string) {
    this.authService.authenticateRequest(request);
    request.EmployeeNumber = request.UserName;

    return this.con.postNoLoad(ItemForSaleService.getCategories, request, onError, errorLabel);
  }
  parser(response: any) {
    try {
      var result = JSON.parse(response.Mohemm_ITG_ResponseItem);
      if (result.result)
        return result.result.data
      else
        return {}
    } catch (res) {
      console.log(res);
    }
  }

  getItemsByEmployee(request: any, onError?: any, errorLabel?: string) {
    this.authService.authenticateRequest(request);
    request.EmployeeNumber = request.UserName;
    request.ItgEmployeeNumber = request.UserName; //request.UserName;
    return this.con.post(ItemForSaleService.getItemForSaleByEmployee, request, onError, errorLabel);
  }
  getRegion(request: any, onError?: any, errorLabel?: string) {
    this.authService.authenticateRequest(request);
    request.EmployeeNumber = request.UserName;
    request.ItgEmployeeNumber = request.UserName; //request.UserName;
    return this.con.post(ItemForSaleService.getRegion, request, onError, errorLabel);
  }

  createItemForSale(itemRequest: any, onError?: any, errorLabel?: string) {
    const request = itemRequest;
    this.authService.authenticateRequest(request);
    request.employeeNumber = request.P_USER_NAME;
    //return this.http.post(ConnectorService.host + ItemForSaleService.createItemForSale, itemRequest, {
    return this.con.post(ItemForSaleService.createItemForSale, request, onError, errorLabel);
    //}    
    //)
  }
  updateItemForSale(itemRequest: any, onError?: any, errorLabel?: string) {
    const request = itemRequest;
    this.authService.authenticateRequest(request);
    request.employeeNumber = request.P_USER_NAME;
    return this.http.post(ConnectorService.host + ItemForSaleService.updateItemForSale, itemRequest, {

    }
    )
  }
}
