<style>
    .ListRowCard {
        margin-bottom: 5px;
    }
</style>

<script>
    defaultBackOnclick = null;
    try {
        currentData = @json($current_data);
    } catch (error) {
        currentData = null;
    }


    LoadDataPageFunc = {};
    LoadDataPageFunc.PageTitle = 'Product Details';
    LoadDataPageFunc.MainDetailsURL = '';
    LoadDataPageFunc.Details = {};
    LoadDataPageFunc.Settings = {};

    LoadDataPageFunc.currentPage = 'BuyViewProductMarket';

    LoadDataPageFunc.Disabled = false;

    LoadDataPageFunc.OutofStock = function () {
        $('#OutOfStockColumn').show();
        $('#ControlsAddCartBuy').hide()

        ModalQuickDismiss(false, 'Product Out of Stock');
        LoadDataPageFunc.Disabled = true;
    };

    LoadDataPageFunc.Buy = function () {
        if (LoadDataPageFunc.Disabled) { return false; }
        gotoPage('ConfirmBUYProductMarket', cartobj, nohistory = 0, redundantpage = 0);
    };
    LoadDataPageFunc.AddToCart = function () {
        if (LoadDataPageFunc.Disabled) { return false; }
        if (!currenttarget) { return false; }
        let reqq = new RequestData(false);
        reqq.fromVarCache(false).data(null).url(`/cart/add/one/${currenttarget}`).type('GET')
            .success((response) => {
                if (response === true) {
                    ModalQuickDismiss('', 'Added to Cart!', 'ModalAddedtoCartSuccessfully');
                    $('#modal-header-ModalAddedtoCartSuccessfully').hide();
                } else {
                    ModalQuickDismiss('', 'Unable to Add to Cart!', 'ModalUnableAddtoCart');
                    $('#modal-header-ModalUnableAddtoCart').hide();
                }
                Preloaders.CartContents();
            }).go();
    };



    if (typeof Buttons == 'undefined') {
        let Buttons;
    }




    Buttons = [
        // { icon: '/assets/addtocart.png', label: 'Add To Cart', onclick: 'LoadDataPageFunc.AddToCart();return false;' },
        // { icon: '/assets/buy.png', label: 'Buy', onclick: 'LoadDataPageFunc.Buy();return false;' },
        { icon: '/assets/briefcase.png', label: 'Manage', onclick: "gotoPage('ManageProductAdmin', currenttarget);return false;" },
    ];


    initGlobal("acct_type", '');

    acct_type = @json($currentUser['acct_type']);


    defaultbackpage = {

        page: 'ListProductsMarket',
        data: 0,
    };

    if (typeof currenttarget.store_hash !== 'undefined' && currenttarget.store_hash) {
        defaultbackpage = {
            page: 'ViewStoreMarket',
            data: currenttarget.store_hash,
        };
    }

    LoadDataPageFunc.Main = function () {
        // Preloaders.ViewProductinMarket(currenttarget, true, LoadDataPageFunc.UpdateDetailsUI);


        ViewBuilder.build({
            dataurl: '/View/Product/Details/data',
            pageName: 'BuyViewProductMarket',
            topbartitle: LoadDataPageFunc.PageTitle,
            defaultback: defaultbackpage,

            defaultErrorHTML: '<b><center>Unable to Load Product</center></b>',
            unabletoLoadFunc: (response) => {

                const errorModalName = 'errorLoadingModal';
                if (document.getElementById(errorModalName)) {
                    modaldestroy(errorModalName);
                }
                let responseMessage = response.message;
                if (typeof responseMessage !== 'object') {
                    responseMessage = 'Unknown Error';
                }
                ModalQuickDismiss('Error!<br>', responseMessage, errorModalName);
            },

            initialize: (response) => {


                if (typeof response.is_from_store !== 'undefined' && response.is_from_store) {
                    Buttons.push(
                        { icon: '/assets/reports.png', label: 'Store Transactions', onclick: `gotoPage('ManageStoreTransactions',currenttarget);return false;` },
                    );
                }
                //TODO Add routes in viewmap and create the blades and controllers for these
                if (acct_type === 'ult' || acct_type === 'superoperator') {
                    Buttons.push(
                        { icon: '/assets/reports.png', label: 'Global Transactions', onclick: `gotoPage('ManageGlobalTransactions',currenttarget);return false;` },
                    );
                }

            },
            sections: [
                {
                    phototype: 'ProductMarket',
                    type: 'photos',
                    id: 'PhotosCard',   
                    currentdatavar: 'photourl'
                },
                {
                    type: 'details',
                    data: [
                        {
                            h: 3,
                            bold: true,
                            type: 'text',
                            currentdatavar: 'name'
                        },
                        {
                            type: 'text',
                            // currentdatavar: 'price',
                            func: function () {
                          
                                const declaredPrice = currentData.store_price || currentData.price;
                                if (typeof currentData.store_price !== 'undefined' && currentData.store_price) {
                                    return 'Store Price P' + declaredPrice + ' / ' + currentData.unitname;
                                } else {
                                    return 'P' + declaredPrice + ' / ' + currentData.unitname;
                                }


                            },
                        }
                    ]
                    // title: ProductData.name,
                    // description: ProductData.description,
                    // price: ProductData.price,
                    // unit: ProductData.unitname,
                },

                {
                    type: 'buttons',
                    buttons: Buttons
                },
                {
                    type: 'card',
                    id: 'DescriptionCard',
                    hidden: false,
                    currentdatavar: 'description',
                },

            ]
        });




        // LoadDataPageFunc.PopulateDetails();

    };




    LoadDataPageFunc.Main();
</script>