<?php

/**
 * File upload field
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $wpcb_field;

echo '<div class="wpcb-input-file';

$filename = '';
$image_path = '';
$image_url = '';

if ( isset( $wpcb_field['field']['file'] ) ) {
	$image_path = get_attached_file( esc_attr( $wpcb_field['field']['file'] ) );
	$image_url  = wp_get_attachment_image_url( $wpcb_field['field']['file'] , 'full' );

	if (is_file($image_path)) {
		$filename = basename($image_path);

		echo ' active">';
		echo '<span class="wpcb-input-file-image"><img src="' . esc_url( $image_url ) . '" alt="' . esc_attr( $filename ) . '"></span>';
		echo '<p class="wpcb-input-file-name">' . esc_attr( $filename ) . '</p>';
	} else {
		echo '"><span class="wpcb-input-file-image"></span><p class="wpcb-input-file-name"></p>';
	}
} else {
	echo '">';
}

echo '<button class="wpcb-input-clear-file">' . wp_kses( wpcb_get_icon('close'), wpcb_get_icon_kses_args() ) . '</button>';
echo '</div>';

echo '<input id="' . esc_attr( $wpcb_field_id ) . '" type="file" name="' . esc_attr( wpcb_sanitize_title( $wpcb_field['field']['label'] ) ) . '"';
wpcb_display_field_attributes( $wpcb_field );

if ( '' !== $filename ) {
	echo ' data-fileurl="' . esc_url( $image_url ) . '" data-filename="' . esc_attr( $filename ) . '"';
}

echo ' autocomplete="off" />';
