<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title pull-left"><i class="fa fa-bug"></i> Debug Logging</h3>
        <button type="button" class="btn btn-primary btn-sm pull-right" id="puq-debug-save"><i class="fas fa-save"></i> Save</button>
        <div class="clearfix"></div>
    </div>
    <div class="panel-body" style="position:relative;">
        <div id="puq-debug-overlay" style="display:block;position:absolute;top:0;left:0;width:100%;height:100%;background:rgba(255,255,255,0.9);z-index:9999;border-radius:4px;">
            <div style="position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);text-align:center;">
                <i class="fa fa-spinner fa-spin fa-3x text-primary"></i>
                <div style="margin-top:10px;font-size:16px;">Loading...</div>
            </div>
        </div>

        <div class="alert alert-info">
            <i class="fa fa-info-circle"></i>
            By default this module writes <strong>nothing</strong> to the WHMCS Module Log
            (<em>Utilities → Logs → Module Log</em>) — the only exception is license verification.
            Turn on the master switch and the specific categories below to capture targeted diagnostics there.
            Each enabled category writes Module Log rows tagged <code>[category]</code>.
            <strong>Remember to switch debugging back off</strong> once you're done — some categories (SSH) are very verbose.
        </div>

        <form id="puq-debug-form">
            <div class="form-group" style="padding:12px;border:1px solid #ddd;border-radius:4px;background:#fafafa;">
                <div class="checkbox" style="margin:0;">
                    <label style="font-size:16px;">
                        <input type="checkbox" name="debug_enabled" id="puq-debug-master" value="1">
                        <strong>Enable debug logging (master switch)</strong>
                    </label>
                </div>
                <span class="help-block" style="margin-bottom:0;">When this is off, every category below is inactive regardless of its checkbox.</span>
            </div>

            <hr>

            <table class="table table-bordered table-condensed">
                <thead>
                    <tr>
                        <th style="width:60px;text-align:center;">On</th>
                        <th style="width:230px;">Category</th>
                        <th>What it logs</th>
                    </tr>
                </thead>
                <tbody id="puq-debug-tbody"></tbody>
            </table>
        </form>
    </div>
</div>

{literal}
<script>
(function() {
    var baseUrl = '{/literal}{$meta.admin_module_url}&ajax={literal}';

    function buildTable(categories) {
        var $tbody = $('#puq-debug-tbody').empty();
        if (!categories) return;
        Object.keys(categories).forEach(function(key) {
            var c = categories[key];
            $tbody.append(
                '<tr>' +
                '<td style="text-align:center;vertical-align:middle;">' +
                    '<input type="checkbox" name="debug[' + key + ']" value="1" class="puq-debug-cat"' + (c.enabled ? ' checked' : '') + '>' +
                '</td>' +
                '<td><strong>' + esc(c.label) + '</strong><br><small class="text-muted"><code>debug_' + esc(key) + '</code></small></td>' +
                '<td><small>' + esc(c.desc) + '</small></td>' +
                '</tr>'
            );
        });
    }

    function applyMasterState() {
        var on = $('#puq-debug-master').is(':checked');
        $('.puq-debug-cat').prop('disabled', !on);
        $('#puq-debug-tbody').css('opacity', on ? '1' : '0.5');
    }

    $.ajax({
        url: baseUrl + 'getSettingsDebug', dataType: 'json',
        success: function(r) {
            if (r.status === 'success' && r.data) {
                $('#puq-debug-master').prop('checked', !!r.data.master);
                buildTable(r.data.categories);
                applyMasterState();
            } else {
                showNotification(r.msg || 'Failed to load debug settings', 'error');
            }
            $('#puq-debug-overlay').hide();
        },
        error: function() {
            showNotification('Failed to load debug settings', 'error');
            $('#puq-debug-overlay').hide();
        }
    });

    $(document).on('change', '#puq-debug-master', applyMasterState);

    $('#puq-debug-save').on('click', function() {
        // Re-enable disabled checkboxes briefly so their unchecked state is
        // still submitted correctly (disabled inputs are not serialized; our
        // save handler treats absence as off, so this is belt-and-braces).
        var reenable = $('.puq-debug-cat:disabled').prop('disabled', false);
        formSubmit({
            btn: this,
            form: '#puq-debug-form',
            baseUrl: baseUrl,
            action: 'saveSettingsDebug',
            onSuccess: function() { applyMasterState(); }
        });
    });
})();
</script>
{/literal}
