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

export async function search(prm) {
  try {
    var resp = await axios.post(URL + "purchaseRequestDirectRealitation/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 + "purchaseRequestDirectRealitation/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 getTotalDraft(token) {
    try {
        var resp = await axios.post(URL + 'purchaseRequestDirectRealitation/getTotalDraft', {
            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 getTotalPaid(token) {
    try {
        var resp = await axios.post(URL + 'purchaseRequestDirectRealitation/getTotalPaid', {
            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(prm) {
  try {
    // console.log(prm)
    var resp = await axios.post(URL + "purchaseRequestDirectRealitation/update",prm);
    // console.log(resp)
    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 updatepaid(prm) {
  try {
    // console.log(prm)
    var resp = await axios.post(URL + "purchaseRequestDirectRealitation/updatepaid", prm);
    console.log('api resp',resp)
    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 getUserReceive(prm) {
  try {
    var resp = await axios.post(URL + "purchaseRequestDirectRealitation/getUserReceive", 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 getVoucherDetail(params) {
  try {
    var resp = await axios.post(URL + "purchaseRequestDirectRealitation/getVoucherDetail", params);
    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 saveVoucherDetail(params) {
  try {
    var resp = await axios.post(URL + "purchaseRequestDirectRealitation/saveVoucherDetail", params);
    if (resp.status != 200) {
      return {
        status: "ERR",
        message: resp.message
      }
    }

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

export async function editVoucherV2(prm) {
  try {
    // console.log(prm)
    var resp = await axios.post(URL + "purchaseRequestDirectRealitation/updateV2",prm);
    // console.log(resp)
    if (resp.status !== 200) {
      return {
        status: "ERR",
        message: resp.statusText,
      };
    }

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