<?php
if ( ! defined( 'ABSPATH' ) ) exit;
/**
 * Repeatable single-line text rows.
 *
 * Storage shape (preserved from Redux):
 *   sequential array of strings, e.g. [ 'addr1', 'addr2' ]
 *
 * @var array  $field
 * @var mixed  $value
 * @var string $name_base
 */
$rows     = is_array( $value ) ? array_values( $value ) : array();
$add_text = isset( $field['add_text'] ) ? $field['add_text'] : 'Add';
$max      = isset( $field['max'] ) ? (int) $field['max'] : 0;
$unique   = ! empty( $field['unique'] );
?>
<div class="cn-multitext"
     data-name-base="<?php echo esc_attr( $name_base ); ?>"
     data-max="<?php echo esc_attr( $max ); ?>"
     <?php if ( $unique ) echo 'data-unique="1"'; ?>>
    <div class="cn-multitext-rows">
        <?php if ( empty( $rows ) ) : ?>
            <div class="cn-multitext-row">
                <input type="text"
                       name="<?php echo esc_attr( $name_base . '[]' ); ?>"
                       value=""
                       class="cn-input cn-multitext-input" />
                <button type="button" class="cn-btn cn-btn-icon cn-btn-danger cn-multitext-remove" aria-label="Remove"></button>
            </div>
        <?php else : ?>
            <?php foreach ( $rows as $row ) : ?>
                <div class="cn-multitext-row">
                    <input type="text"
                           name="<?php echo esc_attr( $name_base . '[]' ); ?>"
                           value="<?php echo esc_attr( is_scalar( $row ) ? $row : '' ); ?>"
                           class="cn-input cn-multitext-input" />
                    <button type="button" class="cn-btn cn-btn-icon cn-btn-danger cn-multitext-remove" aria-label="Remove"></button>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>
    <button type="button" class="cn-btn cn-btn-secondary cn-multitext-add">
        <i class="icon ion-md-add cn-btn-icon-lead" aria-hidden="true"></i>
        <?php echo esc_html( $add_text ); ?>
    </button>
</div>
