<?php

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

// Valeurs actuelles en base
$api_key  = get_option('esi_peppol_api_key', '');
$password = get_option('esi_peppol_password', '');
$email    = get_option('esi_peppol_email', '');
$logo_email_id = get_option('esi_peppol_logo_email_id', '');

// Récupérer l'URL de l'image pour la preview
$logo_email_url = '';
if ($logo_email_id) {
    $logo_email_url = wp_get_attachment_image_url((int) $logo_email_id, 'medium');
}

// Messages de feedback (optionnels, passés via $notice / $error)
/** @var string|null $notice */
/** @var string|null $error */
?>

<div class="wrap esi-peppol-settings-wrap">
    <h1><?php esc_html_e('Configuration ESI Peppol', 'esi_peppol'); ?></h1>

    <p>
        <?php
        printf(
            wp_kses_post(
                /* translators: %s: URL de la page de démonstration API */
                __('Vous pouvez tester et consulter la documentation de l\'API sur la page de démonstration&nbsp;: <a href="%s" target="_blank" rel="noopener noreferrer">ESIPeppol API Demo</a>.', 'esi_peppol')
            ),
            esc_url('https://scrada.esiweb.pro/api-demo.html')
        );
        ?>
    </p>

    <?php
    $vat_notice_message = \ESI_PEPPOL\controllers\PEPPOL_Plugin::get_vat_notice_message();
    if (!empty($vat_notice_message)) :
        // Si les identifiants ESI Peppol sont déjà saisis, on passe en notice "warning"
        $api_key  = (string) get_option('esi_peppol_api_key', '');
        $password = (string) get_option('esi_peppol_password', '');
        $notice_class = ($api_key === '' && $password === '') ? 'notice-info' : 'notice-warning';
        ?>
        <div class="notice <?php echo esc_attr($notice_class); ?>">
            <p><?php echo esc_html($vat_notice_message); ?></p>
        </div>
    <?php endif; ?>

    <?php if (!empty($notice)) : ?>
        <div class="notice notice-success is-dismissible">
            <p><?php echo esc_html($notice); ?></p>
        </div>
    <?php endif; ?>

    <?php if (!empty($error)) : ?>
        <div class="notice notice-error">
            <p><?php echo esc_html($error); ?></p>
        </div>
    <?php endif; ?>

    <div class="esi-peppol-settings-card">
        <form method="post">
            <?php wp_nonce_field('esi_peppol_save_settings', 'esi_peppol_nonce'); ?>

            <table class="form-table" role="presentation">
                <tbody>
                <tr>
                    <th scope="row">
                        <label for="esi_peppol_api_key"><?php esc_html_e('API Key', 'esi_peppol'); ?></label>
                    </th>
                    <td>
                        <input type="text"
                               class="regular-text"
                               id="esi_peppol_api_key"
                               name="esi_peppol_api_key"
                               value="<?php echo esc_attr($api_key); ?>"
                        />
                    </td>
                </tr>

                <tr>
                    <th scope="row">
                        <label for="esi_peppol_password"><?php esc_html_e('Password', 'esi_peppol'); ?></label>
                    </th>
                    <td>
                        <div class="esi-peppol-password-wrapper">
                            <input type="password"
                                   class="regular-text"
                                   id="esi_peppol_password"
                                   name="esi_peppol_password"
                                   value="<?php echo esc_attr($password); ?>"
                            />
                            <button type="button"
                                    class="button button-small esi-peppol-password-toggle"
                                    aria-label="<?php esc_attr_e('Afficher/Masquer le mot de passe', 'esi_peppol'); ?>"
                                    data-target="#esi_peppol_password">
                                <?php esc_html_e('Afficher', 'esi_peppol'); ?>
                            </button>
                        </div>
                    </td>
                </tr>

                <tr>
                    <th scope="row">
                        <label for="esi_peppol_email"><?php esc_html_e('Email', 'esi_peppol'); ?></label>
                    </th>
                    <td>
                        <input type="email"
                               class="regular-text"
                               id="esi_peppol_email"
                               name="esi_peppol_email"
                               value="<?php echo esc_attr($email); ?>"
                        />
                        <p class="description">
                            <?php esc_html_e('En cas d\'erreur d\'envoi des factures vous recevrez un email via l\'adresse encodée ci-dessus.', 'esi_peppol'); ?>
                        </p>
                    </td>
                </tr>

                <!-- <tr>
                    <th scope="row">
                        <label for="esi_peppol_logo_email"><?php esc_html_e('Logo Email', 'esi_peppol'); ?></label>
                    </th>
                    <td>
                        <input type="hidden"
                               id="esi_peppol_logo_email_id"
                               name="esi_peppol_logo_email_id"
                               value="<?php echo esc_attr($logo_email_id); ?>"
                        />
                        <button type="button"
                                id="esi-peppol-upload-logo"
                                class="button">
                            <?php esc_html_e('Choisir un logo', 'esi_peppol'); ?>
                        </button>
                        <button type="button"
                                id="esi-peppol-remove-logo"
                                class="button button-link-delete"
                                style="<?php echo $logo_email_id ? '' : 'display:none;'; ?>">
                            <?php esc_html_e('Supprimer', 'esi_peppol'); ?>
                        </button>
                        <div id="esi-peppol-logo-preview" class="esi-peppol-logo-preview" style="<?php echo $logo_email_url ? '' : 'display:none;'; ?>">
                            <?php if ($logo_email_url) : ?>
                                <img src="<?php echo esc_url($logo_email_url); ?>" alt="<?php esc_attr_e('Logo email', 'esi_peppol'); ?>" />
                            <?php endif; ?>
                        </div>
                        <p class="description">
                            <?php esc_html_e('Logo affiché dans les emails Peppol.', 'esi_peppol'); ?>
                        </p>
                    </td>
                </tr> -->
                </tbody>
            </table>

            <p class="submit">
                <button type="submit"
                        name="esi_peppol_action"
                        value="save"
                        class="button button-primary">
                    <?php esc_html_e('Enregistrer', 'esi_peppol'); ?>
                </button>

                <button type="button"
                        id="esi-peppol-test-connection"
                        class="button button-secondary"
                        style="margin-left: 10px;">
                    <?php esc_html_e('Tester la connexion', 'esi_peppol'); ?>
                </button>
            </p>

            <div id="esi-peppol-test-result"></div>
        </form>
    </div>
</div>


