<style>
    .ListRowCard {
        margin-bottom: 5px;
    }
</style>
<div id="MainView" style="padding-top: 5px;">

    <div class="row" id="CartListRow" style="margin-top: 5px;   margin-bottom: 5px;">


    </div>
</div>


</div>

</div>

<script>
    LoadDataPageFunc = {};
    LoadDataPageFunc.PageTitle = 'Cart';
    LoadDataPageFunc.MainDetailsURL = '';
    LoadDataPageFunc.Details = {};
    LoadDataPageFunc.CartCurrentlyLoading = 0;

    LoadDataPageFunc.SetQuantityUI = function (newquantity, rownum) {
        if (typeof rownum !== 'number') { return false; }
        if (typeof newquantity !== 'number') { return false; }
        let controlid = "ProductCartMarket-" + rownum;
        $('#' + controlid).html(newquantity);
        const newvalue = $('#' + controlid).html();
        if (newquantity !== newvalue) { return false; }
        return true;
    };


    LoadDataPageFunc.FindRowNumByHash = function (hashkey) {
        let element = $('[data-hash="' + hashkey + '"]');
        if (element.length === 0) { return false; }
        let nrownum = element.data('rownum');
        return nrownum
    };


    LoadDataPageFunc.SetQuantitybyHash = function (newquantity, hashkey) {

        if (!hashkey || typeof hashkey !== 'string') { return false; }
        let rownum = '';
        rownum = LoadDataPageFunc.FindRowNumByHash(hashkey);

        if (rownum === false) { return false; }
        const changequantity = LoadDataPageFunc.SetQuantityUI(newquantity, rownum);
        if (!changequantity) { return false; }
        return true;
    };

    LoadDataPageFunc.UpdateProductQuantityCart = function (response, producthash) {
        LoadDataPageFunc.CartCurrentlyLoading--;
        LoadDataPageFunc.PopulateDetails(false);
        /*    if (response === false) { return false; }
                   if (typeof response !== 'number') {
                       return false;
                   };
                  return LoadDataPageFunc.SetQuantitybyHash(response, producthash); */

    };


    LoadDataPageFunc.RequestData = new RequestData(false);

    LoadDataPageFunc.RemoveFromCartOneProduct = function (producthash) {
        if (LoadDataPageFunc.CartCurrentlyLoading) { return false; }
        if (!producthash) { return false; }
        LoadDataPageFunc.CartCurrentlyLoading++;
        LoadDataPageFunc.RequestData
            .url('/cart/remove/one/' + producthash)
            .type('GET')
            .fromVarCache(false)
            .success((response) => {
                const res = LoadDataPageFunc.UpdateProductQuantityCart(response, producthash);
            })
            .go();
    }



    LoadDataPageFunc.CheckOutAllCart = function () {
        const cartobj = objectToUrlSafeBase64(LoadDataPageFunc.Details);
        gotoPage('ConfirmBUYProductMarket', cartobj, nohistory = 0, redundantpage = 0);

    };



    LoadDataPageFunc.AddToCartOneProduct = function (producthash) {
        if (LoadDataPageFunc.CartCurrentlyLoading > 0) { return false; }
        if (!producthash) { return false; }
        LoadDataPageFunc.CartCurrentlyLoading++;
        LoadDataPageFunc.RequestData
            .url('/cart/add/one/' + producthash)
            .type('GET')
            .fromVarCache(false)
            .success((response) => {
                const res = LoadDataPageFunc.UpdateProductQuantityCart(response, producthash);
            })
            .go();

    };


    LoadDataPageFunc.CartProductRow = MarketUI.CartProductRow;

    LoadDataPageFunc.UpdateCartFull = function (response) {
        LoadDataPageFunc.Details = response;
        if (Array.isArray(response) && response.length === 0) {
            let cardCARTempty = CreateCardSimple(false, '<div style="text-align:center;">No Items On Cart! Start Shopping<br>' + buttonGOTOPage('Go to Market', 'ListProductsMarket') + '</div>');
            $('#CartListRow').html(cardCARTempty);
            return null;
        }



        let htmllist = []; let finalhtml = '';
        let rownum = 0;

        if (!response) { return false; }

        LoopArrayorObject(response, (item) => {
            htmllist.push(LoadDataPageFunc.CartProductRow(item, rownum));
            rownum++;
        });
        finalhtml = htmllist.join('');
        finalhtml +=`<br><br>`+buttonprimary('Checkout All','','LoadDataPageFunc.CheckOutAllCart()');
        $('#CartListRow').html(finalhtml);

        LoopArrayorObject(response, (item) => {
            LoadAndCreateURLfromFileHash(item.photourl).then(bloburl => {
                $('#ProductCartMarketPlacePhoto--' + rownum).attr('src', bloburl);
            });

        });
    };


    LoadDataPageFunc.PopulateDetails = function (fromVarCacheN = true) {

        Preloaders.CartContents(fromVarCacheN, (response) => { LoadDataPageFunc.UpdateCartFull(response); });
        /*         let reqq = new RequestData(true);
               reqq.fromVarCache(fromVarCacheN).data().url('/User/My/Details/Cart/data').type('POST')
                   .success((response) => {
                     LoadDataPageFunc.UpdateCartFull(response); 
                   }).go();  */

    };

    LoadDataPageFunc.Main = function () {
        changeTopbarTitle(LoadDataPageFunc.PageTitle);
        LoadDataPageFunc.PopulateDetails();
    };



    LoadDataPageFunc.Main();
</script>