{* Shared "View task log" modal — included from Task Queue and Server Actions.
   The owning page wires up its own click handler that calls
   the global puqOpenTaskLog(id) helper defined alongside the markup. *}
<div class="modal fade" id="puq-tasklog-modal" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">
                    <i class="fa fa-list-alt"></i>
                    Task <span id="puq-tasklog-id"></span>
                    <span id="puq-tasklog-status"></span>
                    <small class="text-muted" id="puq-tasklog-action"></small>
                </h4>
            </div>
            <div class="modal-body">
                <dl class="dl-horizontal" id="puq-tasklog-meta" style="margin-bottom:8px;font-size:12px;"></dl>
                <div id="puq-tasklog-error" class="alert alert-danger small" style="display:none;"></div>
                <div style="display:flex;align-items:center;gap:8px;margin-bottom:6px;">
                    <strong>Log</strong>
                    <span class="text-muted small" id="puq-tasklog-bytes"></span>
                    <label class="text-muted small" style="margin:0 0 0 auto;font-weight:normal;">
                        <input type="checkbox" id="puq-tasklog-autorefresh" checked> Auto-refresh while running (2s)
                    </label>
                </div>
                <pre id="puq-tasklog-body" style="background:#1e1e1e;color:#dcdcdc;padding:10px;border-radius:3px;max-height:55vh;overflow:auto;font-size:11px;line-height:1.4;white-space:pre-wrap;word-break:break-word;">Loading…</pre>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" id="puq-tasklog-refresh"><i class="fa fa-sync"></i> Refresh</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

{literal}
<script>
// Global helper — pages call window.puqOpenTaskLog(id) from their click handlers.
(function(){
    var baseUrl = '{/literal}{$meta.admin_module_url}&ajax={literal}';
    var badge = window.statusBadge;
    var taskLogId = 0;
    var taskLogTimer = null;

    window.puqOpenTaskLog = function(id) {
        taskLogId = id;
        $('#puq-tasklog-id').text('#' + id);
        $('#puq-tasklog-status').html('');
        $('#puq-tasklog-action').text('');
        $('#puq-tasklog-meta').empty();
        $('#puq-tasklog-error').hide().text('');
        $('#puq-tasklog-bytes').text('');
        $('#puq-tasklog-body').text('Loading…');
        $('#puq-tasklog-modal').modal('show');
        loadTaskLog();
    };

    function loadTaskLog() {
        if (!taskLogId) return;
        $.ajax({
            url: baseUrl + 'getTask&id=' + taskLogId, dataType: 'json',
            success: function(r) {
                if (r.status !== 'success' || !r.data) {
                    $('#puq-tasklog-body').text(r.msg || 'Task not found');
                    return;
                }
                var t = r.data;
                $('#puq-tasklog-status').html(' ' + badge(t.status));
                $('#puq-tasklog-action').text(' — ' + t.action);
                var $m = $('#puq-tasklog-meta').empty();
                function row(k, v) { if (v != null && v !== '') $m.append('<dt>'+esc(k)+'</dt><dd>'+esc(v)+'</dd>'); }
                row('Target', (t.target_type || '?') + ' #' + (t.target_id || '?'));
                if (t.service_id) row('Service', t.service_id);
                if (t.server_id) row('Server', t.server_id);
                row('Priority', t.priority);
                row('Attempts', t.attempts + ' / ' + t.max_attempts);
                if (t.parent_task_id) row('Parent', '#' + t.parent_task_id);
                row('Created', t.created_at);
                row('Started', t.started_at);
                row('Finished', t.finished_at);
                if (t.payload) row('Payload', JSON.stringify(t.payload));

                if (t.error_message) {
                    $('#puq-tasklog-error').show().text(t.error_message);
                } else {
                    $('#puq-tasklog-error').hide();
                }

                var log = t.log || '(no log yet)';
                $('#puq-tasklog-bytes').text(log === '(no log yet)' ? '' : (log.length + ' bytes'));
                var $body = $('#puq-tasklog-body');
                var atBottom = $body[0].scrollTop + $body[0].clientHeight >= $body[0].scrollHeight - 30;
                $body.text(log);
                if (atBottom) $body[0].scrollTop = $body[0].scrollHeight; // tail-follow

                if (taskLogTimer) { clearTimeout(taskLogTimer); taskLogTimer = null; }
                if ($('#puq-tasklog-autorefresh').is(':checked')
                    && (t.status === 'queued' || t.status === 'running')) {
                    taskLogTimer = setTimeout(loadTaskLog, 2000);
                }
            },
            error: function() { $('#puq-tasklog-body').text('Request failed.'); }
        });
    }

    $(document).on('click', '#puq-tasklog-refresh', function() {
        var btn = this; btnSpinner(btn);
        loadTaskLog();
        setTimeout(function(){ btnRestore(btn, 'sync', 'Refresh'); }, 400);
    });
    $(document).on('hidden.bs.modal', '#puq-tasklog-modal', function() {
        taskLogId = 0;
        if (taskLogTimer) { clearTimeout(taskLogTimer); taskLogTimer = null; }
    });
})();
</script>
{/literal}
