1jQuery(function(jQuery) {
2    // Adding a due date form if it doesn't exist
3    jQuery('.vcalendar .vtodo').each(function() {
4        var taskContainer = jQuery(this);
5        var dueRow = taskContainer.find('abbr.due').closest('tr');
6        var dueDate = dueRow.length > 0 ? dueRow.find('abbr.due').attr('title').replace('T', ' ').slice(0, 16) : new Date().toISOString().replace('T', ' ').slice(0, 16);
7
8        // Create a due date form if none exists
9        var dueDateForm = '<tr>' +
10                            '<th class="col4 align">Due date:</th>' +
11                            '<td>' +
12                              '<abbr class="due" title="' + dueDate + '">' +
13                                '<form action="' + window.location.href + '" method="post" accept-charset="utf-8" class="doku_form due">' +
14                                  '<div class="no">' +
15                                    '<input type="datetime-local" class="due-date-input" value="' + dueDate + '" />' +
16                                    '<button class="due-update">Change</button>' +
17                                  '</div>' +
18                                '</form>' +
19                              '</abbr>' +
20                            '</td>' +
21                          '</tr>';
22
23        if (dueRow.length > 0) {
24          dueRow.before(dueDateForm);
25          dueRow.remove();
26        } else {
27          taskContainer.find('tbody').append(dueDateForm);
28        }
29    });
30
31    // Adding a priority form if it doesn't exist
32    jQuery('.vcalendar .vtodo').each(function() {
33        var taskContainer = jQuery(this);
34        var priorityRow = taskContainer.find('tr.priority');
35        var priorityLevel = 'Low'; // Default value
36
37        if (taskContainer.hasClass('priority1')) {
38            priorityLevel = 'Normal';
39        } else if (taskContainer.hasClass('priority2')) {
40            priorityLevel = 'High';
41        } else if (taskContainer.hasClass('priority3')) {
42            priorityLevel = 'Critical';
43        }
44
45        if (priorityRow.length === 0) {
46
47            // Create a priority form if none exists
48            var priorityForm = '<tr class="priority">' +
49                                '<th class="col4 align">Priority:</th>' +
50                                '<td>' +
51                                  '<abbr class="priority" title="' + priorityLevel + '">' +
52                                    '<form action="' + window.location.href + '" method="post" accept-charset="utf-8" class="doku_form priority">' +
53                                      '<div class="no">' +
54                                        '<select name="priority">' +
55                                          '<option value="0"' + (priorityLevel === 'Low' ? ' selected' : '') + '>Low</option>' +
56                                          '<option value="1"' + (priorityLevel === 'Normal' ? ' selected' : '') + '>Normal</option>' +
57                                          '<option value="2"' + (priorityLevel === 'High' ? ' selected' : '') + '>High</option>' +
58                                          '<option value="3"' + (priorityLevel === 'Critical' ? ' selected' : '') + '>Critical</option>' +
59                                        '</select>' +
60                                        '<button class="priority-update">Change</button>' +
61                                      '</div>' +
62                                    '</form>' +
63                                  '</abbr>' +
64                                '</td>' +
65                              '</tr>';
66            taskContainer.find('tbody').append(priorityForm);
67        }
68    });
69
70    // Adding unassign button next to assigned user
71    jQuery('.vcalendar .vtodo').each(function() {
72        var taskContainer = jQuery(this);
73        var assignedRow = taskContainer.find('tr:has(.organizer)');
74        var assignedTo = assignedRow.find('.organizer .fn');
75
76        if (assignedTo.length > 0 && assignedRow.find('.unassign-user').length === 0) {
77            // Create unassign button if user is assigned
78            var unassignButton = '<button class="unassign-user">Unassign</button>';
79            assignedRow.find('td').append(unassignButton);
80        }
81    });
82
83    // Adding unassign button next to assigned user
84    jQuery('.vcalendar .vtodo').each(function() {
85        var taskContainer = jQuery(this);
86        var assignedRow = taskContainer.find('tr:has(.organizer)');
87        var assignedTo = assignedRow.find('.organizer .fn');
88
89        if (assignedTo.length > 0 && assignedRow.find('.unassign-user').length === 0) {
90            // Create unassign button if user is assigned
91            var unassignButton = '<button class="unassign-user">Unassign</button>';
92            assignedRow.find('td').append(unassignButton);
93        }
94    });
95
96    // Event listener to update due date
97    jQuery('form.due').on('submit', function(e) {
98        e.preventDefault();
99        var form = jQuery(this);
100        var newDueDate = form.find('input').val();
101        if (newDueDate) {
102            // Send AJAX request to update the due date
103            jQuery.ajax({
104                url: DOKU_BASE + 'lib/exe/ajax.php',
105                type: 'POST',
106                data: {
107                    call: 'taskextra_due',
108                    due_date: newDueDate,
109                    id: JSINFO.id // Current page ID
110                },
111                success: function(response) {
112                    var result = JSON.parse(response);
113                    if (result.status === 'success') {
114                        location.reload();
115                    } else {
116                        console.error('Failed to update due date');
117                    }
118                },
119                error: function() {
120                    console.error('Error while updating due date');
121                }
122            });
123        }
124    });
125
126    // Event listener to update priority
127    jQuery('form.priority').on('submit', function(e) {
128        e.preventDefault();
129        var form = jQuery(this);
130        var newPriority = form.find('select').val();
131        if (newPriority) {
132            // Send AJAX request to update the priority
133            jQuery.ajax({
134                url: DOKU_BASE + 'lib/exe/ajax.php',
135                type: 'POST',
136                data: {
137                    call: 'taskextra_priority',
138                    priority: newPriority,
139                    id: JSINFO.id
140                },
141                success: function(response) {
142                    var result = JSON.parse(response);
143                    if (result.status === 'success') {
144                        location.reload();
145                    } else {
146                        console.error('Failed to update priority');
147                    }
148                },
149                error: function() {
150                    console.error('Error while updating priority');
151                }
152            });
153        }
154    });
155
156    // Event listener to unassign user
157    jQuery(document).on('click', '.unassign-user', function() {
158        var assignedRow = jQuery(this).closest('tr');
159
160        // Send AJAX request to unassign the user
161        jQuery.ajax({
162            url: DOKU_BASE + 'lib/exe/ajax.php',
163            type: 'POST',
164            data: {
165                call: 'taskextra_unassign',
166                id: JSINFO.id // Current page ID
167            },
168            success: function(response) {
169                var result = JSON.parse(response);
170                if (result.status === 'success') {
171                    assignedRow.remove();
172                    location.reload();
173                } else {
174                    console.error('Failed to unassign user');
175                }
176            },
177            error: function() {
178                console.error('Error while unassigning user');
179            }
180        });
181    });
182});
183