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

export async function search(payload) {
  try {
    var response = await axios.post(URL + "PurchaseRequestNonPersediaan/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 + "PurchaseRequestNonPersediaan/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 + "PurchaseRequestNonPersediaan/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 + "PurchaseRequestNonPersediaan/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 + "PurchaseRequestNonPersediaan/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 + "PurchaseRequestNonPersediaan/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 + "PurchaseRequestNonPersediaan/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 + "PurchaseRequestNonPersediaan/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 + "PurchaseRequestNonPersediaan/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 + "PurchaseRequestNonPersediaan/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 + "PurchaseRequestNonPersediaan/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 + 'PurchaseRequestNonPersediaan/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 + 'PurchaseRequestNonPersediaan/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 + 'PurchaseRequestNonPersediaan/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 + 'PurchaseRequestNonPersediaan/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 + 'PurchaseRequestNonPersediaan/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 + 'PurchaseRequestNonPersediaan/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 + 'PurchaseRequestNonPersediaan/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 + 'PurchaseRequestNonPersediaan/'+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(payload) {
  try {
    var response = await axios.post(URL + "PurchaseRequestNonPersediaan/getRiwayatItem", 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 confirmHandOver(payload) {
  try {
    var response = await axios.post(
      URL + "PurchaseRequestNonPersediaan/confirmHandOver",
      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,
    };
  }
}