<?php

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

/** @var array $log_data */
$logs_url = admin_url('admin.php?page=esi-peppol-logs');
?>

<div class="wrap">
    <h1><?php esc_html_e('Détails du log', 'esi_peppol'); ?></h1>

    <div class="esi-peppol-log-details-content">
        <?php
        // 1. Section Statut
        ?>
        <div class="esi-peppol-log-details-section">
            <h3><?php esc_html_e('Statut', 'esi_peppol'); ?></h3>
            <table class="widefat">
                <tr>
                    <th style="width: 200px;"><?php esc_html_e('Succès', 'esi_peppol'); ?></th>
                    <td>
                        <?php
                        echo $log_data['success']
                            ? '<span style="color:green;font-weight:bold;">' . esc_html__('Oui', 'esi_peppol') . '</span>'
                            : '<span style="color:red;font-weight:bold;">' . esc_html__('Non', 'esi_peppol') . '</span>';
                        ?>
                    </td>
                </tr>
                <tr>
                    <th><?php esc_html_e('Message', 'esi_peppol'); ?></th>
                    <td><?php echo esc_html($log_data['message'] ?: '-'); ?></td>
                </tr>
                <?php if (!empty($log_data['order_info']['number'])) : ?>
                    <tr>
                        <th><?php esc_html_e('Commande', 'esi_peppol'); ?></th>
                        <td>
                            <a href="<?php echo esc_url($log_data['order_info']['edit_link']); ?>" target="_blank">
                                #<?php echo esc_html($log_data['order_info']['number']); ?>
                            </a>
                        </td>
                    </tr>
                <?php elseif (!empty($log_data['id_order'])) : ?>
                    <tr>
                        <th><?php esc_html_e('Commande', 'esi_peppol'); ?></th>
                        <td>#<?php echo esc_html($log_data['id_order']); ?></td>
                    </tr>
                <?php endif; ?>
                <?php if (!empty($log_data['document_id'])) : ?>
                    <tr>
                        <th><?php esc_html_e('Document ID', 'esi_peppol'); ?></th>
                        <td><?php echo esc_html($log_data['document_id']); ?></td>
                    </tr>
                <?php endif; ?>
                <?php if (!empty($log_data['peppol_document_id'])) : ?>
                    <tr>
                        <th><?php esc_html_e('Peppol Document ID', 'esi_peppol'); ?></th>
                        <td><?php echo esc_html($log_data['peppol_document_id']); ?></td>
                    </tr>
                <?php endif; ?>
                <?php if (!empty($log_data['status'])) : ?>
                    <tr>
                        <th><?php esc_html_e('Statut', 'esi_peppol'); ?></th>
                        <td>
                            <span class="statut statut-<?php echo esc_attr(strtolower(sanitize_html_class($log_data['status']))); ?>">
                                <?php echo esc_html($log_data['status']); ?>
                            </span>
                        </td>
                    </tr>
                <?php endif; ?>
                <?php if (!empty($log_data['http_code'])) : ?>
                    <tr>
                        <th><?php esc_html_e('Code HTTP', 'esi_peppol'); ?></th>
                        <td><?php echo esc_html($log_data['http_code']); ?></td>
                    </tr>
                <?php endif; ?>
                <?php if (!empty($log_data['date_add'])) : ?>
                    <tr>
                        <th><?php esc_html_e('Date ajout', 'esi_peppol'); ?></th>
                        <td><?php echo esc_html($log_data['date_add']); ?></td>
                    </tr>
                <?php endif; ?>
                <?php if (!empty($log_data['date_update'])) : ?>
                    <tr>
                        <th><?php esc_html_e('Dernière mise à jour', 'esi_peppol'); ?></th>
                        <td><?php echo esc_html($log_data['date_update']); ?></td>
                    </tr>
                <?php endif; ?>
            </table>
        </div>

        <?php
        // 2. Section Données client
        if (!empty($log_data['customer_data']) || (!empty($log_data['order_info']) && (!empty($log_data['order_info']['billing_name']) || !empty($log_data['order_info']['billing_company'])))) :
            $customer = $log_data['customer_data'] ?? [];
            $order_info = $log_data['order_info'] ?? [];
            
            $customer_name = $customer['name'] ?? $order_info['billing_company'] ?? $order_info['billing_name'] ?? '-';
            $customer_legal_name = $customer['legal_name'] ?? $customer['name'] ?? $order_info['billing_company'] ?? $order_info['billing_name'] ?? '-';
            $customer_vat = $customer['vat_number'] ?? $order_info['billing_vat_number'] ?? '-';
            $customer_email = ($customer['contact']['contact_email'] ?? null) ?: ($order_info['billing_email'] ?? '-');
            $customer_phone = ($customer['contact']['contact_phone'] ?? null) ?: ($order_info['billing_phone'] ?? '-');
            
            $address = $customer['address'] ?? [];
            $address_line_1 = $address['address_line_1'] ?? $order_info['billing_address_1'] ?? '-';
            $address_line_2 = $address['address_line_2'] ?? $order_info['billing_address_2'] ?? '';
            $city = $address['city'] ?? $order_info['billing_city'] ?? '-';
            $postal_code = $address['postal_code'] ?? $order_info['billing_postcode'] ?? '-';
            $country_code = $address['country_code'] ?? $order_info['billing_country'] ?? '-';
            ?>
            <div class="esi-peppol-log-details-section">
                <h3><?php esc_html_e('Données client', 'esi_peppol'); ?></h3>
                <table class="widefat">
                    <tr>
                        <th style="width: 200px;"><?php esc_html_e('Nom / Raison sociale', 'esi_peppol'); ?></th>
                        <td><?php echo esc_html($customer_name); ?></td>
                    </tr>
                    <?php if ($customer_legal_name && $customer_legal_name !== $customer_name) : ?>
                        <tr>
                            <th><?php esc_html_e('Nom légal', 'esi_peppol'); ?></th>
                            <td><?php echo esc_html($customer_legal_name); ?></td>
                        </tr>
                    <?php endif; ?>
                    <tr>
                        <th><?php esc_html_e('Numéro TVA', 'esi_peppol'); ?></th>
                        <td><?php echo esc_html($customer_vat); ?></td>
                    </tr>
                    <tr>
                        <th><?php esc_html_e('Email', 'esi_peppol'); ?></th>
                        <td><?php echo esc_html($customer_email); ?></td>
                    </tr>
                    <?php if ($customer_phone && $customer_phone !== '-') : ?>
                        <tr>
                            <th><?php esc_html_e('Téléphone', 'esi_peppol'); ?></th>
                            <td><?php echo esc_html($customer_phone); ?></td>
                        </tr>
                    <?php endif; ?>
                    <tr>
                        <th><?php esc_html_e('Adresse', 'esi_peppol'); ?></th>
                        <td>
                            <?php echo esc_html($address_line_1); ?>
                            <?php if ($address_line_2) : ?>
                                <br><?php echo esc_html($address_line_2); ?>
                            <?php endif; ?>
                            <br><?php echo esc_html($postal_code . ' ' . $city); ?>
                            <br><?php echo esc_html($country_code); ?>
                        </td>
                    </tr>
                </table>
            </div>
        <?php endif; ?>

        <?php
        // 3. Section Détails TVA par taux
        if (!empty($log_data['vat_totals']) && is_array($log_data['vat_totals']) && count($log_data['vat_totals']) > 0) :
            ?>
            <div class="esi-peppol-log-details-section">
                <h3><?php esc_html_e('Détails TVA', 'esi_peppol'); ?></h3>
                <table class="widefat">
                    <thead>
                        <tr>
                            <th><?php esc_html_e('Taux TVA', 'esi_peppol'); ?></th>
                            <th><?php esc_html_e('Montant HTVA', 'esi_peppol'); ?></th>
                            <th><?php esc_html_e('Montant TVA', 'esi_peppol'); ?></th>
                            <th><?php esc_html_e('Montant TVAC', 'esi_peppol'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($log_data['vat_totals'] as $vat) : ?>
                            <?php
                            $vat_rate = floatval($vat['vat_rate'] ?? 0);
                            $taxable_amount = floatval($vat['taxable_amount'] ?? 0);
                            $vat_amount = floatval($vat['vat_amount'] ?? 0);
                            $total_incl_vat = $taxable_amount + $vat_amount;
                            ?>
                            <tr>
                                <td><?php echo esc_html(number_format($vat_rate, 2, ',', ' ') . ' %'); ?></td>
                                <td><?php echo esc_html(number_format($taxable_amount, 2, ',', ' ') . ' €'); ?></td>
                                <td><?php echo esc_html(number_format($vat_amount, 2, ',', ' ') . ' €'); ?></td>
                                <td><?php echo esc_html(number_format($total_incl_vat, 2, ',', ' ') . ' €'); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>

        <?php
        // 4. Section Totaux
        if (!empty($log_data['invoice_totals'])) :
            $totals = $log_data['invoice_totals'];
            $total_htva = floatval($totals['total_amount_excluding_vat'] ?? 0);
            $total_tva = floatval($totals['total_vat_amount'] ?? 0);
            $total_tvac = floatval($totals['total_amount_including_vat'] ?? 0);
            ?>
            <div class="esi-peppol-log-details-section">
                <h3><?php esc_html_e('Totaux', 'esi_peppol'); ?></h3>
                <table class="widefat">
                    <tr>
                        <th style="width: 200px;"><?php esc_html_e('Total HTVA', 'esi_peppol'); ?></th>
                        <td><strong><?php echo esc_html(number_format($total_htva, 2, ',', ' ') . ' €'); ?></strong></td>
                    </tr>
                    <tr>
                        <th><?php esc_html_e('Total TVA', 'esi_peppol'); ?></th>
                        <td><strong><?php echo esc_html(number_format($total_tva, 2, ',', ' ') . ' €'); ?></strong></td>
                    </tr>
                    <tr>
                        <th><?php esc_html_e('Total TVAC', 'esi_peppol'); ?></th>
                        <td><strong><?php echo esc_html(number_format($total_tvac, 2, ',', ' ') . ' €'); ?></strong></td>
                    </tr>
                    <?php if (!empty($totals['total_payable_amount'])) : ?>
                        <?php $payable_amount = floatval($totals['total_payable_amount']); ?>
                        <tr>
                            <th><?php esc_html_e('Montant à payer', 'esi_peppol'); ?></th>
                            <td><?php echo esc_html(number_format($payable_amount, 2, ',', ' ') . ' €'); ?></td>
                        </tr>
                    <?php endif; ?>
                </table>
            </div>
        <?php endif; ?>
    </div>

    <div class="esi-peppol-log-details-actions" style="margin-top: 30px; padding-top: 20px; border-top: 1px solid #ddd;">
        <a href="<?php echo esc_url($logs_url); ?>" class="button button-secondary">
            <span class="dashicons dashicons-arrow-left-alt" style="vertical-align: middle;"></span>
            <?php esc_html_e('Retour vers les logs', 'esi_peppol'); ?>
        </a>
        <?php if (!empty($log_data['id_order'])) : ?>
            <button
                type="button"
                class="button button-primary esi-peppol-log-resend"
                data-order-id="<?php echo esc_attr($log_data['id_order']); ?>"
                style="margin-left: 10px;"
            >
                <span class="dashicons dashicons-update" style="vertical-align: middle;"></span>
                <?php esc_html_e('Envoyer de nouveau', 'esi_peppol'); ?>
            </button>
        <?php endif; ?>
    </div>

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