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

function aipd_orders_page() { ?>
    <div class="wrap aipd-orders-wrap">
        <h1 class="aipd-heading">
            <span class="dashicons dashicons-cart"></span>
            <?php esc_html_e('Awesome Documents – Orders', 'awesome-invoice-delivery-notes-packing-slips'); ?>
        </h1>

        <div class="aipd-orders-card">
            <table id="aipd-orders-table" class="widefat striped">
                <thead>
                    <tr>
                        <th><?php esc_html_e('Order ID', 'awesome-invoice-delivery-notes-packing-slips'); ?></th>
                        <th><?php esc_html_e('Customer', 'awesome-invoice-delivery-notes-packing-slips'); ?></th>
                        <th><?php esc_html_e('Total', 'awesome-invoice-delivery-notes-packing-slips'); ?></th>
                        <th><?php esc_html_e('Status', 'awesome-invoice-delivery-notes-packing-slips'); ?></th>
                        <th><?php esc_html_e('Actions', 'awesome-invoice-delivery-notes-packing-slips'); ?></th>
                    </tr>
                </thead>
                <tbody>
                <?php
                $orders    = wc_get_orders(['limit' => 100, 'orderby' => 'date', 'order' => 'DESC']);
                $templates = [
                    'invoice'      => get_option('aipd_template_invoice', 'default'),
                    'packing_slip' => get_option('aipd_template_package', 'default'),
                    'delivery_note'=> get_option('aipd_template_delivery', 'default'),
                ];

                foreach ($orders as $order) {
                    $id = $order->get_id();
                    $nonce = wp_create_nonce('aipd_generate_document_' . $id);
                    ?>
                    <tr>
                        <td><?php echo esc_html($id); ?></td>
                        <td><?php echo esc_html($order->get_formatted_billing_full_name()); ?></td>
                        <td><?php echo wp_kses_post($order->get_formatted_order_total()); ?></td>
                        <td><?php echo esc_html(wc_get_order_status_name($order->get_status())); ?></td>
                        <td class="aipd-actions">
                            <?php
                            $labels = [
                                'invoice'      => ['text' => __('Invoice', 'awesome-invoice-delivery-notes-packing-slips'), 'icon' => 'media-text'],
                                'packing_slip' => ['text' => __('Packing Slip', 'awesome-invoice-delivery-notes-packing-slips'), 'icon' => 'archive'],
                                'delivery_note'=> ['text' => __('Delivery Note', 'awesome-invoice-delivery-notes-packing-slips'), 'icon' => 'forms'],
                            ];
                            foreach ($labels as $action => $meta) {
                                $tpl = $templates[$action] ?? 'default';
                                printf(
                                    '<a href="%s" target="_blank" class="button button-small aipd-btn"><span class="dashicons dashicons-%s"></span>%s</a>',
                                    esc_url(admin_url('admin-post.php?action=aipd_generate_' . $action . '&order_id=' . $id . '&template=' . $tpl . '&_wpnonce=' . $nonce)),
                                    esc_attr($meta['icon']),
                                    esc_html($meta['text'])
                                );
                            }
                            ?>
                        </td>
                    </tr>
                    <?php
                } ?>
                </tbody>
            </table>
        </div>
    </div>

    <style>
    /* Container & heading */
    .aipd-heading { display:flex; align-items:center; gap:6px; }
    .aipd-orders-card { background:#fff; padding:20px; border-radius:8px; box-shadow:0 2px 6px rgba(0,0,0,0.1); margin-top:20px; }
    /* DataTable tweaks */
    #aipd-orders-table th { background:#f0f0f1; position:sticky; top:0; z-index:2; }
    #aipd-orders-table tbody tr:hover { background:#f9fafc; }
    .aipd-actions .aipd-btn { margin-right:6px; display:inline-flex; align-items:center; gap:4px; }
    .aipd-top { margin-bottom:10px; }
    .dt-buttons .button { margin-right:4px; }
    </style>
<?php }

/**
 * Enqueue Orders Page Scripts & Styles
 */
add_action('admin_enqueue_scripts', 'aipd_enqueue_admin_orders_assets');
function aipd_enqueue_admin_orders_assets($hook) {
    // Replace with your actual menu slug
    if ($hook !== 'toplevel_page_aipd_orders') 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 (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
    $inline_js = <<<JS
jQuery(function($){
    $('#aipd-orders-table').DataTable({
        dom: '<"aipd-top"Bf>rt<"aipd-bottom"lip>',
        pageLength: 25,
        order: [[0,'desc']],
        buttons: [
            { extend: 'copy', className: 'button' },
            { extend: 'csv',  className: 'button' },
            { extend: 'excel',className: 'button' },
            { extend: 'print',className: 'button' }
        ]
    });
});
JS;
    wp_add_inline_script('aipd-datatables-js', $inline_js);
}
