<?php
/** 
 * @package     VikRestaurants
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

/**
 * Layout variables
 * -----------------
 * @var  VREStatisticsWidget  $widget  The instance of the widget to be displayed.
 */
extract($displayData);

/**
 * Preload status codes popup for take-away orders.
 *
 * @since 1.9.1
 */
JHtml::fetch('vrehtml.statuscodes.popup', 2);

/**
 * Preload status codes popup for items.
 *
 * @since 1.8.3
 */
JHtml::fetch('vrehtml.statuscodes.popup', 3);

JText::script('VRSYSTEMCONNECTIONERR');

?>

<script>
    (function($) {
        'use strict';

        let firstLoading = true;
        const dishesLookup = {};

        /**
         * Register callback to be executed before
         * launching the update request.
         *
         * @param   mixed   widget  The widget selector.
         * @param   object  config  The widget configuration.
         *
         * @return  void
         */
        WIDGET_PREFLIGHTS[<?php echo $widget->getID(); ?>] = function(widget, config) {
            // internally cache the last modify date of the dishes
            $(widget).find('.block-list-dish[data-last-update]').each(function() {
                const dishId = parseInt($(this).attr('data-id'));
                const lastUpdate = new Date($(this).attr('data-last-update'));

                dishesLookup[dishId] = lastUpdate;
            });
        }

        /**
         * Register callback to be executed after
         * completing the update request.
         *
         * @param   mixed   widget  The widget selector.
         * @param   string  data    The JSON response.
         * @param   object  config  The widget configuration.
         *
         * @return  void
         */
        WIDGET_CALLBACKS[<?php echo $widget->getID(); ?>] = function(widget, data, config) {
            $(widget).find('.widget-body').html(data);

            if (!firstLoading) {
                let hasUpdates = false;

                // scan all the dishes in search of new updates
                $(widget).find('.block-list-dish[data-last-update]').each(function() {
                    const dishId = parseInt($(this).attr('data-id'));
                    const lastUpdate = new Date($(this).attr('data-last-update'));
                    const prevDate = dishesLookup[dishId] ?? null;

                    if (!prevDate || prevDate < lastUpdate) {
                        // item added or updated
                        hasUpdates = true;

                        // add a badge next to the name of the updated dish
                        const warnIcon = $('<i class="fas fa-exclamation-circle warn"></i>')
                            .css('margin-left', '4px')
                            .css('cursor', 'pointer')
                            .on('click', () => warnIcon.remove());

                        $(this).find('.list-dish-name').append(warnIcon);
                    }
                });

                if (hasUpdates && config.playsound) {
                    // play a notification sound in case something has changed
                    playNotificationSound();
                }
            }

            firstLoading = false;
        }

        const playNotificationSound = () => {
            // fetch sound path
            const src = '<?php echo VikRestaurants::getNotificationSound(); ?>';
            // Try to play the sound.
            // Make sure the same sound is not played
            // again for the next 5 seconds.
            SoundTry.playOnce(src, 5000);
        }
    })(jQuery);
</script>