<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
/**
 * Support / Report a Problem panel.
 * Reply email is auto-resolved from the license record — no input needed.
 */

// Resolve email server-side so it's available for WP-mode AJAX fallback
$_tkt_email = '';
if (defined('INSTINCTOR_ENV') && INSTINCTOR_ENV !== 'wordpress') {
    $_tkt_email = \Instinctor\Core\Config::get('admin_email', '');
} elseif (function_exists('wp_get_current_user')) {
    $_tkt_user  = wp_get_current_user();
    $_tkt_email = $_tkt_user->user_email ?? '';
}
?>
<div id="instinctor-support-panel" style="padding:0 0 4px">

    <div id="tkt-success" style="display:none;background:#1a2520;border:1px solid #2a4a3a;border-radius:3px;padding:8px 10px;margin-bottom:10px;font-size:11px;color:#6abf8e"></div>
    <div id="tkt-error"   style="display:none;background:#251a1a;border:1px solid #4a2a2a;border-radius:3px;padding:8px 10px;margin-bottom:10px;font-size:11px;color:#bf6a6a"></div>

    <div style="margin-bottom:5px">
        <select id="tkt-type" style="width:100%;height:26px;background:#2a2e33;border:1px solid #3a3f44;border-radius:3px;color:#e0e0e0;padding:0 8px;font-size:11px;box-sizing:border-box;box-shadow:none;outline:none;-webkit-appearance:none;appearance:none">
            <option value="bug">Bug / something broken</option>
            <option value="question">Question / how-to</option>
            <option value="feature">Feature request</option>
            <option value="other">Other</option>
        </select>
    </div>

    <div style="margin-bottom:5px">
        <textarea id="tkt-body" rows="1"
                  style="width:100%;height:26px;min-height:26px;background:#2a2e33;border:1px solid #3a3f44;border-radius:3px;color:#e0e0e0;padding:4px 8px;font-size:11px;box-sizing:border-box;resize:none;line-height:1.5;overflow:hidden"></textarea>
    </div>

    <button type="button" id="tkt-submit" class="impulse-btn-sm" style="width:100%;height:26px">Submit ticket</button>

</div>

<script>
(function($) {
    $(document).on('input', '#tkt-body', function() {
        this.style.height = '26px';
        this.style.height = Math.max(26, this.scrollHeight) + 'px';
    });

    $(document).on('click', '#tkt-submit', function() {
        var btn  = $(this);
        var type = $('#tkt-type').val();
        var body = $('#tkt-body').val().trim();

        $('#tkt-success, #tkt-error').hide();

        if (!body) {
            $('#tkt-error').text('Please enter a description.').show();
            return;
        }

        btn.prop('disabled', true).text('Submitting…');

        var subject = type + ' report';

        // Single channel for BOTH standalone and WordPress: the
        // instinctor_submit_ticket AJAX action (registered via AjaxDispatcher
        // on standalone, wp_ajax on WP) forwards to platform /api/report. The
        // old standalone branch posted to a REST route 'support/ticket' that
        // was never registered, so every standalone ticket silently 404'd.
        $.post(ajaxurl, {
            action:  'instinctor_submit_ticket',
            nonce:   window.Impulse.adminNonce,
            email:   (window.Impulse.userEmail || window.Impulse.licenseEmail || <?php echo wp_json_encode($_tkt_email); ?>),
            type: type, subject: subject, body: body
        }, function(res) {
            btn.prop('disabled', false).text('Submit ticket');
            if (res.success) {
                $('#tkt-body').val('');
                $('#tkt-success').text((res.data && res.data.message) || 'Ticket submitted — we\'ll be in touch.').show();
            } else {
                $('#tkt-error').text(res.data || 'Submission failed. Please try again.').show();
            }
        }).fail(function() {
            btn.prop('disabled', false).text('Submit ticket');
            $('#tkt-error').text('Network error. Email helpdesk@instinctor.com directly.').show();
        });
    });
})(jQuery);
</script>
