Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

/**
 * Ticket Manager - Modular AJAX-based ticket management
 * Handles unread/read tracking - marks as read immediately on activity modal click
 */
var TicketManager = (function() {
    var config = {
        endpoints: {
            markRead: '/cs/markTicketRead',
            unreadCount: '/cs/unreadCount'
        }
    };

    var state = {
        currentTicketId: null,
        unreadCount: 0
    };

    function getEndpoint(name) {
        return context + config.endpoints[name];
    }

    return {
        init: function(options) {
            if (options) {
                $.extend(config, options);
            }
            this.refreshUnreadCount();
            this.bindEvents();
        },

        bindEvents: function() {
            // No timer-based events needed; marking is done immediately on click
        },

        markAsRead: function(ticketId) {
            var self = this;
            doPostAjaxParamsPromise(getEndpoint('markRead'), { ticketId: ticketId })
                .done(function(response) {
                    // Update UI - remove unread styling
                    var row = $('tr[data-ticket-id="' + ticketId + '"]');
                    row.removeClass('ticket-unread');
                    row.find('.unread-badge').remove();

                    // Update unread count
                    self.refreshUnreadCount();
                })
                .fail(function(err) {
                    console.error('Failed to mark ticket as read:', err);
                });
        },

        refreshUnreadCount: function() {
            doGetAjaxPromise(getEndpoint('unreadCount'), {})
                .done(function(response) {
                    if (response && response.success !== undefined) {
                        state.unreadCount = response.unreadCount || 0;
                    } else {
                        state.unreadCount = response.unreadCount || 0;
                    }
                    TicketRenderer.updateUnreadBadge(state.unreadCount);
                })
                .fail(function(err) {
                    console.error('Failed to fetch unread count:', err);
                });
        },

        getUnreadCount: function() {
            return state.unreadCount;
        },

        setCurrentTicketId: function(ticketId) {
            $('#myModal').data('current-ticket-id', ticketId);
            var row = $('tr[data-ticket-id="' + ticketId + '"]');
            if (row.hasClass('ticket-unread')) {
                this.markAsRead(ticketId);
            }
        }
    };
})();