<?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 WP media uploader field.
 *
 * @param array $args {
 *   @type string $id           Hidden input id/name storing the attachment ID
 *   @type string $label
 *   @type int    $value        Current attachment ID
 *   @type string $media_type   'image', 'application/pdf', 'video', 'audio' (default: 'image')
 *   @type string $button_text  Button label (default: 'Choose File')
 *   @type string $description
 *   @type bool   $disabled
 * }
 */
if ( ! isset( $args ) ) { return; }
$id          = isset( $args['id'] )          ? $args['id']          : '';
$label       = isset( $args['label'] )       ? $args['label']       : '';
$value       = isset( $args['value'] )       ? absint( $args['value'] ) : 0;
$media_type  = isset( $args['media_type'] )  ? $args['media_type']  : 'image';
$button_text = isset( $args['button_text'] ) ? $args['button_text'] : __( 'Choose File', 'tnc-flipbook-3d-lite' );
$desc        = isset( $args['description'] ) ? $args['description'] : '';
$disabled    = ! empty( $args['disabled'] );

// Resolve current attachment details
$attachment_title = '';
$attachment_url   = '';
if ( $value > 0 ) {
    $attachment_title = get_the_title( $value );
    if ( 'image' === $media_type ) {
        $thumb = wp_get_attachment_image_src( $value, 'thumbnail' );
        $attachment_url = $thumb ? $thumb[0] : '';
    }
}
?>
<div class="tncfb-field tncfb-field-media<?php echo $disabled ? ' tncfb-field-disabled' : ''; ?>"
     data-media-type="<?php echo esc_attr( $media_type ); ?>">
    <?php if ( $label ) : ?>
        <label class="tncfb-label"><?php echo esc_html( $label ); ?></label>
    <?php endif; ?>

    <input type="hidden"
           id="<?php echo esc_attr( $id ); ?>"
           name="<?php echo esc_attr( $id ); ?>"
           value="<?php echo esc_attr( $value ); ?>" />

    <div class="tncfb-media-preview <?php echo $value > 0 ? '' : 'tncfb-hidden'; ?>">
        <?php if ( 'image' === $media_type && $attachment_url ) : ?>
            <img src="<?php echo esc_url( $attachment_url ); ?>" alt="" class="tncfb-media-thumb" />
        <?php endif; ?>
        <span class="tncfb-media-filename"><?php echo esc_html( $attachment_title ); ?></span>
        <button type="button" class="tncfb-media-remove tncfb-btn-pill tncfb-btn-pill--danger"
                <?php disabled( $disabled, true ); ?>>
            <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>
            <?php esc_html_e( 'Remove', 'tnc-flipbook-3d-lite' ); ?>
        </button>
    </div>

    <button type="button"
            class="tncfb-media-upload button"
            <?php disabled( $disabled, true ); ?>>
        <?php echo esc_html( $button_text ); ?>
    </button>

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