const URL = "/one-api/mockup/masterdata/accounting/";

export async function getPeriode(prm) {
    try {
        var resp = await axios.post(URL + "journalcashv2/getPeriode", 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 search(
    token,
    xdate,
    periodeid,
    current_page,
    search,
    branchCode,
    regionalId
) {
    try {
        var resp = await axios.post(URL + "journalcashv2/search", {
            token: token,
            xdate: xdate,
            periodeid: periodeid,
            current_page: current_page,
            search: search,
            branchCode: branchCode,
            regionalId: regionalId
        });
        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 insertSales(branchCode, date, jurnalno) {
    try {
        var resp = await axios.get(
            URL + `mediajurnalauto/insertSales/${branchCode}/${date}?isregen=1&jurnalno=${jurnalno}`
        );
        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 getRegional(prm) {
    try {
        var resp = await axios.post(URL + "journalcashv2/getRegional", 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 getBranch(prm) {
    try {
        var resp = await axios.post(URL + "journalcashv2/getBranch", 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 getUserApproveLevel(params) {
    try {
        var resp = await axios.post(URL + "journalcashv2/getUserApproveLevel", 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 getListingCOA(params) {
    try {
        var resp = await axios.post(URL + "journalcashv2/getListingCOA", 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 requestEditJurnal(params) {
    try {
        const resp = await axios.post(URL + "JournalVerifEdit/requestEditJurnal", params);
        if (resp.status !== 200) {
            return {
                status: "ERR",
                message: resp.statusText
            };
        }

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

export async function saveEditJurnal(params) {
    try {
        var resp = await axios.post(URL + "JournalVerifEdit/saveEditJurnal", 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 changeJurnalToPosted(params) {
    try {
        var resp = await axios.post(URL + "journalcashv2/changeJurnalToPosted", 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 getTotalJurnalNotPostedPerPeriod(params) {
    try {
        var resp = await axios.post(URL + "journalcashv2/getTotalJurnalNotPostedPerPeriod", 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 closeJurnalPerPeriode(params) {
    try {
        var resp = await axios.post(URL + "journalcashv2/closeJurnalPerPeriode", 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 getDataJurnalNotClosedPerPeriod(params) {
    try {
        var resp = await axios.post(URL + "journalcashv2/getDataJurnalNotClosedPerPeriod", 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
        };
    }
}
