<?php

/**

 * Direct injection into fraud management page

 * This ensures the UI loads regardless of page structure

 */



// Use multiple hooks to ensure it loads

add_action('admin_head', 'sensfrx_inject_fmc_styles');

add_action('admin_footer', 'sensfrx_inject_fmc_html');

add_action('admin_print_footer_scripts', 'sensfrx_inject_fmc_scripts');



function sensfrx_inject_fmc_styles() {

    // Check if we're on the fraud page

    $screen = get_current_screen();

    $is_fraud_page = (isset($_GET['page']) && $_GET['page'] === 'sensfrx_fpwoo' && isset($_GET['tab']) && $_GET['tab'] === 'fraud');



    if (!$is_fraud_page && !strpos($screen->id, 'sensfrx') !== false) {

        return;

    }

    ?>

    <style>

    /* Fraud Management Center - Complete Styles */

    .fmc-wrapper {

        margin: 20px 0;

        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;

    }



    .fmc-header {

        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

        border-radius: 16px;

        padding: 30px;

        margin-bottom: 30px;

        color: white;

        box-shadow: 0 10px 40px rgba(0,0,0,0.15);

    }



    .fmc-title {

        font-size: 28px;

        font-weight: 600;

        margin-bottom: 24px;

        display: flex;

        align-items: center;

        gap: 12px;

    }



    .fmc-stats {

        display: grid;

        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));

        gap: 16px;

    }



    .fmc-stat {

        background: rgba(255,255,255,0.95);

        padding: 20px;

        border-radius: 12px;

        backdrop-filter: blur(10px);

    }



    .fmc-stat-value {

        font-size: 32px;

        font-weight: 700;

        color: #1a202c;

        line-height: 1;

        margin-bottom: 8px;

    }



    .fmc-stat-label {

        font-size: 13px;

        color: #64748b;

        text-transform: uppercase;

        letter-spacing: 0.5px;

    }



    .fmc-filters {

        background: white;

        border-radius: 16px;

        padding: 24px;

        box-shadow: 0 4px 20px rgba(0,0,0,0.08);

        margin-bottom: 24px;

    }



    .fmc-filter-row {

        margin-bottom: 20px;

    }



    .fmc-filter-label {

        font-size: 12px;

        font-weight: 600;

        text-transform: uppercase;

        color: #64748b;

        letter-spacing: 0.5px;

        margin-bottom: 10px;

        display: block;

    }



    .fmc-date-pills {

        display: flex;

        gap: 8px;

        padding: 4px;

        background: #f1f5f9;

        border-radius: 10px;

    }



    .fmc-pill {

        padding: 10px 16px;

        background: transparent;

        border: none;

        border-radius: 8px;

        cursor: pointer;

        font-size: 14px;

        font-weight: 500;

        color: #64748b;

        transition: all 0.2s;

    }



    .fmc-pill:hover {

        background: rgba(255,255,255,0.5);

    }



    .fmc-pill.active {

        background: white;

        color: #667eea;

        box-shadow: 0 2px 8px rgba(0,0,0,0.1);

    }



    .fmc-search-box {

        position: relative;

    }



    .fmc-search {

        width: 100%;

        padding: 12px 16px 12px 44px;

        border: 2px solid #e2e8f0;

        border-radius: 10px;

        font-size: 14px;

    }



    .fmc-search:focus {

        outline: none;

        border-color: #667eea;

        box-shadow: 0 0 0 3px rgba(102,126,234,0.1);

    }



    .fmc-search-icon {

        position: absolute;

        left: 16px;

        top: 14px;

        color: #94a3b8;

    }



    .fmc-risk-cards {

        display: grid;

        grid-template-columns: repeat(3, 1fr);

        gap: 12px;

    }



    .fmc-risk-card {

        padding: 16px;

        border: 2px solid #e2e8f0;

        border-radius: 12px;

        text-align: center;

        cursor: pointer;

        transition: all 0.2s;

    }



    .fmc-risk-card:hover {

        transform: translateY(-2px);

        box-shadow: 0 4px 12px rgba(0,0,0,0.08);

    }



    .fmc-risk-card.active {

        border-color: #667eea;

        background: #f0f4ff;

    }



    .fmc-risk-high { border-left: 4px solid #ef4444; }

    .fmc-risk-medium { border-left: 4px solid #f59e0b; }

    .fmc-risk-low { border-left: 4px solid #10b981; }



    .fmc-export-btn {

        padding: 10px 20px;

        background: #10b981;

        color: white;

        border: none;

        border-radius: 8px;

        font-weight: 500;

        cursor: pointer;

        transition: all 0.2s;

    }



    .fmc-export-btn:hover {

        background: #059669;

        transform: translateY(-1px);

    }

    </style>

    <?php

}



function sensfrx_inject_fmc_html() {

    $is_fraud_page = (isset($_GET['page']) && $_GET['page'] === 'sensfrx_fpwoo' && isset($_GET['tab']) && $_GET['tab'] === 'fraud');



    if (!$is_fraud_page) {

        return;

    }

    ?>

    <div id="fmc-injection-point"></div>

    <?php

}



function sensfrx_inject_fmc_scripts() {

    $is_fraud_page = (isset($_GET['page']) && $_GET['page'] === 'sensfrx_fpwoo' && isset($_GET['tab']) && $_GET['tab'] === 'fraud');



    if (!$is_fraud_page) {

        return;

    }

    ?>

    <script>

    jQuery(document).ready(function($) {



        const fmcHTML = `

        <div class="fmc-wrapper">

            <!-- Header Dashboard -->

            <div class="fmc-header">

                <div class="fmc-title">

                    <span style="font-size: 32px;">🛡️</span>

                    <span>Fraud Management Center</span>

                </div>

                <div class="fmc-stats">

                    <div class="fmc-stat">

                        <div class="fmc-stat-value">1,247</div>

                        <div class="fmc-stat-label">Total Cases</div>

                    </div>

                    <div class="fmc-stat">

                        <div class="fmc-stat-value" style="color: #ef4444;">523</div>

                        <div class="fmc-stat-label">Blocked</div>

                    </div>

                    <div class="fmc-stat">

                        <div class="fmc-stat-value" style="color: #10b981;">$47,392</div>

                        <div class="fmc-stat-label">Saved</div>

                    </div>

                    <div class="fmc-stat">

                        <div class="fmc-stat-value" style="color: #f59e0b;">18</div>

                        <div class="fmc-stat-label">Review</div>

                    </div>

                </div>

            </div>



            <!-- Filter Panel -->

            <div class="fmc-filters">

                <!-- Date Range -->

                <div class="fmc-filter-row">

                    <label class="fmc-filter-label">📅 Time Period</label>

                    <div class="fmc-date-pills">

                        <button class="fmc-pill" data-range="today">Today</button>

                        <button class="fmc-pill" data-range="yesterday">Yesterday</button>

                        <button class="fmc-pill active" data-range="week">This Week</button>

                        <button class="fmc-pill" data-range="month">This Month</button>

                        <button class="fmc-pill" data-range="3months">3 Months</button>

                    </div>

                </div>



                <!-- Risk Level -->

                <div class="fmc-filter-row">

                    <label class="fmc-filter-label">⚡ Risk Level</label>

                    <div class="fmc-risk-cards">

                        <div class="fmc-risk-card fmc-risk-high active">

                            <div style="font-size: 24px;">🔴</div>

                            <div style="font-weight: 600;">High Risk</div>

                            <div style="font-size: 12px; color: #64748b;">60-100</div>

                        </div>

                        <div class="fmc-risk-card fmc-risk-medium active">

                            <div style="font-size: 24px;">🟡</div>

                            <div style="font-weight: 600;">Medium Risk</div>

                            <div style="font-size: 12px; color: #64748b;">30-59</div>

                        </div>

                        <div class="fmc-risk-card fmc-risk-low">

                            <div style="font-size: 24px;">🟢</div>

                            <div style="font-weight: 600;">Low Risk</div>

                            <div style="font-size: 12px; color: #64748b;">0-29</div>

                        </div>

                    </div>

                </div>



                <!-- Search -->

                <div class="fmc-filter-row">

                    <label class="fmc-filter-label">🔍 Search</label>

                    <div class="fmc-search-box">

                        <span class="fmc-search-icon">🔍</span>

                        <input type="text" class="fmc-search" placeholder="Search by email, IP, order ID...">

                    </div>

                </div>



                <!-- Export Button -->

                <div class="fmc-filter-row" style="text-align: right;">

                    <button class="fmc-export-btn">📊 Export Report</button>

                </div>

            </div>

        </div>

        `;



        // Try multiple injection points

        let injected = false;



        // Method 1: After the title

        const $title = $('.wrap h1:contains("Fraud Management")');

        if ($title.length && !injected) {

            $title.after(fmcHTML);

            injected = true;

        }



        // Method 2: Before the tabs

        if (!injected) {

            const $tabs = $('.subsubsub');

            if ($tabs.length) {

                $tabs.before(fmcHTML);

                injected = true;

            }

        }



        // Method 3: At the beginning of wrap

        if (!injected) {

            const $wrap = $('.wrap').first();

            if ($wrap.length) {

                $wrap.prepend(fmcHTML);

                injected = true;

            }

        }



        // Initialize interactions

        if (injected) {

            // Date pills

            $('.fmc-pill').on('click', function() {

                $('.fmc-pill').removeClass('active');

                $(this).addClass('active');

            });



            // Risk cards

            $('.fmc-risk-card').on('click', function() {

                $(this).toggleClass('active');

            });



            // Search

            $('.fmc-search').on('keyup', function() {

            });



            // Export

            $('.fmc-export-btn').on('click', function() {

                if (typeof exportFraudReport === 'function') {

                    exportFraudReport();

                } else {

                    alert('Export functionality is loading. Please try again in a moment.');

                }

            });



        } else {

        }

    });

    </script>

    <?php

}