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

function loadGridView() {
    if ($("#gridview").hasClass("d-flex")) {
        $.get("action.php", {
            action: "getgriditems",
            customer: customerid
        }, function (data) {
            $("#gridview").html("");
            for (var i = 0; i < data['items'].length; i++) {
                var itemid = data['items'][i]['id'];
                var price = data['items'][i]['price'];
                var name = data['items'][i]['name'];
                var code = data['items'][i]['code'];
                var codeicon = "";
                if (code != "") {
                    codeicon = '<i class="fas fa-barcode"></i> ';
                }
                var html = '<div class="card p-2 text-center m-1 gridview-itembtn" data-itemid="' + itemid + '" data-price="' + price + '" data-name="' + name + '" data-code="' + code + '">'
                        + '<span class="font-weight-bold">' + name + '</span>'
                        + '<span>'
                        + codeicon + code + "<br />"
                        + '$' + price
                        + '</span>'
                        + '</div>';
                $("#gridview").append(html);
            }
        });
    }
}

$("#gridview").on("click", ".gridview-itembtn", function () {
    var name = $(this).data("name");
    var code = $(this).data("code");
    var price = $(this).data("price");
    var itemid = $(this).data("itemid");
    addItem(name, code, price, itemid);
});

$("#gridviewbtn").click(function () {
    if ($("#gridview").hasClass("d-flex")) {
        $("#gridview").addClass("d-none");
        $("#gridview").removeClass("d-flex");
    } else {
        $("#gridview").addClass("d-flex");
        $("#gridview").removeClass("d-none");
        loadGridView();
    }
});

if (showgridbydefault) {
    $("#gridview").addClass("d-flex");
    $("#gridview").removeClass("d-none");
    loadGridView();
}