<?php
/**
 * Template email pour les erreurs API Peppol
 *
 * Variables disponibles :
 * @var int    $order_id      ID de la commande WooCommerce
 * @var string $order_number  Numéro de commande
 * @var int    $http_code     Code HTTP de la réponse
 * @var string $error_message Message d'erreur
 * @var array  $error_data    Données supplémentaires de l'erreur
 */

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

// Récupérer l'URL du logo
if (defined('ESI_PEPPOL_URL')) {
    $logo_url = ESI_PEPPOL_URL . 'assets/img/LOGO-ESI-PEPPOL_LIGHT.png';
} else {
    $logo_url = plugin_dir_url(dirname(dirname(__FILE__))) . 'assets/img/LOGO-ESI-PEPPOL_LIGHT.png';
}
$logo_url = esc_url($logo_url);

// Récupérer le nom du site
$site_name = get_bloginfo('name');
$site_url = home_url();

// Récupérer l'objet commande pour obtenir le total et les informations client
$order = wc_get_order($order_id);
$order_total = '';
$customer_name = '';
if ($order instanceof WC_Order) {
    $order_total = $order->get_formatted_order_total();
    $first_name = $order->get_billing_first_name();
    $last_name = $order->get_billing_last_name();
    if (!empty($first_name) || !empty($last_name)) {
        $customer_name = trim($first_name . ' ' . $last_name);
    }
}
?>
<!DOCTYPE html>
<html lang="fr" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="x-apple-disable-message-reformatting">
    <title><?php esc_html_e('Erreur API Peppol', 'esi_peppol'); ?></title>
    <!--[if mso]>
    <noscript>
        <xml>
            <o:OfficeDocumentSettings>
                <o:AllowPNG/>
                <o:PixelsPerInch>96</o:PixelsPerInch>
            </o:OfficeDocumentSettings>
        </xml>
    </noscript>
    <style>
        table { border-collapse: collapse; }
        .button-td, .button-a { transition: none !important; }
    </style>
    <![endif]-->
    <style>
        * { box-sizing: border-box; }
        body, table, td, p, a, li, blockquote {
            -webkit-text-size-adjust: 100%;
            -ms-text-size-adjust: 100%;
        }
        table, td {
            mso-table-lspace: 0pt;
            mso-table-rspace: 0pt;
        }
        img {
            -ms-interpolation-mode: bicubic;
            border: 0;
            height: auto;
            line-height: 100%;
            outline: none;
            text-decoration: none;
        }
        body {
            margin: 0 !important;
            padding: 0 !important;
            width: 100% !important;
            background-color: #f4f4f4;
        }
        a { text-decoration: none; }
        @media screen and (max-width: 600px) {
            .email-container { width: 100% !important; }
            .mobile-padding { padding: 20px 25px !important; }
            .header-title { font-size: 22px !important; }
        }
    </style>
</head>
<body style="margin: 0; padding: 0; background-color: #f4f4f4; font-family: Arial, Helvetica, sans-serif; mso-line-height-rule: exactly;">
    <!-- Wrapper table -->
    <table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%" style="background-color: #f4f4f4;">
        <tr>
            <td align="center" style="padding: 30px 15px;">
                
                <!-- Email container -->
                <table role="presentation" cellspacing="0" cellpadding="0" border="0" width="600" class="email-container" style="background-color: #ffffff; max-width: 600px;">
                    
                    <!-- HEADER -->
                    <tr>
                        <td bgcolor="#0B3C61" style="background-color: #0B3C61; padding: 35px 30px; text-align: center;">
                            <!--[if mso]>
                            <table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%">
                            <tr>
                            <td align="center" style="padding-bottom: 20px;">
                            <![endif]-->
                            <table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%">
                                <tr>
                                    <td align="center" style="padding-bottom: 20px;">
                                        <img src="<?php echo $logo_url; ?>" alt="ESI PEPPOL" width="130" style="display: block; max-width: 130px; height: auto; border: 0;">
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center">
                                        <p style="color: #a8c5db; font-size: 13px; margin: 0 0 8px 0; letter-spacing: 1px; text-transform: uppercase; font-family: Arial, Helvetica, sans-serif;">
                                            <?php esc_html_e('Erreur API', 'esi_peppol'); ?>
                                        </p>
                                        <h1 class="header-title" style="color: #ffffff; font-size: 26px; font-weight: bold; margin: 0; font-family: Arial, Helvetica, sans-serif;">
                                            ESI PEPPOL
                                        </h1>
                                    </td>
                                </tr>
                            </table>
                            <!--[if mso]>
                            </td>
                            </tr>
                            </table>
                            <![endif]-->
                        </td>
                    </tr>

                    <!-- CONTENT -->
                    <tr>
                        <td class="mobile-padding" style="padding: 40px 45px; background-color: #ffffff;">
                            
                            <!-- Greeting -->
                            <p style="color: #1a1a1a; font-size: 17px; font-weight: bold; margin: 0 0 25px 0; font-family: Arial, Helvetica, sans-serif;">
                                <?php esc_html_e('Erreur API Peppol', 'esi_peppol'); ?>
                            </p>

                            <!-- Main text -->
                            <p style="color: #333333; font-size: 15px; line-height: 24px; margin: 0 0 18px 0; font-family: Arial, Helvetica, sans-serif;">
                                <?php
                                printf(
                                    /* translators: %s: site name */
                                    esc_html__('Une erreur s\'est produite lors de l\'envoi d\'une facture vers le réseau Peppol sur %s.', 'esi_peppol'),
                                    esc_html($site_name)
                                );
                                ?>
                            </p>
                            
                            <!-- Détails de la commande -->
                            <table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%">
                                <tr>
                                    <td style="padding: 15px 18px; background-color: #f8f9fa; border-left: 4px solid #0B3C61; margin: 20px 0;">
                                        <h2 style="margin: 0 0 10px; color: #333333; font-size: 18px; font-weight: bold; font-family: Arial, Helvetica, sans-serif;">
                                            <?php esc_html_e('Détails de la commande', 'esi_peppol'); ?>
                                        </h2>
                                        <p style="margin: 0; color: #666666; font-size: 14px; line-height: 20px; font-family: Arial, Helvetica, sans-serif;">
                                            <strong><?php esc_html_e('Numéro de commande:', 'esi_peppol'); ?></strong> 
                                            <?php echo esc_html($order_number); ?><br>
                                            <strong><?php esc_html_e('ID de commande:', 'esi_peppol'); ?></strong> 
                                            <?php echo esc_html($order_id); ?><br>
                                            <?php if (!empty($customer_name)) : ?>
                                            <strong><?php esc_html_e('Client:', 'esi_peppol'); ?></strong> 
                                            <?php echo esc_html($customer_name); ?><br>
                                            <?php endif; ?>
                                            <?php if (!empty($order_total)) : ?>
                                            <strong><?php esc_html_e('Total:', 'esi_peppol'); ?></strong> 
                                            <?php echo $order_total; ?>
                                            <?php endif; ?>
                                        </p>
                                    </td>
                                </tr>
                            </table>
                            
                            <!-- Détails de l'erreur -->
                            <table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%">
                                <tr>
                                    <td style="padding: 15px 18px; background-color: #f8d7da; border-left: 4px solid #dc3545; margin: 20px 0;">
                                        <h2 style="margin: 0 0 10px; color: #721c24; font-size: 18px; font-weight: bold; font-family: Arial, Helvetica, sans-serif;">
                                            <?php esc_html_e('Détails de l\'erreur', 'esi_peppol'); ?>
                                        </h2>
                                        <p style="margin: 0 0 10px; color: #721c24; font-size: 14px; line-height: 20px; font-family: Arial, Helvetica, sans-serif;">
                                            <strong><?php esc_html_e('Code HTTP:', 'esi_peppol'); ?></strong> 
                                            <?php echo esc_html($http_code); ?>
                                        </p>
                                        <?php 
                                        // Extraire le message d'erreur depuis différentes structures possibles
                                        $error_message_to_display = $error_message;
                                        
                                        if (!empty($error_data) && is_array($error_data)) {
                                            // Structure avec error.message
                                            if (isset($error_data['error']['message'])) {
                                                $error_message_to_display = (string) $error_data['error']['message'];
                                            }
                                            // Structure avec details.validation_error
                                            elseif (isset($error_data['details']['validation_error'])) {
                                                $error_message_to_display = (string) $error_data['details']['validation_error'];
                                            }
                                            // Structure avec validation_error directement
                                            elseif (isset($error_data['validation_error'])) {
                                                $error_message_to_display = (string) $error_data['validation_error'];
                                            }
                                            // Structure avec message directement
                                            elseif (isset($error_data['message'])) {
                                                $error_message_to_display = (string) $error_data['message'];
                                            }
                                        }
                                        ?>
                                        
                                        <div style="margin: 15px 0; padding: 15px; background-color: rgba(255, 255, 255, 0.5); border: 2px solid #dc3545; border-radius: 4px;">
                                            <p style="margin: 0 0 10px; color: #721c24; font-size: 15px; font-weight: bold; font-family: Arial, Helvetica, sans-serif;">
                                                <?php esc_html_e('Message d\'erreur:', 'esi_peppol'); ?>
                                            </p>
                                            <p style="margin: 0; color: #721c24; font-size: 14px; line-height: 22px; font-family: Arial, Helvetica, sans-serif; white-space: pre-wrap; word-wrap: break-word;">
                                                <?php echo esc_html($error_message_to_display); ?>
                                            </p>
                                        </div>
                                        
                                        <?php if (!empty($error_data) && is_array($error_data)) : ?>
                                            <?php 
                                            // Extraire les détails supplémentaires si disponibles
                                            $error_code = '';
                                            $error_details = [];
                                            
                                            // Structure avec error.code et error.details
                                            if (isset($error_data['error'])) {
                                                if (isset($error_data['error']['code'])) {
                                                    $error_code = (string) $error_data['error']['code'];
                                                }
                                                if (isset($error_data['error']['details']) && is_array($error_data['error']['details'])) {
                                                    $error_details = $error_data['error']['details'];
                                                }
                                            }
                                            
                                            // Structure avec details directement
                                            if (isset($error_data['details']) && is_array($error_data['details'])) {
                                                if (empty($error_details)) {
                                                    $error_details = $error_data['details'];
                                                }
                                            }
                                            
                                            $document_id = isset($error_data['details']['document_id']) ? $error_data['details']['document_id'] : (isset($error_data['document_id']) ? $error_data['document_id'] : '');
                                            ?>
                                            
                                            <?php if (!empty($error_code)) : ?>
                                            <p style="margin: 10px 0; color: #721c24; font-size: 14px; line-height: 20px; font-family: Arial, Helvetica, sans-serif;">
                                                <strong><?php esc_html_e('Code d\'erreur:', 'esi_peppol'); ?></strong> 
                                                <?php echo esc_html($error_code); ?>
                                            </p>
                                            <?php endif; ?>
                                            
                                            <?php if (!empty($document_id)) : ?>
                                            <p style="margin: 10px 0; color: #721c24; font-size: 14px; line-height: 20px; font-family: Arial, Helvetica, sans-serif;">
                                                <strong><?php esc_html_e('ID du document:', 'esi_peppol'); ?></strong> 
                                                <?php echo esc_html($document_id); ?>
                                            </p>
                                            <?php endif; ?>
                                            
                                            <?php if (!empty($error_details) && is_array($error_details)) : ?>
                                                <?php 
                                                // Filtrer les détails pertinents à afficher
                                                $relevant_details = [];
                                                $excluded_keys = ['document_id', 'validation_error', 'processing_details'];
                                                
                                                foreach ($error_details as $key => $value) {
                                                    if (!in_array($key, $excluded_keys) && !empty($value)) {
                                                        $relevant_details[$key] = $value;
                                                    }
                                                }
                                                ?>
                                                
                                                <?php if (!empty($relevant_details)) : ?>
                                                <div style="margin: 10px 0; padding: 10px; background-color: rgba(255, 255, 255, 0.3); border-radius: 4px;">
                                                    <p style="margin: 0 0 8px; color: #721c24; font-size: 14px; font-weight: bold; font-family: Arial, Helvetica, sans-serif;">
                                                        <?php esc_html_e('Détails supplémentaires:', 'esi_peppol'); ?>
                                                    </p>
                                                    <?php foreach ($relevant_details as $key => $value) : ?>
                                                        <?php if (is_scalar($value)) : ?>
                                                        <p style="margin: 0 0 5px; color: #721c24; font-size: 13px; line-height: 18px; font-family: Arial, Helvetica, sans-serif;">
                                                            <strong><?php echo esc_html(ucfirst(str_replace('_', ' ', $key))); ?>:</strong> 
                                                            <?php echo esc_html($value); ?>
                                                        </p>
                                                        <?php endif; ?>
                                                    <?php endforeach; ?>
                                                </div>
                                                <?php endif; ?>
                                            <?php endif; ?>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            </table>
                            
                            <p style="color: #333333; font-size: 15px; line-height: 24px; margin: 22px 0 22px 0; font-family: Arial, Helvetica, sans-serif;">
                                <?php esc_html_e('Veuillez vérifier la configuration de votre connecteur Peppol et consulter les logs pour plus de détails.', 'esi_peppol'); ?>
                            </p>
                            
                            <!-- BUTTON - Compatible Outlook -->
                            <table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%">
                                <tr>
                                    <td align="center" style="padding: 8px 0 28px 0;">
                                        <!--[if mso]>
                                        <v:roundrect xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="urn:schemas-microsoft-com:office:word" href="<?php echo esc_url(admin_url('admin.php?page=esi-peppol-logs')); ?>" style="height:48px;v-text-anchor:middle;width:220px;" arcsize="10%" strokecolor="#0B3C61" fillcolor="#0B3C61">
                                        <w:anchorlock/>
                                        <center style="color:#ffffff;font-family:Arial,Helvetica,sans-serif;font-size:15px;font-weight:bold;"><?php esc_html_e('Consulter les logs', 'esi_peppol'); ?> &rarr;</center>
                                        </v:roundrect>
                                        <![endif]-->
                                        <!--[if !mso]><!-->
                                        <table role="presentation" cellspacing="0" cellpadding="0" border="0">
                                            <tr>
                                                <td style="border-radius: 5px; background-color: #0B3C61;">
                                                    <a href="<?php echo esc_url(admin_url('admin.php?page=esi-peppol-logs')); ?>" target="_blank" style="display: inline-block; background-color: #0B3C61; color: #ffffff; font-size: 15px; font-weight: bold; text-decoration: none; padding: 14px 35px; border-radius: 5px; font-family: Arial, Helvetica, sans-serif; border: none;">
                                                        <?php esc_html_e('Consulter les logs', 'esi_peppol'); ?> &rarr;
                                                    </a>
                                                </td>
                                            </tr>
                                        </table>
                                        <!--<![endif]-->
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>

                    <!-- FOOTER -->
                    <tr>
                        <td bgcolor="#0B3C61" style="background-color: #0B3C61; padding: 28px 45px; text-align: center;">
                            <table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%">
                                <tr>
                                    <td align="center" style="padding-bottom: 15px;">
                                        <img src="<?php echo $logo_url; ?>" alt="ESI PEPPOL" width="100" style="display: block; max-width: 100px; height: auto; border: 0;">
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center">
                                        <p style="color: #a8c5db; font-size: 12px; margin: 0 0 5px 0; font-family: Arial, Helvetica, sans-serif;">
                                            <?php esc_html_e('Contact', 'esi_peppol'); ?>
                                        </p>
                                        <p style="color: #ffffff; font-size: 13px; margin: 0; font-family: Arial, Helvetica, sans-serif;">
                                            087 27 92 14 &nbsp;-&nbsp; web@esi-informatique.com
                                        </p>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>

                </table>
                <!-- End email container -->

            </td>
        </tr>
    </table>
    <!-- End wrapper table -->
</body>
</html>

