$(function () {
    $("#prod").on("click", ".beli", function () {
        var token = $("meta[name='csrf-token']").attr("content");
        var kode = $(this).attr("data-kd");
        $.ajax({
            type: "post",
            url: "add_cart",
            data: {
                kode: kode,
                _token: token,
            },
            success: function (data) {
                vcart();
                Swal.fire({
                    icon: "success",
                    title: "Berhasil",
                    text: "Produk ditambahkan kekeranjang",
                });
            },
        });
    });

    $("#cart_item").on("click", ".qty_update", function () {
        var token = $("meta[name='csrf-token']").attr("content");
        var id = $(this).attr("data-id");
        var idp = $(this).attr("data-idp");
        var nm = $(this).attr("data-nm");
        $.ajax({
            type: "post",
            url: "/qty-update",
            data: {
                id: id,
                idp: idp,
                nm: nm,
                _token: token,
            },
            success: function (data) {
                vcart();
                document.location.href = "/keranjang";
            },
        });
    });

    $("#cart_item").on("click", ".hapus_item", function () {
        var token = $("meta[name='csrf-token']").attr("content");
        var id = $(this).attr("data-id");
        $.ajax({
            type: "post",
            url: "/hapus-cart",
            data: {
                id: id,
                _token: token,
            },
            success: function (data) {
                vcart();
                document.location.href = "/keranjang";
            },
        });
    });

    vcart();
    function vcart() {
        $.ajax({
            type: "GET",
            url: "view-cart",
            success: function (data) {
                $("#isi_cart1").html(data);
                $("#isi_cart2").html(data);
            },
        });
    }
});
