<?php

if (!defined('ABSPATH')) {
    exit;
}

/** @var array $rows */
?>

<div class="wrap">
    <h1><?php esc_html_e('Journal des échanges Peppol', 'esi_peppol'); ?></h1>

    <p>
        <?php esc_html_e('Voici les derniers enregistrements synchronisés avec l\'API ESIPeppol.', 'esi_peppol'); ?>
    </p>

    <div id="esi-peppol-logs-result"></div>

    <?php if (empty($rows)) : ?>
        <p><?php esc_html_e('Aucun enregistrement trouvé pour le moment.', 'esi_peppol'); ?></p>
    <?php else : ?>
        <table class="widefat fixed striped" id="esi-peppol-logs-table">
            <thead>
            <tr>
                <th><?php esc_html_e('ID', 'esi_peppol'); ?></th>
                <th><?php esc_html_e('Commande', 'esi_peppol'); ?></th>
                <th><?php esc_html_e('Document ID', 'esi_peppol'); ?></th>
                <th><?php esc_html_e('Statut', 'esi_peppol'); ?></th>
                <th><?php esc_html_e('Succès', 'esi_peppol'); ?></th>
                <th><?php esc_html_e('Date ajout', 'esi_peppol'); ?></th>
                <th><?php esc_html_e('Dernière mise à jour', 'esi_peppol'); ?></th>
                <th><?php esc_html_e('Actions', 'esi_peppol'); ?></th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($rows as $row) : ?>
                <tr>
                    <td><?php echo esc_html($row->id); ?></td>
                    <td>
                        <?php if ($row->id_order) : ?>
                            <?php
                            $order = wc_get_order($row->id_order);
                            $customer_name = '';
                            if ($order) {
                                $first_name = $order->get_billing_first_name();
                                $last_name = $order->get_billing_last_name();
                                $customer_name = trim($first_name . ' ' . $last_name);
                            }
                            ?>
                            <a href="<?php echo esc_url(get_edit_post_link($row->id_order)); ?>" target="_blank">
                                #<?php echo esc_html($row->id_order); ?>
                                <?php if ($customer_name) : ?>
                                    - <?php echo esc_html($customer_name); ?>
                                <?php endif; ?>
                            </a>
                        <?php else : ?>
                            <?php echo esc_html($row->id_order); ?>
                        <?php endif; ?>
                    </td>
                    <td><?php echo esc_html($row->document_id); ?></td>
                    <td>
                        <span class="statut statut-<?php echo esc_attr(strtolower(sanitize_html_class($row->status))); ?>">
                            <?php echo esc_html($row->status); ?>
                        </span>
                    </td>
                    <td>
                        <?php
                        echo $row->success
                            ? '<span style="color:green;">' . esc_html__('Oui', 'esi_peppol') . '</span>'
                            : '<span style="color:red;">' . esc_html__('Non', 'esi_peppol') . '</span>';
                        ?>
                    </td>
                    <td><?php echo esc_html($row->date_add); ?></td>
                    <td><?php echo esc_html($row->date_update); ?></td>
                    <td>
                        <button
                            type="button"
                            class="button button-secondary esi-peppol-log-detail"
                            data-log-id="<?php echo esc_attr($row->id); ?>"
                        >
                            <?php esc_html_e('Détail', 'esi_peppol'); ?>
                        </button>
                        <button
                            type="button"
                            class="button button-secondary esi-peppol-log-resend"
                            data-order-id="<?php echo esc_attr($row->id_order); ?>"
                        >
                            <?php esc_html_e('Renvoyer', 'esi_peppol'); ?>
                        </button>
                        <button
                            type="button"
                            class="button button-secondary esi-peppol-log-status"
                            data-order-id="<?php echo esc_attr($row->id_order); ?>"
                        >
                            <?php esc_html_e('Status', 'esi_peppol'); ?>
                        </button>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>

<?php
// Inclusion de la modal de détails
$modal_template = ESI_PEPPOL_DIR . 'templates/modules/logs_details_modal.php';
if (file_exists($modal_template)) {
    include $modal_template;
}
?>

