<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound, WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Template-scope variables and single-post admin meta reads, not globals or slow queries.
if ( ! defined( 'ABSPATH' ) ) { exit; }

/**
 * Reusable repeater field skeleton.
 *
 * This partial renders the outer container, row template, and Add button.
 * The caller passes a $args['rows_callback'] callable that renders a single row's HTML,
 * receiving ($index, $row_data) as arguments.
 *
 * @param array $args {
 *   @type string   $id              Unique identifier for this repeater
 *   @type string   $label           Section heading
 *   @type array    $rows            Current rows data (array of assoc arrays)
 *   @type callable $rows_callback   Callable that outputs HTML for a single row
 *   @type string   $add_button_text Label for the "Add row" button (default: 'Add Row')
 *   @type string   $description
 *   @type bool     $sortable        Whether rows are drag-sortable (default true)
 * }
 */
if ( ! isset( $args ) ) { return; }
$id          = isset( $args['id'] )              ? $args['id']              : '';
$label       = isset( $args['label'] )           ? $args['label']           : '';
$rows        = isset( $args['rows'] )            ? (array) $args['rows']    : array();
$callback    = isset( $args['rows_callback'] )   ? $args['rows_callback']   : null;
$add_text    = isset( $args['add_button_text'] ) ? $args['add_button_text'] : __( 'Add Row', 'tnc-flipbook-3d-lite' );
$empty_text  = isset( $args['empty_text'] )      ? $args['empty_text']      : __( 'No rows added yet.', 'tnc-flipbook-3d-lite' );
$desc        = isset( $args['description'] )     ? $args['description']     : '';
$sortable    = isset( $args['sortable'] )        ? (bool) $args['sortable'] : true;
?>
<div class="tncfb-repeater<?php echo $sortable ? ' tncfb-repeater-sortable' : ''; ?>"
     id="<?php echo esc_attr( $id ); ?>_repeater"
     data-repeater-id="<?php echo esc_attr( $id ); ?>">

    <?php if ( $label ) : ?>
        <div class="tncfb-repeater-header">
            <strong><?php echo esc_html( $label ); ?></strong>
            <?php if ( $desc ) : ?>
                <p class="tncfb-field-desc"><?php echo esc_html( $desc ); ?></p>
            <?php endif; ?>
        </div>
    <?php endif; ?>

    <div class="tncfb-repeater-rows" id="<?php echo esc_attr( $id ); ?>_rows">
        <?php if ( $callback && ! empty( $rows ) ) : ?>
            <?php foreach ( $rows as $index => $row ) : ?>
                <div class="tncfb-repeater-row" data-index="<?php echo esc_attr( $index ); ?>">
                    <?php if ( $sortable ) : ?>
                        <span class="tncfb-row-handle dashicons dashicons-move" title="<?php esc_attr_e( 'Drag to reorder', 'tnc-flipbook-3d-lite' ); ?>"></span>
                    <?php endif; ?>
                    <div class="tncfb-row-fields">
                        <?php call_user_func( $callback, $index, $row ); ?>
                    </div>
                    <button type="button" class="tncfb-row-remove tncfb-btn-pill tncfb-btn-pill--danger">
                        <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 6h18M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>
                        <span class="screen-reader-text"><?php esc_html_e( 'Remove row', 'tnc-flipbook-3d-lite' ); ?></span>
                    </button>
                </div>
            <?php endforeach; ?>
        <?php endif; ?>

        <?php if ( empty( $rows ) ) : ?>
            <p class="tncfb-repeater-empty"><?php echo esc_html( $empty_text ); ?></p>
        <?php endif; ?>
    </div>

    <?php if ( $callback ) : ?>
        <script type="text/html" id="<?php echo esc_attr( $id ); ?>_template">
            <?php if ( $sortable ) : ?>
                <span class="tncfb-row-handle dashicons dashicons-move" title="<?php esc_attr_e( 'Drag to reorder', 'tnc-flipbook-3d-lite' ); ?>"></span>
            <?php endif; ?>
            <div class="tncfb-row-fields">
                <?php call_user_func( $callback, '__INDEX__', array() ); ?>
            </div>
            <button type="button" class="tncfb-row-remove tncfb-btn-pill tncfb-btn-pill--danger">
                <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 6h18M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>
                <span class="screen-reader-text"><?php esc_html_e( 'Remove row', 'tnc-flipbook-3d-lite' ); ?></span>
            </button>
        </script>
    <?php endif; ?>

    <button type="button"
            class="tncfb-repeater-add button"
            data-repeater="<?php echo esc_attr( $id ); ?>">
        <span class="dashicons dashicons-plus-alt2"></span>
        <?php echo esc_html( $add_text ); ?>
    </button>
</div>
