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

$('#name').on('input propertychange paste', function () {
    $('#name_title').text($('#name').val());
});

$("#zreport").on('change', function () {
    var cashid = $(this).val();
    if (cashid == "") {
        return;
    }
    $("#zframe").data('cash', cashid);
    $("#zframe").attr("src", "action.php?action=zreport&cash=" + cashid);
});

$("#printzreportbtn").click(function () {
    var cashid = $("#zreport").val();
    if (cashid == "") {
        return;
    }
    if (nickelbridge) {
        $.get('action.php', {
            action: 'zreport',
            format: 'json',
            width: receiptwidth,
            cash: $("#zframe").data('cash')
        }, 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("zframe").contentWindow.print();
    }
});

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