/*
 * 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/.
 */


$("#openmanagement").click(function () {
    $("#opendrawerbtn").removeClass('d-none');
    if (!nickelbridge) {
        $("#opendrawerbtn").addClass('d-none');
    }
    document.getElementById("xframe").contentDocument.location.reload(true);
    $("#managermodal").modal();
});

$("#xprintbtn").click(function () {
    if (nickelbridge) {
        $.get('action.php', {
            action: 'xreport',
            format: 'json',
            width: receiptwidth,
            register: $("#register").data('id')
        }, function (receipt) {
            $.ajax("http://127.0.0.1:64269/print", {
                data: JSON.stringify(receipt),
                contentType: 'application/json',
                type: 'POST',
                success: function (resp) {

                }
            }).fail(function (resp) {
                alert("Error: " + JSON.stringify(resp));
            });
        });
    } else {
        document.getElementById("xframe").contentWindow.print();
    }
});

function showTransactionList(search, start, end) {
    if (search == "" && start == "" && end == "") {
        return;
    }
    $.get('action.php', {
        action: 'transactionsearch',
        q: search,
        start: start,
        end: end
    }, function (data) {
        var html = "";
        if (data['transactions'].length > 0) {
            for (var i = 0; i < data['transactions'].length; i++) {
                var txid = '<i class="fas fa-hashtag"></i> ' + data['transactions'][i]['txid'];
                var date = '<i class="fas fa-calendar"></i> ' + data['transactions'][i]['txdate'];
                var customername = "";
                var cashiername = "";
                var buttons = "";
                if (typeof data['transactions'][i]['customer']['name'] == 'string') {
                    customername = '<i class="fas fa-user-circle"></i> ' + data['transactions'][i]['customer']['name'];
                }
                if (data['transactions'][i]['cashier']['name'] != "") {
                    cashiername = '<i class="fas fa-id-card-alt"></i> ' + data['transactions'][i]['cashier']['name'];
                }
                buttons += '<span data-txid=' + data['transactions'][i]['txid'] + '" class="btn btn-sm btn-primary printreceiptbtn"><i class="fas fa-receipt"></i> Receipt</span> ';
                if (data['transactions'][i]['editable'] === true) {
                    buttons += '<a href="app.php?page=pos&txid=' + data['transactions'][i]['txid'] + '" class="btn btn-sm btn-info"><i class="fas fa-edit"></i> Edit</a>';
                } else {
                    buttons += '<a href="app.php?page=pos&txid=' + data['transactions'][i]['txid'] + '" class="btn btn-sm btn-info"><i class="fas fa-undo-alt"></i> Return</a>';
                }
                html += '<div class="list-group-item transaction d-flex justify-content-between flex-wrap">'
                        + '<div>' + buttons + '</div>'
                        + '<div>' + txid + '</div>'
                        + '<div>' + date + '</div>'
                        + '<div>' + cashiername + '</div>'
                        + '<div>' + customername + '</div>'
                        + '</div>';
            }
        } else {
            html = '<div class="list-group-item"><i class="fas fa-search-minus"></i> No results.</div>';
        }
        $("#transactionselection").html(html);
    });
}

$("#transactionsearch").on('keypress', function (e) {
    if (e.which === 13) {
        showTransactionList($("#transactionsearch").val(), $("#tx-startdate").val(), $("#tx-enddate").val());
        $("#transactionsearch").val("");
        $("#txsearch-datetimefilters").addClass("d-none");
    }
});

$("#transactionsearchbtn").on("click", function () {
    showTransactionList($("#transactionsearch").val(), $("#tx-startdate").val(), $("#tx-enddate").val());
    $("#transactionsearch").val("");
    $("#txsearch-datetimefilters").addClass("d-none");
});

$("#managermodal").on("click", ".printreceiptbtn", function () {
    $("#managermodal").modal("hide");
    $("#receiptchangediv").addClass("d-none");
    $("#receiptframe").data("txid", $(this).data("txid"));
    $("#receiptframe").attr("src", 'action.php?action=getreceipt&txid=' + $(this).data("txid"));
    $("#receiptmodal").modal();
});

$("#opendrawerbtn").click(function () {
    $.ajax("http://127.0.0.1:64269/opendrawer", {
        contentType: 'application/json',
        type: 'POST',
        success: function (resp) {

        }
    }).fail(function (resp) {
        alert("Error: " + JSON.stringify(resp));
    });
});

$("#managermodal .open-number-pad-btn").click(function () {
    var inputbox = $("#transactionsearch");
    var value = inputbox.val();
    if (isNaN(value)) {
        value = '';
    }
    bsnumpad("Keyboard", value, '<i class="fas fa-search"></i> Search', "Cancel", function (answer) {
        showTransactionList(answer);
    });
});

$("#txsearch-datetime-btn").click(function () {
    if ($("#txsearch-datetimefilters").hasClass("d-none")) {
        $("#txsearch-datetimefilters").removeClass("d-none");
    } else {
        $("#txsearch-datetimefilters").addClass("d-none");
    }
});

$("#managermodal").on("hidden.bs.modal", function () {
    $("#txsearch-datetimefilters").addClass("d-none");
})

$('#tx-startdate').datetimepicker({
    useCurrent: false,
    icons: {
        time: "fas fa-clock fa-fw fa-2x",
        date: "fas fa-calendar fa-fw fa-2x",
        up: "fas fa-arrow-up fa-fw fa-2x",
        down: "fas fa-arrow-down fa-fw fa-2x"
    }
});
$('#tx-enddate').datetimepicker({
    useCurrent: true,
    icons: {
        time: "fas fa-clock fa-fw fa-2x",
        date: "fas fa-calendar fa-fw fa-2x",
        up: "fas fa-arrow-up fa-fw fa-2x",
        down: "fas fa-arrow-down fa-fw fa-2x"
    }
});