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

export async function search(prm) {
  try {
    var resp = await axios.post(URL + "purchaseRequestDirect/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 searchBranch(token, prm) {
    try {
        var resp = await axios.post(URL + 'purchaseRequestDirect/getbranch',{
            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 searchAccDana(token, prm) {
    try {
        var resp = await axios.post(URL + 'purchaseRequestDirect/getcoa', {
            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 searchAccBiaya(token, prm) {
    try {
        var resp = await axios.post(URL + 'purchaseRequestDirect/getcoa', {
            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 searchAccTemp(token, prm) {
    try {
        var resp = await axios.post(URL + 'purchaseRequestDirect/getcoa', {
            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 getPurchase(token, prm) {
  try {
    var resp = await axios.post(URL + "purchaseRequestDirect/getPurchase", {
        token: token,
        branchcode: 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 searchdetail(token, prm) {
  try {
    var resp = await axios.post(URL + "purchaseRequestDirect/searchdetail", {
        token: token,
        ID: 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 saveDetail(token, prm) {
  try {
    var resp = await axios.post(URL + "purchaseRequestDirect/save", {
        token: token,
        BranchCode: prm.branch,
        CoaSourceID: prm.accountDana,
        CoaExpenseID: prm.accountBiaya,
        CoaTemporaryID: prm.accountTemp,
        total: prm.total,
        details: prm.details
    }
    );
    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 getTotalApproved(token) {
    try {
        var resp = await axios.post(URL + 'purchaseRequestDirect/getTotalApproved', {
            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 deleteVoucher(token, prm) {
    try {
        var resp = await axios.post(URL + 'purchaseRequestDirect/delete', {
            token: token,
            ID: prm.PVID
        });
        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 editVoucher(token, prm) {
  try {
    var resp = await axios.post(URL + "purchaseRequestDirect/update", {
        token: token,
        BranchCode: prm.branch,
        CoaSourceID: prm.accountDana,
        CoaExpenseID: prm.accountBiaya,
        CoaTemporaryID: prm.accountTemp,
        total: prm.total,
        details: prm.details,
        ID: prm.voucherID
    }
    );
    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 getPurchaseUpdateNew(token, prm) {
  try {
    var resp = await axios.post(URL + "purchaseRequestDirect/getPurchaseUpdateNew", {
        token: token,
        branchcode: prm.branchcode,
        ID: prm.voucherID
    }
    );
    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 getVoucherDetailItem(params) {
  try {
    var resp = await axios.post(URL + "purchaseRequestDirect/voucherDetailItem", 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,
    };
  }
}

export async function saveAccountItem(params) {
  try {
    var resp = await axios.post(URL + "purchaseRequestDirect/saveAccountDetail", 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,
    };
  }
}

export async function moveToKasir(param) {
  try {
    var resp = await axios.post(URL + "purchaseRequestDirect/moveToKasir", param)
    if (resp.status != 200) {
      return {
        status: "ERR",
        message: resp.statusText
      }
    } else {
      let data = resp.data
      return data
    }
  } catch (error) {
    return {
      status: "ERR",
      message: error.message
    }
  }
}