<?php
/**
 * Product selection for a withdrawal request.
 *
 * @package SkyServers\Retragere
 *
 * @var array<string,mixed> $args
 */

declare(strict_types=1);

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

$ssrcLines = isset($args['lines']) && is_array($args['lines']) ? $args['lines'] : [];

if ([] === $ssrcLines) {
    return;
}

$ssrcEligible = [];
$ssrcExcluded = [];

foreach ($ssrcLines as $ssrcLine) {
    if (true === ($ssrcLine['eligible'] ?? false)) {
        $ssrcEligible[] = $ssrcLine;
    } else {
        $ssrcExcluded[] = $ssrcLine;
    }
}
?>
<div class="ssrc-lines">
    <?php
    /*
     * Marks that the picker was rendered. Without it, an empty selection is
     * ambiguous: it could mean "the customer unticked everything" or "no picker
     * was ever shown". Those need opposite handling.
     */
    ?>
    <input type="hidden" name="ssrc_lines_present" value="1">

    <?php if ([] !== $ssrcEligible) : ?>
        <p class="ssrc-lines-title"><?php esc_html_e('The products you are withdrawing from', 'skyservers-retragere-contract'); ?></p>

        <ul class="ssrc-lines-list">
            <?php foreach ($ssrcEligible as $ssrcLine) : ?>
                <?php $ssrcId = (int) $ssrcLine['item_id']; ?>
                <li class="ssrc-line">
                    <label>
                        <input type="checkbox" class="ssrc-line-toggle" data-item="<?php echo esc_attr((string) $ssrcId); ?>" checked>
                        <span class="ssrc-line-name">
                            <?php echo esc_html((string) $ssrcLine['name']); ?>
                            <?php if ('' !== trim((string) ($ssrcLine['sku'] ?? ''))) : ?>
                                <span class="ssrc-line-sku"><?php echo esc_html((string) $ssrcLine['sku']); ?></span>
                            <?php endif; ?>
                        </span>
                    </label>

                    <?php if ((int) $ssrcLine['available'] > 1) : ?>
                        <span class="ssrc-line-qty">
                            <label for="ssrc-qty-<?php echo esc_attr((string) $ssrcId); ?>"><?php esc_html_e('quantity', 'skyservers-retragere-contract'); ?></label>
                            <input type="number"
                                   id="ssrc-qty-<?php echo esc_attr((string) $ssrcId); ?>"
                                   name="ssrc_lines[<?php echo esc_attr((string) $ssrcId); ?>]"
                                   value="<?php echo esc_attr((string) $ssrcLine['available']); ?>"
                                   min="1"
                                   max="<?php echo esc_attr((string) $ssrcLine['available']); ?>">
                            <span class="description">
                                <?php
                                printf(
                                    /* translators: %d: available quantity. */
                                    esc_html__('of %d', 'skyservers-retragere-contract'),
                                    (int) $ssrcLine['available']
                                );
                                ?>
                            </span>
                        </span>
                    <?php else : ?>
                        <input type="hidden" name="ssrc_lines[<?php echo esc_attr((string) $ssrcId); ?>]" value="1">
                    <?php endif; ?>
                </li>
            <?php endforeach; ?>
        </ul>
    <?php endif; ?>

    <?php if ([] !== $ssrcExcluded) : ?>
        <p class="ssrc-lines-title"><?php esc_html_e('Products that cannot be included in the request', 'skyservers-retragere-contract'); ?></p>

        <ul class="ssrc-lines-list ssrc-lines-excluded">
            <?php foreach ($ssrcExcluded as $ssrcLine) : ?>
                <li class="ssrc-line">
                    <span class="ssrc-line-name"><?php echo esc_html((string) $ssrcLine['name']); ?></span>
                    <span class="ssrc-line-reason">
                        <?php echo esc_html(\SkyServers\Retragere\Wc\LineReasons::label((string) ($ssrcLine['reason'] ?? ''))); ?>
                    </span>
                </li>
            <?php endforeach; ?>
        </ul>
    <?php endif; ?>
</div>
