<?php
/**
 * Withdrawal form.
 *
 * @package SkyServers\Retragere
 *
 * @var array<string,mixed> $args
 */

declare(strict_types=1);

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

$ssrcErrors   = isset($args['errors']) && is_array($args['errors']) ? $args['errors'] : [];
$ssrcSettings = isset($args['settings']) && is_array($args['settings']) ? $args['settings'] : [];

$ssrcMessages = [
    'name'         => __('Please fill in your name.', 'skyservers-retragere-contract'),
    'email'        => __('The email address is not valid.', 'skyservers-retragere-contract'),
    'contract_ref' => __('Please indicate the order or the contract.', 'skyservers-retragere-contract'),
    'reason'       => __('The reason exceeds the maximum length.', 'skyservers-retragere-contract'),
    'lines'        => __('Select at least one product, or choose “My order is not in the list” to send a free-form declaration.', 'skyservers-retragere-contract'),
    'hp'           => __('The request could not be sent. Please try again.', 'skyservers-retragere-contract'),
    'rate'         => __('You have sent too many requests in a short time. You can also send your withdrawal by email, to the shop\'s address.', 'skyservers-retragere-contract'),
    'nonce'        => __('The session has expired. Please submit the form again.', 'skyservers-retragere-contract'),
];
?>
<div class="ssrc-wrap">
    <p class="ssrc-legal"><?php echo esc_html((string) ($ssrcSettings['legal_text'] ?? '')); ?></p>

    <?php if ('' !== trim((string) ($ssrcSettings['return_notice'] ?? ''))) : ?>
        <p class="ssrc-notice"><?php echo esc_html((string) $ssrcSettings['return_notice']); ?></p>
    <?php endif; ?>

    <?php foreach ($ssrcErrors as $ssrcError) : ?>
        <?php if (isset($ssrcMessages[$ssrcError])) : ?>
            <p class="ssrc-error" role="alert"><?php echo esc_html($ssrcMessages[$ssrcError]); ?></p>
        <?php endif; ?>
    <?php endforeach; ?>

    <form class="ssrc-form" method="post" action="<?php echo esc_url((string) ($args['actionUrl'] ?? '')); ?>">
        <input type="hidden" name="action" value="ssrc_submit">
        <?php echo $args['nonceField']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- generated by wp_nonce_field(). ?>

        <?php
        if (!empty($args['orders'])) {
            $ssrcPicker = \SkyServers\Retragere\Wp\Shortcode::template(
                'order-picker',
                ['orders' => $args['orders'], 'selected' => $args['selected'] ?? 0]
            );

            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped inside the template.
            echo $ssrcPicker;
        } elseif (!empty($args['wcActive'])) {
            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped inside the template.
            echo \SkyServers\Retragere\Wp\Shortcode::template('guest-lookup', []);
        }
        ?>

        <div id="ssrc-lines-container">
            <?php
            if (!empty($args['lines'])) {
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped inside the template.
                echo \SkyServers\Retragere\Wp\Shortcode::template('order-lines', ['lines' => $args['lines']]);
            }
            ?>
        </div>

        <p>
            <label for="ssrc-name"><?php esc_html_e('Full name', 'skyservers-retragere-contract'); ?> <span aria-hidden="true">*</span></label><br>
            <input type="text" id="ssrc-name" name="name" required maxlength="120" autocomplete="name">
        </p>

        <p>
            <label for="ssrc-email"><?php esc_html_e('Email address for the confirmation', 'skyservers-retragere-contract'); ?> <span aria-hidden="true">*</span></label><br>
            <input type="email" id="ssrc-email" name="email" required maxlength="190" autocomplete="email">
        </p>

        <p>
            <label for="ssrc-ref"><?php esc_html_e('Order or contract number', 'skyservers-retragere-contract'); ?> <span aria-hidden="true">*</span></label><br>
            <input type="text" id="ssrc-ref" name="contract_ref" required maxlength="100">
        </p>

        <p>
            <label for="ssrc-reason"><?php esc_html_e('Reason (optional — the law does not require you to give one)', 'skyservers-retragere-contract'); ?></label><br>
            <textarea id="ssrc-reason" name="reason" rows="4" maxlength="2000"></textarea>
        </p>

        <p class="ssrc-hp" aria-hidden="true" style="position:absolute;left:-9999px;">
            <label for="ssrc-hp"><?php esc_html_e('Leave this field empty', 'skyservers-retragere-contract'); ?></label>
            <input type="text" id="ssrc-hp" name="hp" tabindex="-1" autocomplete="off">
        </p>

        <p>
            <button type="submit" class="ssrc-submit"><?php esc_html_e('Confirm the withdrawal', 'skyservers-retragere-contract'); ?></button>
        </p>
    </form>
</div>
