Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
35626 amit 1
/**
2
 * Ticket Manager - Modular AJAX-based ticket management
3
 * Handles unread/read tracking - marks as read immediately on activity modal click
4
 */
5
var TicketManager = (function() {
6
    var config = {
7
        endpoints: {
8
            markRead: '/cs/markTicketRead',
9
            unreadCount: '/cs/unreadCount'
10
        }
11
    };
12
 
13
    var state = {
14
        currentTicketId: null,
15
        unreadCount: 0
16
    };
17
 
18
    function getEndpoint(name) {
19
        return context + config.endpoints[name];
20
    }
21
 
22
    return {
23
        init: function(options) {
24
            if (options) {
25
                $.extend(config, options);
26
            }
27
            this.refreshUnreadCount();
28
            this.bindEvents();
29
        },
30
 
31
        bindEvents: function() {
32
            // No timer-based events needed; marking is done immediately on click
33
        },
34
 
35
        markAsRead: function(ticketId) {
36
            var self = this;
37
            doPostAjaxParamsPromise(getEndpoint('markRead'), { ticketId: ticketId })
38
                .done(function(response) {
39
                    // Update UI - remove unread styling
40
                    var row = $('tr[data-ticket-id="' + ticketId + '"]');
41
                    row.removeClass('ticket-unread');
42
                    row.find('.unread-badge').remove();
43
 
44
                    // Update unread count
45
                    self.refreshUnreadCount();
46
                })
47
                .fail(function(err) {
48
                    console.error('Failed to mark ticket as read:', err);
49
                });
50
        },
51
 
52
        refreshUnreadCount: function() {
53
            doGetAjaxPromise(getEndpoint('unreadCount'), {})
54
                .done(function(response) {
55
                    if (response && response.success !== undefined) {
56
                        state.unreadCount = response.unreadCount || 0;
57
                    } else {
58
                        state.unreadCount = response.unreadCount || 0;
59
                    }
60
                    TicketRenderer.updateUnreadBadge(state.unreadCount);
61
                })
62
                .fail(function(err) {
63
                    console.error('Failed to fetch unread count:', err);
64
                });
65
        },
66
 
67
        getUnreadCount: function() {
68
            return state.unreadCount;
69
        },
70
 
71
        setCurrentTicketId: function(ticketId) {
72
            $('#myModal').data('current-ticket-id', ticketId);
73
            var row = $('tr[data-ticket-id="' + ticketId + '"]');
74
            if (row.hasClass('ticket-unread')) {
75
                this.markAsRead(ticketId);
76
            }
77
        }
78
    };
79
})();