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

export async function search(payload) {
  try {
    var response = await axios.post(URL + "PurchaseRequestDirect/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 + "PurchaseRequestDirect/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 + "PurchaseRequestDirect/getRegional", 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 + "PurchaseRequestDirect/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 + "PurchaseRequestDirect/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 + "PurchaseRequestDirect/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 + "PurchaseRequestDirect/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 + "PurchaseRequestDirect/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 + "PurchaseRequestDirect/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 + "PurchaseRequestDirect/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 + "PurchaseRequestDirect/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 + 'PurchaseRequestDirect/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 saveFiles(payload) {
  try {
    var response = await axios.post(
      URL + "PurchaseRequestDirect/saveFiles",
      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 getCategoryPD(params) {
  try {
    var response = await axios.post(URL + 'PurchaseRequestDirect/PurchaseDirectCategory', params)
    if (response.status !== 200) {
      return {
        status: "ERR",
        message: response.statusText
      }
    }

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