/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

var nickelbridge = false;
var receiptwidth = 48;

function recalculate() {
    removezero();
    var total = 0.0;
    var paid = 0.0;
    var remaining = 0.0;
    var change = 0.0;
    $("#pos-lines-box .list-group-item").each(function () {
        var each = $(".item-price", this).val() * 1.0;
        var qty = $(".item-qty", this).val() * 1.0;
        var line = each * qty;
        $(".line-total", this).text(line.toFixed(2));
        $(".item-price", this).val(each.toFixed(2));
        console.log(each.toFixed(2));
        total += line;
    });

    $("#payment-lines .list-group-item").each(function () {
        var line = $(".payment-entry", this).val() * 1.0;
        paid += line;
    });

    var discountpercent = $("#discountpercentbtn").data("percent");
    if (discountpercent > 0 && discountpercent < 100) {
        total *= 1.0 - ((discountpercent * 1.0) / 100);
    }

    remaining = total - paid;
    change = (total - paid) * -1.0;
    if (remaining <= 0) {
        remaining = 0.0;
        $("#owed-amount").parent().removeClass("font-weight-bold");
    } else {
        $("#owed-amount").parent().addClass("font-weight-bold");
    }

    if (change <= 0) {
        change = 0.0;
        $("#change-amount").parent().removeClass("font-weight-bold");
    } else {
        $("#change-amount").parent().addClass("font-weight-bold");
    }

    $("#grand-total").text(total.toFixed(2));
    $("#paid-amount").text(paid.toFixed(2));
    $("#owed-amount").text(remaining.toFixed(2));
    $("#change-amount").text(change.toFixed(2));
    $("#receiptchange").text(change.toFixed(2));
}

$("#deletetxbtn").click(function () {
    bsalert("Confirm", "Really delete transaction?", "Yes", "No", function () {
        if ($("#txid").length) {
            $.post("action.php", {
                action: "delete_transaction",
                txid: $("#txid").val()
            }, function (data) {
                console.log(data);
                console.log(data.status == "OK");
                if (data.status == "OK") {
                    window.location.href = "app.php?page=pos";
                } else {
                    bsalert("Error", data.message);
                }
            }).fail(function () {
                bsalert("Error", "An unknown error occurred.");
            });
        } else {
            window.location.href = "app.php?page=pos";
        }
    });
});

// Make sure the session doesn't expire
setInterval(function () {
    $.get("action.php", {action: "session_keepalive"});
}, 1000 * 60);

$(document).ready(function () {
    if ($("#txid").length) {
        recalculate();
        $("#paymentui").removeClass("d-none");
    }
    if ($("#return").length) {
        recalculate();
    }

    $.get('http://127.0.0.1:64269/status', {}, function (resp) {
        if (resp.status == "OK") {
            nickelbridge = true;
            receiptwidth = resp.width;
        }
    });
});