const URL = "/one-api/mockup/purchase/requester/";

export async function search(payload) {
  try {
    var response = await axios.post(URL + "PurchaseRequestPersediaan/search", payload);
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}

export async function searchDetail(payload) {
  try {
    var response = await axios.post(
      URL + "PurchaseRequestPersediaan/searchDetail",
      payload
    );
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}

export async function getRegional(payload) {
  try {
    var response = await axios.post(URL + "PurchaseRequestPersediaan/getRegionalBranchByUser", payload);
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}

export async function getBranch(payload) {
  try {
    var response = await axios.post(URL + "PurchaseRequestPersediaan/getBranch", payload);
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}

export async function saveRequest(payload) {
  try {
    var response = await axios.post(
      URL + "PurchaseRequestPersediaan/saveRequest",
      payload
    );
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}

export async function updateRequest(payload) {
  try {
    var response = await axios.post(
      URL + "PurchaseRequestPersediaan/updateRequest",
      payload
    );
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}

export async function deleteRequest(payload) {
  try {
    var response = await axios.post(
      URL + "PurchaseRequestPersediaan/deleteRequest",
      payload
    );
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}

export async function saveDetail(payload) {
  try {
    var response = await axios.post(
      URL + "PurchaseRequestPersediaan/saveDetail",
      payload
    );
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}
export async function updateDetail(payload) {
  try {
    var response = await axios.post(
      URL + "PurchaseRequestPersediaan/updateDetail",
      payload
    );
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}
export async function deleteDetail(payload) {
  try {
    var response = await axios.post(
      URL + "PurchaseRequestPersediaan/deleteDetail",
      payload
    );
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}

export async function orderRequest(payload) {
  try {
    var response = await axios.post(
      URL + "PurchaseRequestPersediaan/orderRequest",
      payload
    );
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText,
      };
    }

    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message,
    };
  }
}

export async function getUnit(token, prm) {
  try {
      var resp = await axios.post(URL + 'PurchaseRequestPersediaan/getUnit', {
          token: token,
          search: prm
      });
      if (resp.status != 200) {
          return {
              status: "ERR",
              message: resp.statusText
          };
      }
      let data = resp.data;
      return data;
  } catch (e) {
      return {
          status: "ERR",
          message: e.message
      };
  }
}


export async function getDivisionByUser(token) {
  try {
      var resp = await axios.post(URL + 'PurchaseRequestPersediaan/getDivisionByUser', {
          token: token
      });
      if (resp.status != 200) {
          return {
              status: "ERR",
              message: resp.statusText
          };
      }
      let data = resp.data;
      return data;
  } catch (e) {
      return {
          status: "ERR",
          message: e.message
      };
  }
}


export async function getItems(payload) {
  try {
      var resp = await axios.post(URL + 'PurchaseRequestPersediaan/getItems', payload);
      if (resp.status != 200) {
          return {
              status: "ERR",
              message: resp.statusText
          };
      }
      let data = resp.data;
      return data;
  } catch (e) {
      return {
          status: "ERR",
          message: e.message
      };
  }
}

export async function getItemGroupSubGroup(payload) {
  try {
    var resp = await axios.post(URL + 'PurchaseRequestPersediaan/getItemGroupSubGroup', payload);
    if (resp.status != 200) {
      return {  
        status: "ERR",
        message: resp.statusText
      };
    }
    let data = resp.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message
    };
  }
}

export async function getUnitByItemId(payload) {
  try {
    var resp = await axios.post(URL + 'PurchaseRequestPersediaan/getUnitByItemId', payload);
    if (resp.status != 200) {
      return {
        status: "ERR",
        message: resp.statusText
      };
    }
    let data = resp.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message
    };
  }
}

export async function savePurchaseRequest(payload) {
  try {
    var response = await axios.post(URL + 'PurchaseRequestPersediaan/savePurchaseRequest', payload);
    if (response.status != 200) {
      return {
        status: "ERR",
        message: response.statusText
      };
    }
    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message
    };
  }
}

export async function saveAndDoPurchaseRequest(payload){
  try {
    var response = await axios.post(URL + 'PurchaseRequestPersediaan/saveAndDoPurchaseRequest', payload);
    if (response.status != 200) {
      return {
        status: "ERR",
        message: response.statusText
      };
    }
    let data = response.data;
    return data;
  } catch (e) {
    
    return {
      status: "ERR",
      message: e.message
    };
  }
}

export async function getAPI(fn,payload) {
  try {
    let urx = URL + 'PurchaseRequestPersediaan/'+fn;
    console.log(urx);
    var response = await axios.post(urx, payload);
    if (response.status != 200) {
      return {
        status: "ERR",
        message: response.statusText
      };
    }
    let data = response.data;
    return data;
  } catch (e) {
    return {
      status: "ERR",
      message: e.message
    };
  }
} 

export async function getRiwayatItem(params) {
  try {
      var resp = await axios.post(URL + 'PurchaseRequestPersediaan/getRiwayatItem', params);
      if (resp.status != 200) {
          return {
              status: "ERR",
              message: resp.statusText
          };
      }
      let data = resp.data;
      return data;
  } catch (error) {
      return {
          status: "ERR",
          message: error.message
      };    
  }
}

