<?php
if (!defined('ABSPATH')) exit;

/**
 * Admin Reports Page for Awesome Invoice Plugin
 */
function aipd_reports_page() {

    // Fetch WooCommerce orders
    $orders = wc_get_orders([
        'limit'   => 50,
        'orderby' => 'date',
        'order'   => 'DESC',
    ]);
    ?>
    <div class="wrap">
        <h1><span class="dashicons dashicons-chart-bar"></span> Plugin Reports</h1>

        <div class="aipd-tabs">
            <div class="aipd-tab-buttons">
                <button class="active" data-tab="invoices">Invoices</button>
                <button data-tab="packages">Packages</button>
                <button data-tab="delivery">Delivery Notes</button>
            </div>

            <?php
            $tabs = [
                'invoices' => ['title'=>'Invoices','meta'=>'_aipd_invoice_number'],
                'packages' => ['title'=>'Packages','meta'=>'_aipd_package_number'],
                'delivery'=> ['title'=>'Delivery Notes','meta'=>'_aipd_delivery_number']
            ];

            foreach($tabs as $tab_id => $tab):
            ?>
            <div id="<?php echo esc_attr($tab_id); ?>" class="aipd-tab-content <?php echo $tab_id=='invoices'?'active':''; ?>">
                <h2><?php echo esc_html($tab['title']); ?></h2>
                <table class="widefat striped aipd-report-table">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Order ID</th>
                            <th>Customer</th>
                            <th>Date</th>
                            <?php if($tab_id==='invoices'): ?><th>Total</th><?php endif; ?>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach($orders as $order): ?>
                        <tr>
                            <td><?php echo esc_html(get_post_meta($order->get_id(), $tab['meta'], true) ?: '-'); ?></td>
                            <td><?php echo esc_html($order->get_id()); ?></td>
                            <td><?php echo esc_html($order->get_formatted_billing_full_name()); ?></td>
                            <td><?php echo esc_html(wc_format_datetime($order->get_date_created())); ?></td>
                            <?php if($tab_id==='invoices'): ?><td><?php echo wp_kses_post($order->get_formatted_order_total()); ?></td><?php endif; ?>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php endforeach; ?>
        </div>
    </div>

    <style>
        .aipd-tabs { margin-top:20px; }
        .aipd-tab-buttons button {
            background:#f1f1f1; border:1px solid #ccc; padding:10px 20px; cursor:pointer; margin-right:2px; border-radius:4px 4px 0 0;
        }
        .aipd-tab-buttons button.active { background:#0073aa; color:#fff; }
        .aipd-tab-content { border:1px solid #ccc; padding:20px; display:none; border-radius:0 4px 4px 4px; background:#fff; }
        .aipd-tab-content.active { display:block; }
        .aipd-heading { display:flex; align-items:center; gap:8px; margin-bottom:10px; }
        .aipd-report-table .dt-buttons { margin-bottom:10px; }
    </style>
    <?php
}

/**
 * Enqueue Admin Scripts & Styles for Reports Page
 */
add_action('admin_enqueue_scripts', 'aipd_enqueue_admin_reports_assets');
function aipd_enqueue_admin_reports_assets($hook) {
    // Replace 'toplevel_page_aipd_reports' with your actual reports menu slug
    if ($hook !== 'toplevel_page_aipd_reports') return;

    // DataTables CSS
    wp_enqueue_style('aipd-datatables-css', 'https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css', [], '1.13.6');
    wp_enqueue_style('aipd-datatables-buttons-css', 'https://cdn.datatables.net/buttons/2.4.1/css/buttons.dataTables.min.css', ['aipd-datatables-css'], '2.4.1');

    // jQuery (already bundled with WP)
    wp_enqueue_script('jquery');

    // DataTables JS
    wp_enqueue_script('aipd-datatables-js', 'https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js', ['jquery'], '1.13.6', true);
    wp_enqueue_script('aipd-datatables-buttons-js', 'https://cdn.datatables.net/buttons/2.4.1/js/dataTables.buttons.min.js', ['aipd-datatables-js'], '2.4.1', true);
    wp_enqueue_script('aipd-datatables-buttons-html5-js', 'https://cdn.datatables.net/buttons/2.4.1/js/buttons.html5.min.js', ['aipd-datatables-buttons-js'], '2.4.1', true);
    wp_enqueue_script('aipd-datatables-buttons-print-js', 'https://cdn.datatables.net/buttons/2.4.1/js/buttons.print.min.js', ['aipd-datatables-buttons-js'], '2.4.1', true);

    // Inline JS for tabs & DataTables
    $inline_js = <<<JS
jQuery(document).ready(function($){
    // Tabs
    $('.aipd-tab-buttons button').click(function(){
        var tab = $(this).data('tab');
        $('.aipd-tab-buttons button').removeClass('active');
        $(this).addClass('active');
        $('.aipd-tab-content').removeClass('active');
        $('#' + tab).addClass('active');
    });

    // DataTables
    $('.aipd-report-table').DataTable({
        dom: 'Bfrtip',
        pageLength: 25,
        order: [[0,'desc']],
        buttons: ['copy','csv','excel','print']
    });
});
JS;
    wp_add_inline_script('aipd-datatables-js', $inline_js);
}
