<?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 select/dropdown field.
 *
 * @param array $args {
 *   @type string $id          Input id/name
 *   @type string $label       Label text
 *   @type mixed  $value       Current value
 *   @type array  $options     Associative array of value => label
 *   @type string $description Optional description
 *   @type bool   $disabled
 * }
 */
if ( ! isset( $args ) ) { return; }
$id       = isset( $args['id'] )          ? $args['id']          : '';
$label    = isset( $args['label'] )       ? $args['label']       : '';
$value    = isset( $args['value'] )       ? $args['value']       : '';
$options  = isset( $args['options'] )     ? (array) $args['options'] : array();
$desc     = isset( $args['description'] ) ? $args['description'] : '';
$disabled = ! empty( $args['disabled'] );
?>
<div class="tncfb-field tncfb-field-select<?php echo $disabled ? ' tncfb-field-disabled' : ''; ?>">
    <?php if ( $label ) : ?>
        <label for="<?php echo esc_attr( $id ); ?>" class="tncfb-label"><?php echo esc_html( $label ); ?><?php if ( ! empty( $args['tip'] ) ) { tncfb3d_tip( $args['tip'] ); } ?></label>
    <?php endif; ?>
    <select
        id="<?php echo esc_attr( $id ); ?>"
        name="<?php echo esc_attr( $id ); ?>"
        <?php disabled( $disabled, true ); ?>
    >
        <?php foreach ( $options as $opt_val => $opt_label ) : ?>
            <option value="<?php echo esc_attr( $opt_val ); ?>" <?php selected( $value, $opt_val ); ?>>
                <?php echo esc_html( $opt_label ); ?>
            </option>
        <?php endforeach; ?>
    </select>
    <?php if ( $desc ) : ?>
        <p class="tncfb-field-desc"><?php echo esc_html( $desc ); ?></p>
    <?php endif; ?>
</div>
